gpt4 book ai didi

sql-server - 带有计数和总和的 SQL Server 数据透视表

转载 作者:行者123 更新时间:2023-12-02 12:34:26 25 4
gpt4 key购买 nike

我正在尝试让 SQL Server 数据透视表正常工作,它允许我对许多列(总共 6 列)进行计数然后求和。数据透视表的目的是汇总任意数量生产站点的在线调查问卷结果。有 6 个问题,可以有 3 个结果值 - 目标、行动和失败。我想做的是计算每个问题的目标、行动和失败的数量,然后对每个问题进行总结。因此,例如,生产站点 A 可能有 2 个目标、2 个操作和 2 个失败。

我的理解是,SQL Server 数据透视表可以提供此信息,然后可以在 ASP.Net ReportViewer 中显示这些信息。下面是我的代码,但它不起作用,需要一些专家的帮助:

     SELECT PRODUCTION_Site,
[Target],
[Action],
[Fail]
FROM
(SELECT Production_Site,
SUM(Coding),
SUM(Measurable),
SUM(Appearance),
SUM(Aroma),
SUM(Flavour),
SUM(Texture)
FROM t_Pqe_Grocery
GROUP BY Production_Site) AS T
PIVOT
(
COUNT(Coding) FOR Grocery_Packaging_And_Coding IN ([Target],[Action],[Fail])
COUNT(Measurable) FOR Grocery_Measurable IN ([Target],[Action],[Fail])
COUNT(Appearance) FOR Grocery_Appearance IN ([Target],[Action],[Fail])
COUNT(Aroma) FOR Grocery_Aroma IN ([Target],[Action],[Fail])
COUNT(Flavour) FOR Grocery_Flavour IN ([Target],[Action],[Fail])
COUNT(Texture) FOR Grocery_Texture IN ([Target],[Action],[Fail])) AS P

有办法解决这个问题,还是数据透视表不是解决方案?

表是

Production_Site,
Grocery_Packaging_And_Coding,
Grocery_Measurable,
Grocery_Appearance,
Grocery_Aroma,
Grocery_Flavour,
Grocery_Texture

表格中的数据如下:

Site A, Target, Action, Fail, Target, Target, Target
Site B, Target, Action, Fail, Target, Target, Target
Site C, Target, Target, Target, Target, Target, Target
Site A, Target, Target, Target, Target, Target, Target

我正在寻找的结果是

Production_Site | Target | Action | Fail
Site A 10 1 1
Site B 4 1 1
Site C 6 0 0

最佳答案

执行此查询的一种更简单的方法是应用 UNPIVOTPIVOT 函数:

select *
from
(
select Production_Site, value
from t_Pqe_Grocery
unpivot
(
value
for col in (Grocery_Packaging_And_Coding, Grocery_Measurable,
Grocery_Appearance, Grocery_Aroma,
Grocery_Flavour, Grocery_Texture)
) unp
) src
pivot
(
count(value)
for value in ([Target], [Action], [Fail])
) piv

参见SQL Fiddle with Demo

UNPIVOT 获取您的列列表并将其转换为多行,这使得计数变得更加容易:

select Production_Site, value 
from t_Pqe_Grocery
unpivot
(
value
for col in (Grocery_Packaging_And_Coding, Grocery_Measurable,
Grocery_Appearance, Grocery_Aroma,
Grocery_Flavour, Grocery_Texture)
) unp

逆透视结果:

| PRODUCTION_SITE |  VALUE |
----------------------------
| Site A | Target |
| Site A | Action |
| Site A | Fail |
| Site A | Target |
| Site A | Target |
| Site A | Target |
| Site B | Target |
| Site B | Action |
| Site B | Fail |
| Site B | Target |
| Site B | Target |
| Site B | Target |
| Site C | Target |
| Site C | Target |
| Site C | Target |
| Site C | Target |
| Site C | Target |
| Site C | Target |
| Site A | Target |
| Site A | Target |
| Site A | Target |
| Site A | Target |
| Site A | Target |
| Site A | Target |

然后对其应用 PIVOT 将获得您想要的每个 PRODUCTION_SITE 的计数。添加PIVOT后,结果是:

| PRODUCTION_SITE | TARGET | ACTION | FAIL |
--------------------------------------------
| Site A | 10 | 1 | 1 |
| Site B | 4 | 1 | 1 |
| Site C | 6 | 0 | 0 |

关于sql-server - 带有计数和总和的 SQL Server 数据透视表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13415540/

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