对 SQL 中多列中的行计数求和的查询

Posted

技术标签:

【中文标题】对 SQL 中多列中的行计数求和的查询【英文标题】:a query that sum the count of a row from multiple columns in SQL 【发布时间】:2021-01-05 12:09:18 【问题描述】:

我必须编写一个查询,对多列中的行计数求和

我的代码是这样的,但是总不行:

WITH 
NextPreviousItemAndTotal
AS
(
    SELECT LAG(Datetype, 1) OVER (PARTITION BY Fname ORDER BY Datetype ASC) AS PreviousItemDate,
            LAG(Fcount,1) OVER(PARTITION BY Fname ORDER BY Datetype) AS  PreviousItemCount,
            CurrentItemDate, CurrentItemCount,
            LEAD(Datetype, 1) OVER (PARTITION BY Fname ORDER BY Datetype ASC) AS NextItemDate, 
            LEAD(Fcount,1) OVER(PARTITION BY Fname ORDER BY Datetype) AS NextItemCount,
            SUM(Fcount) OVER(PARTITION BY Fname ORDER BY Datetype ) AS Total
    FROM FoodSara_tbl 
)
SELECT PreviousItemDate, PreviousItemCount, CurrentItemDate, CurrentItemCount,CurrentItemCount,NextItemCount, Total
FROM NextPreviousItemAndTotal

输入:

|PreviousItemDate|PreviousItemCount|CurrentItemDate|CurrentItemCount|NextItemDate|NextItemCount|
 |----------------|-----------------|---------------|----------------|------------|-------------|
 |   Null         |    Null         |   2019/10/10  |      10        |2019/10/12  |  2          |
 |   2019/10/10   |     10          |2019/10/12     |    2           |2020/01/20  |  12         |
 |  2019/10/12    |  12             |2020/01/20     |   12           |Null        |  Null       |

输出:

|PreviousItemDate|PreviousItemCount|CurrentItemDate|CurrentItemCount|NextItemDate|NextItemCount|Total|
 |----------------|-----------------|---------------|----------------|------------|-------------|-----|
 |   Null         |    Null         |   2019/10/10  |      10        |2019/10/12  |  2          |12   |
 |   2019/10/10   |     10          |2019/10/12     |    2           |2020/01/20  |  12         |24   |
 |  2019/10/12    |  12             |2020/01/20     |   8            |Null        |  Null       |20   | 

【问题讨论】:

您希望该值是多少?什么逻辑定义total?您的查询还引用了许多不属于input 的列,这有点令人困惑。 是的,你是对的,我编辑了 【参考方案1】:

如下使用coalesce

Coalesce(PreviousItemCount,0) 
+ Coalesce(currentItemCount,0)
+ Coalesce(nextItemCount,0) as total

【讨论】:

【参考方案2】:

我更改了一些列的名称,但它会起作用

WITH 
NextPreviousItemAndTotal
AS
(
    SELECT LAG(Datetype, 1) OVER (PARTITION BY Fname ORDER BY Datetype ASC) AS PreviousItemDate,
            LAG(Fregion,1) OVER(PARTITION BY Fname ORDER BY Datetype) AS PreviousRegion,
            LAG(Fcount,1) OVER(PARTITION BY Fname ORDER BY Datetype) AS PreviousCount,
            Datetype, Fregion, Fname, Fcount,
            LEAD(Datetype, 1) OVER (PARTITION BY Fname ORDER BY Datetype ASC) AS NextItemDate, 
            LEAD(Fregion,1) OVER(PARTITION BY Fname ORDER BY Datetype) AS NextItemRegion,
            LEAD(Fcount,1) OVER(PARTITION BY Fname ORDER BY Datetype) AS NextCount
            
    FROM FoodSara_tbl 
)
SELECT PreviousItemDate, PreviousRegion,PreviousCount, Datetype, Fregion, Fname, Fcount, NextItemDate, NextItemRegion,NextCount, Coalesce(PreviousCount,0) + Coalesce(Fcount,0)+ Coalesce(NextCount,0)AS Total
FROM NextPreviousItemAndTotal

【讨论】:

以上是关于对 SQL 中多列中的行计数求和的查询的主要内容,如果未能解决你的问题,请参考以下文章

Groupby对python中的多列求和并计数

如何在 SQL 中对多列求和

GroupBy 多列作为键并对多列求和,如 sql?

SQL 查询返回填充了最多列的行

Laravel 查询生成器对多列求和

DART SQL 如何从 Dart 中的 SQL 查询结果中读取多行值(多列)?