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 |
|
josh_sql
Starting Member
2 Posts |
Posted - 2012-08-31 : 03:41:21
|
| You have three fields ID, Date and Total. Your table contains multiple rows for the same day which is valid data however for reporting purpose you need to show only one row per day. The row with the highest ID per day should be returned the rest should be hidden from users (not returned).ID Date Total1 2012-08-25 151 2012 -08-25 191 2012-08-27 253 2012-08-25 303 2012 -08-29 295 2012-08-25 25Output:ID Date Total5 2012-08-25 251 2012-08-27 253 2012 -08-29 29I tried using this querySELECT MAX(ID), Date FROM tblSalesGROUP BY Datebut I also need the Total column to be displayed. Please let me know how this can be done. Thanks in advance.Josh |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2012-08-31 : 03:53:42
|
[code]select *from( select ID, Date, Total, rn = row_number() over (partition by Date order by ID desc) from tblSales) dwhere rn = 1[/code] KH[spoiler]Time is always against us[/spoiler] |
 |
|
|
josh_sql
Starting Member
2 Posts |
Posted - 2012-08-31 : 05:18:01
|
| Thanks Khtan, it works great! Seems like row_number and Partition could be very useful functions.Josh |
 |
|
|
arpana patil
Starting Member
24 Posts |
Posted - 2012-09-04 : 01:38:31
|
| select [ID],[Date],[Total] from [dbo].[Test] a where [ID] in (select max([ID]) from [dbo].[Test] b where a.[Date]=b.[Date]) order by [Date] |
 |
|
|
|
|
|