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
 max date per item per warehouse

Author  Topic 

Craazycat
Starting Member

1 Post

Posted - 2012-09-04 : 15:55:56
How do I restrict this to give me only the max date record per each Item # per WH_ID? I only want 1 record for Truck 41 with the latest date of 12/13/11.

Select
invent.[Location Code] AS WH_ID
,invent.[ITEM NO#] as ItemNo
,invent.[Posting Date] as Date
,invent.[Remaining Quantity] as Qty
,invent.[Unit Cost] as Cost
FROM OPENROWSET
('MSDASQL', 'Driver={Microsoft Text Driver (*.txt; *.csv)};
DefaultDir=Z:\\mydata;','select * from Itemledger.txt')invent
Left join (SELECT [Location Code],[ITEM NO#],max([Posting Date] )[Date] FROM OPENROWSET
('MSDASQL', 'Driver={Microsoft Text Driver (*.txt; *.csv)};
DefaultDir=Z:\\mydata;','select * from Itemledger.txt')
Group by [Location Code], [ITEM NO#], [Posting Date]) B
On B.[Location Code] = invent.[Location Code] and B.[ITEM NO#] = invent.[ITEM NO#]
Where invent.[Remaining Quantity] > '0'
and invent.[ITEM NO#] = '10-BH-5'
order by invent.[ItemNo]


This is my result:
WH_ID ItemNo Date Qty Cost
MAIN 10-BH-5 08/07/12 20 0.93
TRUCK 13 10-BH-5 09/01/11 6 1.52
TRUCK 27 10-BH-5 03/03/11 6 1.44
TRUCK 41 10-BH-5 10/26/11 2 1.52
TRUCK 41 10-BH-5 10/26/11 2 1.52
TRUCK 41 10-BH-5 12/13/11 4 1.51
TRUCK 41 10-BH-5 12/13/11 4 1.51
TRUCK 51 10-BH-5 02/22/12 1 1.09

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-09-05 : 21:25:29
[code]
Select
invent.[Location Code] AS WH_ID
,invent.[ITEM NO#] as ItemNo
,invent.[Posting Date] as Date
,invent.[Remaining Quantity] as Qty
,invent.[Unit Cost] as Cost
FROM OPENROWSET
('MSDASQL', 'Driver={Microsoft Text Driver (*.txt; *.csv)};
DefaultDir=Z:\\mydata;','select * from Itemledger.txt')invent
Left join (SELECT [Location Code],[ITEM NO#],max([Posting Date] )[Date] FROM OPENROWSET
('MSDASQL', 'Driver={Microsoft Text Driver (*.txt; *.csv)};
DefaultDir=Z:\\mydata;','select * from Itemledger.txt')
Group by [Location Code], [ITEM NO#]) B
On B.[Location Code] = invent.[Location Code] and B.[ITEM NO#] = invent.[ITEM NO#]
and B.[Date] = invent.[Posting Date]
Where invent.[Remaining Quantity] > '0'
and invent.[ITEM NO#] = '10-BH-5'
order by invent.[ItemNo]
[/code]

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -