Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 problem with a query

Author  Topic 

MrPeroni
Starting Member

9 Posts

Posted - 2008-07-21 : 06:27:12


This is maybe a easy thing to do but I'm pretty new to SQL, so i need some help.


I need to do a query that gets all the items that has been located at location "Missing" but is
at a different location today.



Four tables:

tbl_item (info about the specific item, barcode, color etc.

tbl_itemName (specific name of the product, ex J300)

tbl_location (where the item is located, ex MainStorage, Storage23, Missing, Handed Out etc)

tbl_admin (the person that have changed the location of a specific item)



This is the query I've done. I know something is wrong with it because I still only get location "Missing" in the result.





DECLARE @datFrom datetime, @datTo datetime;

SET @datFrom = '2008-04-01'

SET @datTo = '2008-07-05'

SELECT lo1.vc_location_desc AS Plats, in.strName AS Produkt, i1.strItemBarCode AS BarCode, i1.dtModified AS Datum, a.strFirstname AS Förnamn, a.strLastName AS Efternamn

FROM tbl_location lo1
INNER JOIN tbl_item i1 ON lo1.i_location_id=i1.intLocationID
INNER JOIN tbl_itemName in ON i1.intNameID=tbl_itemName.intID
INNER JOIN tbl_admin a ON i1.intAdminID=a.ingAdmin

WHERE i1.strItemBarCode IN(select i2.strItemBarCode
from tbl_item i2, tbl_location lo2
where i_location_id=i2.intLocationID
and lo2.vc_location_desc LIKE '%Missing%')

AND i1.dtModified > @datFrom

AND i1.dtModified < @datTo

ORDER BY i1.dtModified DESC




Thx guys!

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-07-21 : 06:31:08
how does you capture change of locations? what's the significance of handedout?
Go to Top of Page

MrPeroni
Starting Member

9 Posts

Posted - 2008-07-21 : 07:07:13
quote:
Originally posted by visakh16

how does you capture change of locations? what's the significance of handedout?



A items location can change if the item has been sold, at the inventory, if the item is transported from a MainStorage to Storage10 etc. Did I understand your question right?

If a item is labeld as "handed out" it has been handed out without having a connection to a Order. If a item is listed as "missing" it has not been found at the inventory.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-07-21 : 10:20:53
quote:
Originally posted by MrPeroni

quote:
Originally posted by visakh16

how does you capture change of locations? what's the significance of handedout?



A items location can change if the item has been sold, at the inventory, if the item is transported from a MainStorage to Storage10 etc. Did I understand your question right?

If a item is labeld as "handed out" it has been handed out without having a connection to a Order. If a item is listed as "missing" it has not been found at the inventory.


so will each change in location puts a new record in the tbl_location? if not, where will you be keeping the history info?
Go to Top of Page

MrPeroni
Starting Member

9 Posts

Posted - 2008-07-21 : 10:26:48
Yes, the tbl_location keep the history info.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-07-21 : 10:43:59
[code]DECLARE @datFrom datetime, @datTo datetime;

SET @datFrom = '2008-04-01'

SET @datTo = '2008-07-05'

SELECT lo1.vc_location_desc AS Plats, in.strName AS Produkt, i1.strItemBarCode AS BarCode, i1.dtModified AS Datum, a.strFirstname AS Förnamn, a.strLastName AS Efternamn

FROM tbl_location lo1
INNER JOIN tbl_item i1 ON lo1.i_location_id=i1.intLocationID
INNER JOIN tbl_itemName in ON i1.intNameID=tbl_itemName.intID
INNER JOIN tbl_admin a ON i1.intAdminID=a.ingAdmin
INNER JOIN (SELECT i2.intNameID,MAX(i2.dtModified) AS MaxDate
FROM tbl_item i2
INNER JOIN tbl_location lo2
GROUP BY i2.intNameID
HAVING SUM(CASE WHEN lo2.vc_location_desc LIKE '%Missing%' THEN 1 ELSE 0 END)=1) tmp
ON tmp.intNameID=i1.intNameID
AND tmp.MaxDate=i1.dtModified
WHERE i1.dtModified > @datFrom
AND i1.dtModified < @datTo
AND lo1.vc_location_desc NOT LIKE '%Missing%'
ORDER BY i1.dtModified DESC[/code]
Go to Top of Page

MrPeroni
Starting Member

9 Posts

Posted - 2008-07-22 : 09:25:09
thx foor your answer!

I changed the "in" to "imn" because I got an error on that.

But now I get "Incorrect syntax near the keyword 'GROUP'."

DECLARE @datFrom datetime, @datTo datetime;

SET @datFrom = '2008-04-01'

SET @datTo = '2008-07-05'

SELECT lo1.vc_location_desc AS Plats, imn.strName AS Produkt, i1.strItemBarCode AS BarCode, i1.dtModified AS Datum, a.strFirstname AS Förnamn, a.strLastName AS Efternamn

FROM tbl_location lo1
INNER JOIN tbl_item i1 ON lo1.i_location_id=i1.intLocationID
INNER JOIN tbl_itemName imn ON i1.intNameID=imn.intID
INNER JOIN tbl_admin a ON i1.intAdminID=a.ingAdmin
INNER JOIN (SELECT i2.intNameID,MAX(i2.dtModified) AS MaxDate
FROM tbl_item i2
INNER JOIN tbl_location lo2
GROUP BY i2.intNameID
HAVING SUM(CASE WHEN lo2.vc_location_desc LIKE '%Missing%' THEN 1 ELSE 0 END)=1) tmp
ON tmp.intNameID=i1.intNameID
AND tmp.MaxDate=i1.dtModified
WHERE i1.dtModified > @datFrom
AND i1.dtModified < @datTo
AND lo1.vc_location_desc NOT LIKE '%Missing%'
ORDER BY i1.dtModified DESC
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-07-22 : 09:47:17
missed an on

DECLARE @datFrom datetime, @datTo datetime; 

SET @datFrom = '2008-04-01'

SET @datTo = '2008-07-05'

SELECT lo1.vc_location_desc AS Plats, imn.strName AS Produkt, i1.strItemBarCode AS BarCode, i1.dtModified AS Datum, a.strFirstname AS Förnamn, a.strLastName AS Efternamn

FROM tbl_location lo1
INNER JOIN tbl_item i1 ON lo1.i_location_id=i1.intLocationID
INNER JOIN tbl_itemName imn ON i1.intNameID=imn.intID
INNER JOIN tbl_admin a ON i1.intAdminID=a.ingAdmin
INNER JOIN (SELECT i2.intNameID,MAX(i2.dtModified) AS MaxDate
FROM tbl_item i2
INNER JOIN tbl_location lo2
ON lo2.i_location_id=i2.intLocationID
GROUP BY i2.intNameID
HAVING SUM(CASE WHEN lo2.vc_location_desc LIKE '%Missing%' THEN 1 ELSE 0 END)=1) tmp
ON tmp.intNameID=i1.intNameID
AND tmp.MaxDate=i1.dtModified
WHERE i1.dtModified > @datFrom
AND i1.dtModified < @datTo
AND lo1.vc_location_desc NOT LIKE '%Missing%'
ORDER BY i1.dtModified DESC
Go to Top of Page

MrPeroni
Starting Member

9 Posts

Posted - 2008-07-22 : 10:38:31

The syntax of the question is correct, so far, all good. I get two rows in return when I execute the query. But it must be something wrong with it, because I now for a fact that more than 200 items have been on location "Missing". Oh wait a minute...
When I look at these items history (the two items i got from the execution of the query), it says that those two items never have been at location "Missing"...ever. weird.

Something with the question most be wrong :/

Go to Top of Page
   

- Advertisement -