0
WITH MYTABLE 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 T1.Executive, SUM([1-3]) AS [1-3],SUM([4-6]) AS [4-6],SUM([7-10]) AS [7-10],SUM([11-15]) AS [11-15],SUM([16+]) AS [16+]
  from MYTABLE T1
  GROUP BY T1.Executive

i got output below

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

 kushali     1    2    0     1      2
 nirupama    0    1    1     3      1

但我需要下面的输出-这里我想要项目列以及我们如何将项目拆分为一列

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

kushali    swgruha,runi,nanuapa       1    2    0     1      2
nirupama   swgruha,runi,nanuapa       1    2    0     1      2
4

1 回答 1

0

从您的示例中,您似乎在询问如何以这种形式汇总一系列记录...

 kushali      swgruha    1
 kushali      runi       1
 kushali      nanuapa    2

...到这种形式:

kushali      swgruha,runi,nanuapa      4

...这样您就可以对第一列进行 GROUP,将第二列聚合为 CSV(逗号分隔变量)字符串,然后对第三列执行 SUM。

这可以做到,并且有多种方法。

在不知道哪个表包含您要聚合成 CSV 字符串的字符串的情况下,我无法给您准确的 SQL 语句。但是您可以在此处找到分步示例。(免责声明:这是我的博客。)您可以使用 CTE 和一些 XML 函数来实现。

于 2013-04-23T18:15:59.770 回答