gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 04:56:54 25 4
gpt4 key购买 nike

我在 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

这就是我希望交叉表的样子(键上的不同计数):
               | 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 计数的列:

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

过去,我做过多个条件公式......
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 进行了明确的计数,最后在页脚中总结了它。

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

最佳答案

如果您将那些“演示”列反透视为行,它将使交叉表变得非常简单......

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]。

关于sql-server - 如何从多列(在 sql 中)创建交叉表(在 Crystal 中),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36485799/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com