带有计数和总和的 SQL Server 数据透视表 [英] SQL Server Pivot Table with Counts and Sums

查看:15
本文介绍了带有计数和总和的 SQL Server 数据透视表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to get an SQL Server Pivot table to work that allows me to count and then sum a number of columns (6 in total). The purpose of the pivot table is to aggregate online questionnaire results for any number of production sites. There are 6 questions which can have 3 result values - Target, Action and Fail. What I am trying to do is count up the number of Target, Action and Fail for each question, and to then sum this up for each. So, for example, Production Site A could have 2 Target, 2 Action and 2 Fail.

我的理解是 SQL Server Pivot 表可以传递这些信息,然后可以在 ASP.Net ReportViewer 中显示.以下是我的代码,但它无法正常工作,需要一些专家帮助:

My understanding is that the SQL Server Pivot table could deliver this information, which can then be display in ASP.Net ReportViewer. Below is my code, but it is not working, and could do with some expert help:

     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

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

Is there a way round this, or is Pivot table not the solution?

表是

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

表格中的数据如下所示:

Data in the table would like this:

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功能:

A much easier way to perform this query would be to apply both the UNPIVOT and then the PIVOT functions:

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 获取您的列列表并将其转换为多行,这样更容易计数:

The UNPIVOT takes your column list and transform it into multiple rows which makes it much easier to count:

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 后的结果是:

Then applying the PIVOT to this will get the count that you want for each of the PRODUCTION_SITEs. After adding the PIVOT the result is:

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

这篇关于带有计数和总和的 SQL Server 数据透视表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆