| 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 EfternamnFROM tbl_location lo1INNER JOIN tbl_item i1 ON lo1.i_location_id=i1.intLocationIDINNER JOIN tbl_itemName in ON i1.intNameID=tbl_itemName.intIDINNER JOIN tbl_admin a ON i1.intAdminID=a.ingAdminWHERE 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 > @datFromAND i1.dtModified < @datToORDER BY i1.dtModified DESCThx 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? |
 |
|
|
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. |
 |
|
|
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? |
 |
|
|
MrPeroni
Starting Member
9 Posts |
Posted - 2008-07-21 : 10:26:48
|
| Yes, the tbl_location keep the history info. |
 |
|
|
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 EfternamnFROM tbl_location lo1INNER JOIN tbl_item i1 ON lo1.i_location_id=i1.intLocationIDINNER JOIN tbl_itemName in ON i1.intNameID=tbl_itemName.intIDINNER JOIN tbl_admin a ON i1.intAdminID=a.ingAdminINNER 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) tmpON tmp.intNameID=i1.intNameIDAND tmp.MaxDate=i1.dtModifiedWHERE i1.dtModified > @datFromAND i1.dtModified < @datToAND lo1.vc_location_desc NOT LIKE '%Missing%'ORDER BY i1.dtModified DESC[/code] |
 |
|
|
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 EfternamnFROM tbl_location lo1INNER JOIN tbl_item i1 ON lo1.i_location_id=i1.intLocationIDINNER JOIN tbl_itemName imn ON i1.intNameID=imn.intIDINNER JOIN tbl_admin a ON i1.intAdminID=a.ingAdminINNER 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) tmpON tmp.intNameID=i1.intNameIDAND tmp.MaxDate=i1.dtModifiedWHERE i1.dtModified > @datFromAND i1.dtModified < @datToAND lo1.vc_location_desc NOT LIKE '%Missing%'ORDER BY i1.dtModified DESC |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-07-22 : 09:47:17
|
missed an onDECLARE @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 EfternamnFROM tbl_location lo1INNER JOIN tbl_item i1 ON lo1.i_location_id=i1.intLocationIDINNER JOIN tbl_itemName imn ON i1.intNameID=imn.intIDINNER JOIN tbl_admin a ON i1.intAdminID=a.ingAdminINNER JOIN (SELECT i2.intNameID,MAX(i2.dtModified) AS MaxDateFROM tbl_item i2INNER JOIN tbl_location lo2ON lo2.i_location_id=i2.intLocationIDGROUP BY i2.intNameIDHAVING SUM(CASE WHEN lo2.vc_location_desc LIKE '%Missing%' THEN 1 ELSE 0 END)=1) tmpON tmp.intNameID=i1.intNameIDAND tmp.MaxDate=i1.dtModifiedWHERE i1.dtModified > @datFromAND i1.dtModified < @datToAND lo1.vc_location_desc NOT LIKE '%Missing%'ORDER BY i1.dtModified DESC |
 |
|
|
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 :/ |
 |
|
|
|