473,385 Members | 1,356 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

SQL question

I have a solution using two queries wich I wish to combine into a single
statement, to create a recordset.

the first which is named qryAllMovements
----------------------------------------------------------------------------
-----
SELECT tblStockItems.ProdId, tblMovements.mQuantity, tblMovements.sDate
FROM tblStockItems INNER JOIN (tblLocations INNER JOIN tblMovements ON
tblLocations.LocId = tblMovements.LocId) ON tblStockItems.stockId =
tblMovements.stockId
WHERE (((tblMovements.sDate)<#10/3/2003#) AND
((tblLocations.StImpFlag)=-1));
----------------------------------------------------------------------------
---------------
and the second

----------------------------------------------------------------------
SELECT qryAllMovements.ProdId, qryAllMovements.mQuantity
FROM qryAllMovements
GROUP BY qryAllMovements.ProdId, qryAllMovements.mQuantity
------------------------------------------------------------------------

Basically Im filtering the first using a date but leaving the date out of
the result, , then aggregating the resultss in the second. .
Any help most welcome

Gerry Abbott

Nov 13 '05 #1
5 1281
"Gerry Abbott" <pl****@ask.ie> wrote in message
news:nz***************@news.indigo.ie...
I have a solution using two queries wich I wish to combine into a single
statement, to create a recordset.


post the table structures with some sample data and the output you need and
someone will be able to help.
Nov 13 '05 #2

"John Winterbottom" <as******@hotmail.com> wrote in message
news:2h************@uni-berlin.de...
"Gerry Abbott" <pl****@ask.ie> wrote in message
news:nz***************@news.indigo.ie...
I have a solution using two queries wich I wish to combine into a single
statement, to create a recordset.

post the table structures with some sample data and the output you need

and someone will be able to help.


tblStockItems:
StockId(Key)
ProdId (number)

tblMovements
MovementId (key)
StockId (number)
sDate (date)
LocId(number)

tblProducts
ProdId (key)

Im filtering on the date in the movements table.
Each stock item has a product associated with it.
Each movement has a location associated with it.
I only include movements for locations for which a stock impact flag
(stImpFlag) is set.

Once its filtered on the date and the stockImpactFlag, I no longer need
these or the location flags.
I now have the total stock movements in and out of all stock locations
before a user defined date, forr all products.
So all i've got to do is to group the result by product, to get the net
stock total for each. (the second query)

Im really looking for a way to nest these sql statements into a single
statement, and am more interested in understanding the principles, than a
definitieve solution to this particular problem. I could not aggregate the
the query once the date remained in it for all the movements, thats' why i
ended up with two queries.

Thanks in advance.

Gerry






Nov 13 '05 #3
> > post the table structures with some sample data and the output you need
and
someone will be able to help.


tblStockItems:
StockId(Key)
ProdId (number)

tblMovements
MovementId (key)
StockId (number)
sDate (date)
LocId(number)

tblProducts
ProdId (key)

Im filtering on the date in the movements table.
Each stock item has a product associated with it.
Each movement has a location associated with it.
I only include movements for locations for which a stock impact flag
(stImpFlag) is set.


Your example tables don't show a locations table, and tblMovements doesn't
show a quantity. Apart from that you haven't given any sample data, so the
following is just a guess

select s.ProdID, Sum(m.Qty) as qtyMoved
from tblStockItems as s inner join
(
tblLocations as l inner join tblMovements as m on l.LocID = m.LocID
) on s.StockID = m.StockID
where l.stImpFlag=True
and m.sDate=[enterDate]
group by s.ProdID;
Nov 13 '05 #4
> > post the table structures with some sample data and the output you need
and
someone will be able to help.


tblStockItems:
StockId(Key)
ProdId (number)

tblMovements
MovementId (key)
StockId (number)
sDate (date)
LocId(number)

tblProducts
ProdId (key)

Im filtering on the date in the movements table.
Each stock item has a product associated with it.
Each movement has a location associated with it.
I only include movements for locations for which a stock impact flag
(stImpFlag) is set.


Your example tables don't show a locations table, and tblMovements doesn't
show a quantity. Apart from that you haven't given any sample data, so the
following is just a guess

select s.ProdID, Sum(m.Qty) as qtyMoved
from tblStockItems as s inner join
(
tblLocations as l inner join tblMovements as m on l.LocID = m.LocID
) on s.StockID = m.StockID
where l.stImpFlag=True
and m.sDate=[enterDate]
group by s.ProdID;
Nov 13 '05 #5
My reply has somehow become detached from this thread!!

Here is my reply

Thanks John,
Think I've chosen an unnecesarily complex query as an example of what i'm
trying to solve. I've been building SQL using the QBE design view. The
inclusion of the 'where' clause as you outlined below in your example, in
place of the 'group' allows me to get close to where I want to be, by
allowing me to group, aggregate and filter in the same query, thus being
able to remove the date.
A simpler example which works is below. This shows me the aggegrate total
for each ProdId which has a total, ie is not null. .

----------------------------------------------------------------------------
--------------------------------------------
TEST1
SELECT tblStockItems.ProdId, Sum(tblMovements.mQuantity) AS SumOfmQuantity
FROM tblStockItems INNER JOIN tblMovements ON tblStockItems.stockId =
tblMovements.stockId
WHERE (((tblMovements.LocId)=sysconst(3)) AND ((tblMovements.sDate) Between
[parameter1] And [parameter2]))
GROUP BY tblStockItems.ProdId;
----------------------------------------------------------------------------
---------------------------------------------

I then tried adding my tblProducts, and was forced to change the joins
between tblStockItems and tbl Movements, and between tblProducts and
tblStockIetms, to Left Joins.
----------------------------------------------------------------------------
---------
TEST1
SELECT tblProducts.ProdId, Sum(tblMovements.mQuantity) AS Total
FROM tblProducts LEFT JOIN (tblStockItems LEFT JOIN tblMovements ON
tblStockItems.stockId = tblMovements.stockId) ON tblProducts.ProdId =
tblStockItems.ProdId
WHERE (((tblMovements.LocId)=sysconst(3)) AND ((tblMovements.sDate) Between
[parameter1] And [parameter2]))
GROUP BY tblProducts.ProdId
ORDER BY tblProducts.ProdId;
----------------------------------------------------------------------------
--------

But the result of this is the same as the first one above. and I don't get
the full list of products, only those for which the aggregate value is non
null.
So i'm left with generating another query, by adding the tblProducts, and
creating a left join with the prodId from either of the above queries, to
display all the products, including the ones for which there is no stock
items and hence no movements to compute. This is what I want.

----------------------------------------------------------------------------
-------
SELECT tblProducts.ProdId, Test1.Total
FROM Test1 RIGHT JOIN tblProducts ON Test1.ProdId = tblProducts.ProdId;
----------------------------------------------------------------------------
-------
But this leaves me once agin with two queries, one derived from the other.
Im hoping this makes my problem a little clearer.
Gerry Abbott


"John Winterbottom" <as******@hotmail.com> wrote in message
news:2h************@uni-berlin.de...
post the table structures with some sample data and the output you
need and
someone will be able to help.
tblStockItems:
StockId(Key)
ProdId (number)

tblMovements
MovementId (key)
StockId (number)
sDate (date)
LocId(number)

tblProducts
ProdId (key)

Im filtering on the date in the movements table.
Each stock item has a product associated with it.
Each movement has a location associated with it.
I only include movements for locations for which a stock impact flag
(stImpFlag) is set.


Your example tables don't show a locations table, and tblMovements doesn't
show a quantity. Apart from that you haven't given any sample data, so the
following is just a guess

select s.ProdID, Sum(m.Qty) as qtyMoved
from tblStockItems as s inner join
(
tblLocations as l inner join tblMovements as m on l.LocID =

m.LocID ) on s.StockID = m.StockID
where l.stImpFlag=True
and m.sDate=[enterDate]
group by s.ProdID;

Nov 13 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Mohammed Mazid | last post by:
Can anyone please help me on how to move to the next and previous question? Here is a snippet of my code: Private Sub cmdNext_Click() End Sub Private Sub cmdPrevious_Click() showrecord
3
by: Stevey | last post by:
I have the following XML file... <?xml version="1.0"?> <animals> <animal> <name>Tiger</name> <questions> <question index="0">true</question> <question index="1">true</question> </questions>
7
by: nospam | last post by:
Ok, 3rd or is it the 4th time I have asked this question on Partial Types, so, since it seems to me that Partial Types is still in the design or development stages at Microsoft, I am going to ask...
3
by: Ekqvist Marko | last post by:
Hi, I have one Access database table including questions and answers. Now I need to give answer id automatically to questionID column. But I don't know how it is best (fastest) to do? table...
10
by: glenn | last post by:
I am use to programming in php and the way session and post vars are past from fields on one page through to the post page automatically where I can get to their values easily to write to a...
10
by: Rider | last post by:
Hi, simple(?) question about asp.net configuration.. I've installed ASP.NET 2.0 QuickStart Sample successfully. But, When I'm first start application the follow message shown. ========= Server...
53
by: Jeff | last post by:
In the function below, can size ever be 0 (zero)? char *clc_strdup(const char * CLC_RESTRICT s) { size_t size; char *p; clc_assert_not_null(clc_strdup, s); size = strlen(s) + 1;
56
by: spibou | last post by:
In the statement "a *= expression" is expression assumed to be parenthesized ? For example if I write "a *= b+c" is this the same as "a = a * (b+c)" or "a = a * b+c" ?
2
by: Allan Ebdrup | last post by:
Hi, I'm trying to render a Matrix question in my ASP.Net 2.0 page, A matrix question is a question where you have several options that can all be rated according to several possible ratings (from...
3
by: Zhang Weiwu | last post by:
Hello! I wrote this: ..required-question p:after { content: "*"; } Corresponding HTML: <div class="required-question"><p>Question Text</p><input /></div> <div...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.