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 |
|
funk.phenomena
Posting Yak Master
121 Posts |
Posted - 2012-03-15 : 14:14:54
|
| Hi All - I have the following table. The PRICE_CODE is the unique identifier, but is duplicated beacause it has multiple Dates, and descriptions:[CODE]PRICE_CODE | DATE | DESCRIPTION1003ABCDEF 01/01/11 Plan11003ABCDEF 01/03/12 Plan21003ABCDEF 01/31/12 Plan3[/CODE]I need run a query that outputs the distinct Price_Code and other fields based on the latest Date. Output would be: [CODE]1003ABCDEF 01/31/12 Plan3[/CODE]I've figured out how to output the results based on the latest "Date" however it still outputs multiple records because the same price_code has multiple descriptions. How can I make the description distinct?[CODE]SELECT PRICE_CODE, DESCRIPTION, DATE=MAX(DATE) FROM TABLE1 GROUP BY PRICE_CODE, DESCRIPTION[/CODE] |
|
|
X002548
Not Just a Number
15586 Posts |
|
|
Lamprey
Master Smack Fu Yak Hacker
4614 Posts |
Posted - 2012-03-15 : 14:34:26
|
| Well PRICE_CODE is NOT the unique identifier. :)You can do as Brett suggested. Additionally, you can use the ROW_NUMBER() function, an OUTTER APPLY, a join to derived table, etc.. Lots of options. |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-03-15 : 15:23:39
|
using cross applySELECT t.*FROM table tCROSS APPLY (SELECT MAX([date]) AS latest FROM table WHERE PRICE_CODE = t.PRICE_CODE )t1WHERE t1.latest = t.[Date] ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
X002548
Not Just a Number
15586 Posts |
|
|
|
|
|