SSRS中的非连续列分组

时间:2016-12-22 14:01:18

标签: sql-server reporting-services ssrs-2012

我有一份报告,将数据分组为日期和月份,以便进行年度比较。它使用我在SO上学习的技巧,使用下面的SQL来分割当前年份和之前的年份。

select 
iif(DATEDIFF(yy,DATEADD(month, DATEDIFF(month, 0, [Start Time]), 0),GETDATE())=0,0,1) as [Row],
CONCAT(DATEPART(d,[start time]),'-',Datepart(m,[start time])) as [daymonth],


  sum(iif([Type] in ('Normal operator call'),1,0)) as [Calls Answered],
  sum(iif([Type] in ('Caller rang off','Caller rang off during divert','No suitable operator logged on'),1,0)) as [Ring Offs]

  From inboundcallsview 

   where
   ([Start Time] between '2016-12-15' and '2016-12-22')
   Or
   ([Start Time] between '2015-12-15' and '2015-12-22')

   group by 
iif(DATEDIFF(yy,DATEADD(month, DATEDIFF(month, 0, [Start Time]), 0),GETDATE())=0,0,1),

CONCAT(DATEPART(d,[start time]),'-',Datepart(m,[start time]))

当我使用此设置将其全部放入SSRS时,

Setup

我得到了这个结果,其中0是今年,1是去年

result1

这很好,但我更希望得到这个结果

resultwanted

这样人们就可以更轻松地将回答与回答和响铃进行比较。是否有一种方法可以在SQL或报表中设置,以便将列分组置于列标题下方?

2 个答案:

答案 0 :(得分:1)

LONG发布的查询对我来说不起作用(在我中止之前运行了20分钟)但它确实让我使用了我的大脑一次并指出了正确的方向。我所做的是将Row区别直接合并到每个iif语句中,而不是让它位于上面,以便SQL现在

select 

CONCAT(DATEPART(d,[start time]),'-',Datepart(m,[start time])) as [daymonth],


  sum(iif(([Type] in ('Normal operator call'))and(DATEDIFF(yy,DATEADD(month, DATEDIFF(month, 0, [Start Time]), 0),GETDATE())=0),1,0)) as [Calls Answered This Year],
    sum(iif(([Type] in ('Normal operator call'))and(DATEDIFF(yy,DATEADD(month, DATEDIFF(month, 0, [Start Time]), 0),GETDATE())<>0),1,0)) as [Calls Answered Last Year],
  sum(iif(([Type] in ('Caller rang off','Caller rang off during divert','No suitable operator logged on'))and(DATEDIFF(yy,DATEADD(month, DATEDIFF(month, 0, [Start Time]), 0),GETDATE())=0),1,0)) as [Ring Offs This Year],
  sum(iif(([Type] in ('Caller rang off','Caller rang off during divert','No suitable operator logged on'))and(DATEDIFF(yy,DATEADD(month, DATEDIFF(month, 0, [Start Time]), 0),GETDATE())<>0),1,0)) as [Ring Offs Last Year]


   From inboundcallsview 

   where
   ([Start Time] between '2016-12-15' and '2016-12-22')
   Or
   ([Start Time] between '2015-12-15' and '2015-12-22')

   group by 

CONCAT(DATEPART(d,[start time]),'-',Datepart(m,[start time]))

order by CONCAT(DATEPART(d,[start time]),'-',Datepart(m,[start time]))

我得到了我想要的输出

output

非常感谢LONG让我吸引我的大脑。

答案 1 :(得分:0)

不使用列组,只需使用表格尝试此查询。

Select A.daymonth,B.[Calls Answered],A.[Calls Answered],B.[Ring Offs],A.[Ring Offs] 

 FROM 

 (
 select 
iif(DATEDIFF(yy,DATEADD(month, DATEDIFF(month, 0, [Start Time]), 0),GETDATE())=0,0,1) as [Row],
CONCAT(DATEPART(d,[start time]),'-',Datepart(m,[start time])) as [daymonth],
sum(iif([Type] in ('Normal operator call'),1,0)) as [Calls Answered],
sum(iif([Type] in ('Caller rang off','Caller rang off during divert','No suitable operator logged on'),1,0)) as [Ring Offs]

From inboundcallsview 

where
   ([Start Time] between '2016-12-15' and '2016-12-22')
 group by iif(DATEDIFF(yy,DATEADD(month, DATEDIFF(month, 0, [Start Time]), 0),GETDATE())=0,0,1),CONCAT(DATEPART(d,[start time]),'-',Datepart(m,[start time]))
) as A

LEFT JOIN 

 (
 select
iif(DATEDIFF(yy,DATEADD(month, DATEDIFF(month, 0, [Start Time]), 0),GETDATE())=0,0,1) as [Row],
CONCAT(DATEPART(d,[start time]),'-',Datepart(m,[start time])) as [daymonth],
sum(iif([Type] in ('Normal operator call'),1,0)) as [Calls Answered],
sum(iif([Type] in ('Caller rang off','Caller rang off during divert','No suitable operator logged on'),1,0)) as [Ring Offs]

From inboundcallsview 

where
([Start Time] between '2015-12-15' and '2015-12-22')
 group by iif(DATEDIFF(yy,DATEADD(month, DATEDIFF(month, 0, [Start Time]), 0),GETDATE())=0,0,1),CONCAT(DATEPART(d,[start time]),'-',Datepart(m,[start time]))
) as B

ON B.daymonth = A.daymonth

Where A.Row ! = B.Row