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.
| 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 CostFROM OPENROWSET('MSDASQL', 'Driver={Microsoft Text Driver (*.txt; *.csv)};DefaultDir=Z:\\mydata;','select * from Itemledger.txt')inventLeft 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]) BOn 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 CostMAIN 10-BH-5 08/07/12 20 0.93TRUCK 13 10-BH-5 09/01/11 6 1.52TRUCK 27 10-BH-5 03/03/11 6 1.44TRUCK 41 10-BH-5 10/26/11 2 1.52TRUCK 41 10-BH-5 10/26/11 2 1.52TRUCK 41 10-BH-5 12/13/11 4 1.51TRUCK 41 10-BH-5 12/13/11 4 1.51TRUCK 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 CostFROM OPENROWSET('MSDASQL', 'Driver={Microsoft Text Driver (*.txt; *.csv)};DefaultDir=Z:\\mydata;','select * from Itemledger.txt')inventLeft 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#]) BOn 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 MVPhttp://visakhm.blogspot.com/ |
 |
|
|
|
|
|
|
|