简体   繁体   English

使用SUM和CASE函数获取一行总和

[英]Using SUM and CASE Functions to get a row of sums

I want to create a output adding up all the integers associated with each variable that fits with a certain range. 我想创建一个输出,将与适合某个范围的每个变量关联的所有整数加起来。 So one column is the ranges (0-99,100-199,etc.), second column has the total number of variables, and the third column will be the summation. 因此,第一列是范围(0-99、100-199等),第二列是变量的总数,第三列是求和。

I've created the format for creating the first two columns, and for understanding purposes my goal is just to recreate the same table with the second and third column. 我已经创建了用于创建前两列的格式,并且出于理解目的,我的目标只是与第二列和第三列重新创建同一表。

The Ttable contains a list of items each assigned a value. Ttable包含一个项目列表,每个项目都分配了一个值。

SELECT Ttable.SUM AS TotalTime,
        COUNT(*) AS Number

FROM 
    (SELECT 
    SUM(CASE WHEN count BETWEEN 0 AND 99 THEN [Value] ELSE 0 END) AS SUM FROM Ttable)
    (SELECT
    SUM(CASE WHEN count BETWEEN 100 AND 199 THEN [Value] ELSE 0 END) AS SUM FROM Ttable)
    (SELECT
    SUM(CASE WHEN count BETWEEN 200 AND 300 THEN [Value] ELSE 0 END) AS SUM FROM Ttable)
    Ttable
GROUP BY Ttable.SUM
ORDER BY Ttable.SUM;

Code returns columns one and two 代码返回第一和第二列

SELECT  Ttable.RANGE AS FileRange,
        COUNT(*) AS Number
FROM (
    SELECT 
    CASE
        WHEN count BETWEEN 0 AND 99 THEN '0 - 99'
        WHEN count BETWEEN 100 AND 199 THEN '100 - 199'
        WHEN count BETWEEN 200 AND 300 THEN '200 - 299'
        ELSE '300-399' END AS RANGE FROM table)
    Ttable
GROUP BY Ttable.RANGE
ORDER BY Ttable.RANGE;

I expect the code to return columns two and three of 我希望代码返回第二列和第三列

FileRange         Number        SumValues
 0 - 99            600            8000
100 - 199          400            5600
200 - 300          200            3000

but I'm getting a "SQL command not properly ended" 但我收到“ SQL命令未正确结束”

You seem to want conditional aggregation: 您似乎想要条件聚合:

SELECT COUNT(*) AS Number
       SUM(CASE WHEN count BETWEEN 0 AND 99 THEN [Value] ELSE 0 END) AS SUM_000_0999
       SUM(CASE WHEN count BETWEEN 100 AND 199 THEN [Value] ELSE 0 END) AS SUM_100_199,
       SUM(CASE WHEN count BETWEEN 200 AND 300 THEN [Value] ELSE 0 END) AS SUM_200_300
FROM table;

Simply keep the original number, count , in the subquery SELECT , then aggregate it in outer query: 只需将原始数字count保留在子查询SELECT ,然后将其聚合在外部查询中:

SELECT  Ttable."RANGE" AS FileRange,
        COUNT(*) AS "Number",
        SUM("count") As SumValues
FROM (SELECT 
            "count",
            CASE
               WHEN "count" BETWEEN 0 AND 99 THEN '0 - 99'
               WHEN "count" BETWEEN 100 AND 199 THEN '100 - 199'
               WHEN "count" BETWEEN 200 AND 300 THEN '200 - 299'
               ELSE '300-399' 
             END AS "RANGE"
      FROM table) Ttable
GROUP BY Ttable."RANGE"
ORDER BY Ttable."RANGE";

Also, be careful of reserved words such as table , count , and range which can have unintended results in query compilation. 另外,请注意保留字,例如tablecountrange ,它们可能在查询编译中产生意想不到的结果。 Escape by double quotes or better use different identifiers or aliases. 用双引号转义或最好使用不同的标识符或别名。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM