4
select Executive, count([Cat A]) AS [1-3], count([Cat B]) AS [4-6],count([Cat C]) AS [7-10],count([Cat D]) AS [11-15],count([Cat E]) AS [16+]
      from (
            select 
                  Executive,
                case  when [Next Follow Up Date] < GETDATE() and (DATEDIFF(d,[next follow up date],getdate()) > 0 and DATEDIFF(d,[next follow up date],getdate()) < 4)
                              then 'A'
                  end as [Cat A],
                  case  when [Next Follow Up Date] < GETDATE() and (DATEDIFF(d,[next follow up date],getdate()) > 3 and DATEDIFF(d,[next follow up date],getdate()) < 7)
                              then 'B'
                  end as [Cat B],
                  case when [Next Follow Up Date] < GETDATE() and (DATEDIFF(d,[next follow up date],getdate()) > 6 and DATEDIFF(d,[next follow up date],getdate()) < 11)
                              then 'C'
                  end as [Cat C],
                  case  when [Next Follow Up Date] < GETDATE() and (DATEDIFF(d,[next follow up date],getdate()) > 10 and DATEDIFF(d,[next follow up date],getdate()) < 16)
                              then 'D'
                  end as [Cat D],
                  case  when [Next Follow Up Date] < GETDATE() and (DATEDIFF(d,[next follow up date],getdate()) > 15)
                              then 'E'
                  end as [Cat E]
                  from vw_FollowUps)
            as Table1
      group by Executive, [Cat A], [Cat B], [Cat C], [Cat D], [Cat E]

输出如下

Executive  1-3  4-6  7-10  11-15  16+

Rani       0    0     0     0     0
Rani       0    1     0     2     0
Rani       0    0     1     0     0 

但我需要像下面这样的输出

Executive  1-3  4-6  7-10  11-15  16+

Rani        0    1     1     2     0

4

4 回答 4

2

额外的 SELECT (其他两个答案)是无关的。

简单的 GROUP BY只有Executive

select Executive, count([Cat A]) AS [1-3], count([Cat B]) AS [4-6],count([Cat C]) AS [7-10],count([Cat D]) AS [11-15],count([Cat E]) AS [16+]
      from (
            select 
                  Executive,
                case  when [Next Follow Up Date] < GETDATE() and (DATEDIFF(d,[next follow up date],getdate()) > 0 and DATEDIFF(d,[next follow up date],getdate()) < 4)
                              then 'A'
                  end as [Cat A],
                  case  when [Next Follow Up Date] < GETDATE() and (DATEDIFF(d,[next follow up date],getdate()) > 3 and DATEDIFF(d,[next follow up date],getdate()) < 7)
                              then 'B'
                  end as [Cat B],
                  case when [Next Follow Up Date] < GETDATE() and (DATEDIFF(d,[next follow up date],getdate()) > 6 and DATEDIFF(d,[next follow up date],getdate()) < 11)
                              then 'C'
                  end as [Cat C],
                  case  when [Next Follow Up Date] < GETDATE() and (DATEDIFF(d,[next follow up date],getdate()) > 10 and DATEDIFF(d,[next follow up date],getdate()) < 16)
                              then 'D'
                  end as [Cat D],
                  case  when [Next Follow Up Date] < GETDATE() and (DATEDIFF(d,[next follow up date],getdate()) > 15)
                              then 'E'
                  end as [Cat E]
                  from vw_FollowUps)
            as Table1
      group by Executive;
于 2013-04-23T04:18:34.300 回答
2

在整个事物周围添加一个选择:

SELECT Executive  SUM([1-3]), SUM([4-6]), SUM([7-10]), SUM([11-15]), SUM([16+])
FROM
(

   ....YOUR BIG SELECT....
)
GROUP BY Executive

所以像这样:

SELECT Executive  SUM([1-3]), SUM([4-6]), SUM([7-10]), SUM([11-15]), SUM([16+])
FROM
(

    select Executive, count([Cat A]) AS [1-3], count([Cat B]) AS [4-6],count([Cat C]) AS [7-10],count([Cat D]) AS [11-15],count([Cat E]) AS [16+]
      from (
            select 
                  Executive,
                case  when [Next Follow Up Date] < GETDATE() and (DATEDIFF(d,[next follow up date],getdate()) > 0 and DATEDIFF(d,[next follow up date],getdate()) < 4)
                              then 'A'
                  end as [Cat A],
                  case  when [Next Follow Up Date] < GETDATE() and (DATEDIFF(d,[next follow up date],getdate()) > 3 and DATEDIFF(d,[next follow up date],getdate()) < 7)
                              then 'B'
                  end as [Cat B],
                  case when [Next Follow Up Date] < GETDATE() and (DATEDIFF(d,[next follow up date],getdate()) > 6 and DATEDIFF(d,[next follow up date],getdate()) < 11)
                              then 'C'
                  end as [Cat C],
                  case  when [Next Follow Up Date] < GETDATE() and (DATEDIFF(d,[next follow up date],getdate()) > 10 and DATEDIFF(d,[next follow up date],getdate()) < 16)
                              then 'D'
                  end as [Cat D],
                  case  when [Next Follow Up Date] < GETDATE() and (DATEDIFF(d,[next follow up date],getdate()) > 15)
                              then 'E'
                  end as [Cat E]
                  from vw_FollowUps)
            as Table1
      group by Executive, [Cat A], [Cat B], [Cat C], [Cat D], [Cat E]

)
GROUP BY Executive
于 2013-04-23T04:14:42.743 回答
1

您可以使用Common Table Expression(CTE)获得上述结果...您可以使用 CTE 进行新查询,如下所示:(未经测试,但实施方式是正确的)

WITH CTETABLE AS 
(
select Executive, count([Cat A]) AS [1-3], count([Cat B]) AS [4-6],count([Cat C]) AS [7-10],count([Cat D]) AS [11-15],count([Cat E]) AS [16+]
      from (
            select 
                  Executive,
                case  when [Next Follow Up Date] < GETDATE() and (DATEDIFF(d,[next follow up date],getdate()) > 0 and DATEDIFF(d,[next follow up date],getdate()) < 4)
                              then 'A'
                  end as [Cat A],
                  case  when [Next Follow Up Date] < GETDATE() and (DATEDIFF(d,[next follow up date],getdate()) > 3 and DATEDIFF(d,[next follow up date],getdate()) < 7)
                              then 'B'
                  end as [Cat B],
                  case when [Next Follow Up Date] < GETDATE() and (DATEDIFF(d,[next follow up date],getdate()) > 6 and DATEDIFF(d,[next follow up date],getdate()) < 11)
                              then 'C'
                  end as [Cat C],
                  case  when [Next Follow Up Date] < GETDATE() and (DATEDIFF(d,[next follow up date],getdate()) > 10 and DATEDIFF(d,[next follow up date],getdate()) < 16)
                              then 'D'
                  end as [Cat D],
                  case  when [Next Follow Up Date] < GETDATE() and (DATEDIFF(d,[next follow up date],getdate()) > 15)
                              then 'E'
                  end as [Cat E]
                  from vw_FollowUps)
            as Table1
      group by Executive, [Cat A], [Cat B], [Cat C], [Cat D], [Cat E]
)
SELECT Executive, MAX([1-3]),MAX([4-6]),MAX([7-10]),MAX([11-15],MAX([16+]) FROM CTETABLE GROUP BY Executive;
于 2013-04-23T04:21:04.003 回答
0
select  Executive, SUM([1-3]), SUM([4-6]), SUM([7-10]), SUM([11-15]), SUM([16+]) FROM
(
select Executive, count([Cat A]) AS [1-3], count([Cat B]) AS [4-6],count([Cat C]) AS [7-10],count([Cat D]) AS [11-15],count([Cat E]) AS [16+]
      from (
            select 
                  Executive,
                case  when [Next Follow Up Date] < GETDATE() and (DATEDIFF(d,[next follow up date],getdate()) > 0 and DATEDIFF(d,[next follow up date],getdate()) < 4)
                              then 'A'
                  end as [Cat A],
                  case  when [Next Follow Up Date] < GETDATE() and (DATEDIFF(d,[next follow up date],getdate()) > 3 and DATEDIFF(d,[next follow up date],getdate()) < 7)
                              then 'B'
                  end as [Cat B],
                  case when [Next Follow Up Date] < GETDATE() and (DATEDIFF(d,[next follow up date],getdate()) > 6 and DATEDIFF(d,[next follow up date],getdate()) < 11)
                              then 'C'
                  end as [Cat C],
                  case  when [Next Follow Up Date] < GETDATE() and (DATEDIFF(d,[next follow up date],getdate()) > 10 and DATEDIFF(d,[next follow up date],getdate()) < 16)
                              then 'D'
                  end as [Cat D],
                  case  when [Next Follow Up Date] < GETDATE() and (DATEDIFF(d,[next follow up date],getdate()) > 15)
                              then 'E'
                  end as [Cat E]
                  from vw_FollowUps)
            as Table1
      group by Executive, [Cat A], [Cat B], [Cat C], [Cat D], [Cat E] ) AS T
group by Executive
于 2013-04-23T04:16:05.840 回答