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

我在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进行了明确的计数,最后在页脚中对其进行了总结.

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

最佳答案 如果您将这些“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列上的计数(aka行计数),行上的[DEMO]和列上的[RELATIONSHIP].

点赞