如何从多列创建交叉表(在水晶中)(在sql中)

时间:2016-04-07 19:46:24

标签: sql-server crystal-reports crosstab

我在SQL中有5列需要转换为Crystal中的交叉表。

这就是我所拥有的:

 Key  | RELATIONSHIP    | DISABLED | LIMITED | RURAL | IMMIGRANT
-----------------------------------------------------------------
  1   | Other Dependent |   Yes    |   No    |  No   |   No
  2   | Victim/Survivor |   No     |   No    |  No   |   No
  3   | Victim/Survivor |   Yes    |   No    |  No   |   No
  4   | Child           |   No     |   No    |  No   |   No
  5   | Victim/Survivor |   No     |   No    |  No   |   No
  6   | Victim/Survivor |   No     |   No    |  No   |   No
  7   | Child           |   No     |   No    |  No   |   No
  8   | Victim/Survivor |   No     |   Yes   |  Yes  |   Yes
  9   | Child           |   No     |   Yes   |  Yes  |   Yes
  10  | Child           |   No     |   Yes   |  Yes  |   Yes

这就是我希望交叉标签看起来像(Key上的Distinct count):

               | Victim/Survivor | Child | Other Dependent | Total |
    --------------------------------------------------------------
   | DISABLED  |       1         |   0   |        1        |   2   |
    --------------------------------------------------------------
   | LIMITED   |       1         |   2   |        0        |   3   |
    --------------------------------------------------------------    
   | RURAL     |       1         |   2   |        0        |   3   |
    --------------------------------------------------------------    
   | IMMIGRANT |       1         |   2   |        0        |   3   |
    --------------------------------------------------------------    
   | TOTAL     |       4         |   6   |        1        |   11  |
    --------------------------------------------------------------

我在Crystal中使用此公式以组合4列(字段名称= {@OTHERDEMO})...

IF {TABLE.DISABLED} = "YES" THEN "DISABLED"  ELSE
IF {TABLE.LIMITED} = "YES" THEN "LIMITED" ELSE
IF {TABLE.IMMIGRANT} = "YES" THEN "IMMIGRANT" ELSE
IF {TABLE.RURAL} = "YES" THEN "RURAL"

...然后使用@OTHERDEMO作为行创建交叉表,将RELATIONSHIP作为列,并在KEY上使用不同的计数:

问题是,一旦水晶击中第一个"是"它停止计数,因此在交叉表中没有正确分类。所以我得到一个表首先计算DISABILITY并给出正确的显示,然后计算限制并提供一些信息,然后转储其他所有内容。

在过去,我做了多个条件公式...

IF {TABLE.DISABLED} = "YES" AND {TABLE.RELATIONSHIP} = "Victim/Survivor" THEN {TABLE.KEY} ELSE {@NULL} 
(the @null formula is because Crystal, notoriously, gets confused with nulls.)

...然后对Key进行了明确的计数,最后在页脚中对其进行了总结。

我确信还有另一种方法可以做到这一点。任何帮助/想法将不胜感激。

1 个答案:

答案 0 :(得分:1)

如果你忽视那些" DEMO"列成行,它将使交叉表变得非常容易......

select 
  u.[Key], 
  u.[RELATIONSHIP], 
  u.[DEMO]
from 
  Table1
  unpivot (
    [b] for [DEMO] in ([DISABLED], [LIMITED], [RURAL], [IMMIGRANT])
  ) u
where
    u.[b] = 'Yes'

SqlFiddle

或者如果您遇到SQL2000兼容级别,您可以手动取消忽略Yes值...

select [Key], [REALTIONSHIP], [DEMO] = cast('DISABLED' as varchar(20)) 
from Table1 
where [DISABLED] = 'Yes'
union
select [Key], [REALTIONSHIP], [DEMO] = cast('LIMITED' as varchar(20)) 
from Table1 
where [LIMITED] = 'Yes'
union
select [Key], [REALTIONSHIP], [DEMO] = cast('RURAL' as varchar(20)) 
from Table1 
where [RURAL] = 'Yes'
union
select [Key], [REALTIONSHIP], [DEMO] = cast('IMMIGRANT' as varchar(20)) 
from Table1 
where [IMMIGRANT] = 'Yes'

对于交叉表,使用Key列上的计数(也称为行计数),行上的[DEMO]和列上的[RELATIONSHIP]。