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 |
|
arnesbeganovic
Starting Member
9 Posts |
Posted - 2010-10-11 : 03:00:59
|
| HiI work with material economics and I get purchase orders from our customers. One PO has date, few material numbers, type of orders, date and so on. All PO’s goes in one table with columns, and one below other. I have till example purchase order G111 and it has 3 materials with a date 2010-10-09. After few days came another one G222 with 5 materials, but 2 of them are same. It looks like here in my table: PO Material Date Type Price CurrencyG111 111111 2010-10-09 AAAA 56 USDG111 222222 2010-10-09 AAAA 13 USDG111 333333 2010-10-09 AAAA 52 USDG222 444444 2010-10-13 AAAA 100 USDG222 111111 2010-10-13 AAAA 60 USDG222 222222 2010-10-13 AAAA 20 USDG222 555555 2010-10-13 AAAA 20 USDG222 666666 2010-10-13 AAAA 20 USDNow, I need to extract all materials with latest prices and all information which goes with it. 111111 60 2010-10-13 AAAA USD222222 20 2010-10-13 AAAA USD333333 52 2010-10-09 AAAA USD444444 100 2010-10-13 AAAA USD555555 20 2010-10-13 AAAA USD666666 20 2010-10-13 AAAA USDNote that material 333333 is from PO G111 and all others are from PO G222. I have tried with:SELECT Material, max(Date) From TableName Group by MaterialAnd a get:111111 2010-10-13222222 2010-10-13333333 2010-10-09444444 2010-10-13555555 2010-10-13666666 2010-10-13So, I get right material and date but I cannot add column Price or Type or Currency in it. I am guessing that I need to have more complicated code but I am not so good with SQL. Can you help me? |
|
|
senthil_nagore
Master Smack Fu Yak Hacker
1007 Posts |
Posted - 2010-10-11 : 03:22:47
|
| Try thisSelect tbl.Material,tbl.max_date,Type, Price, Currency from (SELECT Material, max(Date) as max_date From DatabaseName Group by Material) tblinner join <table_name> a on a. Material=tbl.Material and a. Date =tbl.max_dateSenthil.C------------------------------------------------------[Microsoft][ODBC SQL Server Driver]Operation canceledhttp://senthilnagore.blogspot.com/ |
 |
|
|
arnesbeganovic
Starting Member
9 Posts |
Posted - 2010-10-11 : 06:41:05
|
| It works. Thanks :) |
 |
|
|
senthil_nagore
Master Smack Fu Yak Hacker
1007 Posts |
Posted - 2010-10-11 : 07:18:36
|
quote: Originally posted by arnesbeganovic It works. Thanks :)
Welcome Senthil.C------------------------------------------------------[Microsoft][ODBC SQL Server Driver]Operation canceledhttp://senthilnagore.blogspot.com/ |
 |
|
|
|
|
|
|
|