简体   繁体   English

如何在不增加GROUP BY的情况下计算不同的值

[英]How to COUNT different values without adding to GROUP BY

I have a data set that contains a name for every "job" record, and whether the job passed or failed. 我有一个数据集,其中包含每个“作业”记录的名称,以及该作业是否通过。 I want to show the Name, number of jobs, how many passed, and how many failed in one row. 我想在一行中显示名称,作业数量,通过的数量以及失败的数量。

I am grouping the name and using COUNT on the name to count the total number of jobs, which works fine, but I can't show how many passed and how many failed without adding them to the GROUP BY clause causing the data to separate again. 我正在对名称进行分组,并在名称上使用COUNT来计数作业总数,这很好,但是如果不将其添加到GROUP BY子句中导致数据再次分离,就无法显示已通过的数量和失败的数量。

SELECT  I.Name, Count(I.Name) As NumberOfJobs,
CASE WHEN WI.resultTypeID = 1 THEN COUNT(WI.resultTypeID) END AS [Passed],
CASE WHEN WI.resultTypeID = 2 THEN COUNT(WI.resultTypeID) END AS [Failed],
FROM DB.DBO.People AS I 
INNER JOIN DB2.dbo.Jobs AS WI  ON I.JOBID = WI.JOBID
GROUP BY I.Name, wi.resultTypeID
    +-----------+-----------+--------+--------+
    |   Name    | NumofJobs | Passed | Failed |
    +-----------+-----------+--------+--------+
    | Dale Test |         2 | 2      | NULL   |
    | Dale Test |         2 | NULL   | 2      |
    +-----------+-----------+--------+--------+

This is what happens when I add ResultTypeID to the GROUP BY, but I want this: 当我将ResultTypeID添加到GROUP BY时会发生这种情况,但是我想要这样做:

    +-----------+-----------+--------+--------+
    |   Name    | NumofJobs | Passed | Failed |
    +-----------+-----------+--------+--------+
    | Dale Test |         4 |      2 |      2 |
    +-----------+-----------+--------+--------+

Is there anyway to do this? 反正有这样做吗?

You want conditional aggregation. 您需要条件聚合。 The case expression is an argument to the aggregation function: case表达式是聚合函数的一个参数:

SELECT I.Name, Count(*) As NumberOfJobs,
       SUM(CASE WHEN WI.resultTypeID = 1 THEN 1 ELSE 0 END) AS [Passed],
       SUM(CASE WHEN WI.resultTypeID = 2 THEN 1 ELSE 0 END) AS [Failed],
FROM DB.DBO.People I INNER JOIN
     DB2.dbo.Jobs WI
     ON I.JOBID = WI.JOBID
GROUP BY I.Name;

I am guessing that wi.resultTypeID is not NULL , so I replaced the COUNT() with SUM() because I prefer SUM() in this case. 我猜wi.resultTypeID不是NULL ,所以我用SUM()替换了COUNT() ,因为在这种情况下我更喜欢SUM()

You don't need to group your query by wi.resultTypeID . 您无需按wi.resultTypeID对查询进行wi.resultTypeID

simply remove wi.resultTypeID from group by statement and put it inside aggregate function: 只需从group by语句中删除wi.resultTypeID并将其放入聚合函数中即可:

SELECT  I.Name, Count(I.Name) As NumberOfJobs,
SUM(CASE WHEN WI.resultTypeID = 1 THEN 1 ELSE 0 END) AS [Passed],
SUM(CASE WHEN WI.resultTypeID = 2 THEN 1 ELSE 0 END) AS [Failed],
FROM DB.DBO.People AS I 
INNER JOIN DB2.dbo.Jobs AS WI  ON I.JOBID = WI.JOBID
GROUP BY I.Name

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何计算同一列和同一组中的不同值 - How to count different values in a column and same group SQL:如何计算没有GROUP BY的值的总和 - SQL: How to count the sum of values without GROUP BY 如何在不使用 group by 的情况下计算()和聚合值? - How to count() and aggregate values without using group by? 如何在 PostgreSQL 中使用 CASE WHEN 而不向聚合和 GROUP BY 添加值? - How to use CASE WHEN in PostgreSQL without adding values to aggregation and GROUP BY? 如何在没有 Group By 的情况下使用 Count? - How to use Count without Group By? 如何计算列值的出现次数但没有GROUP BY(不是不同的值)mysql - How to count the number of occurrence of column values but without GROUP BY (not distinct values) mysql 如何在MS Access中编写SQL查询以通过外键对同一字段和组中的不同值进行计数 - How to write a SQL Query in MS Access to Count different values in same field and group by a Foreign Key 如何在MS-Access中计算三个不同的不同值并在ID上分组? - How do I count three different distinct values and group on an ID in MS-Access? 如何在蜂巢中没有分组的情况下实现计数(不同) - how to achieve count(distinct) without group by in hive 如何重复值,计数和分组? - How can I repeat values, count and group?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM