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.

 All Forums
 SQL Server 2012 Forums
 Transact-SQL (2012)
 help me in query

Author  Topic 

niyaz4872
Starting Member

41 Posts

Posted - 2015-05-18 : 09:50:45
following is my table structure

id count date
01 2 20100101
04 1 20100101
09 5 20100101
09 6 20100101
09 5 20100107
01 8 20100105
04 5 20100125
01 4 20100201
09 8 20100205

following is sample results i wan't

id sumcountdate1(between 20100101 and 20100108 ) sumcountdate1(between 20100201 and 20100230 )
01 10 4
04 1 0
09 16 8

Bustaz Kool
Master Smack Fu Yak Hacker

1834 Posts

Posted - 2015-05-18 : 17:23:10
select
id,
sum(case
when [date] between '20100101' and '20100108' then [count]
else 0
end,
sum(case
when [date] between '20100201' and '20100228' then [count]
else 0
end[/code]NOTE: I am assuming that there is no February 30th in 2010.



I would rather be the man who bought the Brooklyn Bridge than the one who sold it. -Will Rogers
Go to Top of Page
   

- Advertisement -