Sql server 在报告中水平显示M和F摘要

Sql server 在报告中水平显示M和F摘要,sql-server,crystal-reports,Sql Server,Crystal Reports,我正在使用聚合查询生成基于性别的报告。我的查询工作正常,并生成以下结果: New Comers M 10 New Comers F 5 我正在使用crystal reports,并希望将上述内容显示为: M F New Comers 10 5 有什么想法吗?数据来自SQL SErver 2008,使用条件聚合: select col1 /* new comers */ , M = count(case wh

我正在使用聚合查询生成基于性别的报告。我的查询工作正常,并生成以下结果:

New Comers M 10
New Comers F 5
我正在使用crystal reports,并希望将上述内容显示为:

             M         F     
New Comers   10        5 

有什么想法吗?数据来自SQL SErver 2008,使用条件聚合:

select 
    col1 /* new comers */
  , M = count(case when gender = 'M' then 1 end)
  , F = count(case when gender = 'F' then 1 end)
from t
group by col1 
或者在找不到任何值时使用sum返回0而不是null:

select 
    col1 /* new comers */
  , M = sum(case when gender = 'M' then 1 else 0 end)
  , F = sum(case when gender = 'F' then 1 else 0 end)
from t
group by col1 

重新表述您的问题:您需要将列1中的行显示为列标题。如果我理解正确,您问题的解决方案在于使用PIVOT 正如她所描述的:

PIVOT通过旋转唯一值来旋转表值表达式 从表达式中的一列到输出中的多列, 并在需要时对任何剩余数据执行聚合 最终输出中需要的列值

有关语法和其他详细信息,请参阅