Site Sponsored By: SQLDSC - SQL Server Desired State Configuration
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.
Below are my fields. The goal is to return the first non zero value for each country for the most recent date, and it needs to be summed. Sample Data: (Final Result is at bottom)Country Date ValueUS 1/1/2008 0US 1/1/2009 23US 1/1/2009 10US 1/1/2010 24France 1/1/2009 0France 1/1/2010 0France 1/1/2011 13Result Should Be:US 1/1/2009 33France 1/1/2011 13I am having difficulty getting this correct so any help would be appreciate.
vijayisonly
Master Smack Fu Yak Hacker
1836 Posts
Posted - 2009-12-28 : 11:19:13
Try this
select * from (select row_number() over (partition by country order by datecol) as seq,country,datecol,sum(val) as valfrom table1where val > 0group by country,datecol) twhere t.seq = 1
creieru
Starting Member
12 Posts
Posted - 2009-12-28 : 11:24:54
or try this:select A.Country, A.Date, sum(A.Value)from TabelaA Ainner join ( select Country, min(Date) as Date from TabelaA where [Value]>0 group by Country)as B on A.Country = B.Country and A.Date = B.Dategroup by A.Country, A.Date
peryan77
Starting Member
8 Posts
Posted - 2009-12-28 : 11:37:48
Thanks that did the trick. :) First time I used partition by.