我试图让一个 SQL Server Pivot 表工作,它允许我计算然后总结一些列(总共 6 个)。数据透视表的目的是汇总任意数量的生产站点的在线问卷调查结果。有 6 个问题可以有 3 个结果值 - Target、Action 和 Fail。我要做的是计算每个问题的目标、操作和失败的数量,然后对每个问题进行总结。因此,例如,生产站点 A 可能有 2 个目标、2 个操作和 2 个失败。
我的理解是 SQL Server Pivot 表可以传递这些信息,然后可以在 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