T-SQL help needed to create a result set in SQL Server 2008 -


i have display data in following format:

enter image description here

suppose select 08-2013 date display data. consumer data.so this month column should calculate data ( i.e. count or sum ) aug-2013 , from april column should calculate data ( i.e. count or sum ) apr-2013 aug-2013.

so problem how can write t-sql result set. can calculate this month data , from april data separately how merge these 2 result in single result set.

do have create table result set.

ps

ok take example of first row 1) sample text 1 represent individual calculation this month , from april

declare @bill_date datetime set @bill_date= '2013-08-15' --seprate result set month , row sample text 1 select  isnull(count(case when cat_code='1' c.consumer_no end),0) cat_1_this_month, isnull(count(case when cat_code='2' c.consumer_no end),0) cat_2_this_month, isnull(count(case when cat_code='3' c.consumer_no end),0) cat_3_this_month consumer c inner join billing b on c.consumer_no = b.consumer_no b.bill_date = @bill_date , (more condition per sample text 1) --seprate result set april , row sample text 1 select  isnull(sum(case when cat_code='1' c.unit end),0) cat_1_from_april, isnull(sum(case when cat_code='2' c.unit end),0) cat_2_from_april, isnull(sum(case when cat_code='3' c.unit end),0) cat_3_from_april consumer c inner join billing b on c.consumer_no = b.consumer_no b.bill_date between '2013-04-01' , @bill_date  , (more condition per sample text 1) 

now aim display these 2 individual result in 1 row in 2 column ( i.e. month , april )

you need condition include entire period in clause thus, can use nested case expression check month august

declare @bill_date datetime,         @sdate datetime,          @edate datetime set @bill_date = '20130815'         set @sdate = dateadd(month, datediff(month, 0, @bill_date), 0) set @edate = dateadd(month, 1, @sdate)  select count(case when b.bill_date >= @sdate , b.bill_date < @edate                    case when cat_code='1'                              c.consumer_no end end) cat_1_this_month,               sum(case when cat_code='1' c.unit end) cat_1_from_april,        count(case when b.bill_date >= @sdate , b.bill_date < @edate                    case when cat_code='2'                              c.consumer_no end end) cat_2_this_month,        sum(case when cat_code='2' c.unit end) cat_2_from_april,        count(case when b.bill_date >= @sdate , b.bill_date < @edate                    case when cat_code='3'                              c.consumer_no end end) cat_3_this_month,                      sum(case when cat_code='3' c.unit end) cat_3_from_april consumer c inner join billing b on c.consumer_no = b.consumer_no b.bill_date >= '20130401 00:00:00' , b.bill_date < @edate   , (more condition per sample text 1) 

Comments

Popular posts from this blog

How to remove text and logo OR add Overflow on Android ActionBar using AppCompat on API 8? -

html - How to style widget with post count different than without post count -

url rewriting - How to redirect a http POST with urlrewritefilter -