You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

按要求处理数据:一行展示合计值与InvoiceId全额合计值

数据处理解决方案:分组聚合与发票全额合计

原始数据表(Table X)

AccountIdInvoiceIdSeasonIdAmountSold
1I1S15
1I1S15
1I1S26
1I2S18

处理需求

  • 筛选AccountId = 1的数据
  • 按SeasonId分组,对同一InvoiceId+SeasonId组合的AmountSold求和
  • 计算每个InvoiceId对应的全额合计值TotalInvoice,并与分组结果同行展示

目标输出结果

AccountIdInvoiceIdSeasonIdAmountSoldTotalInvoice
1I1S11016
1I1S2616
1I2S188

实现代码(SQL)

WITH grouped_data AS (
    SELECT 
        AccountId,
        InvoiceId,
        SeasonId,
        SUM(AmountSold) AS AmountSold
    FROM TableX
    WHERE AccountId = 1
    GROUP BY AccountId, InvoiceId, SeasonId
)
SELECT 
    gd.AccountId,
    gd.InvoiceId,
    gd.SeasonId,
    gd.AmountSold,
    SUM(gd.AmountSold) OVER (PARTITION BY gd.InvoiceId) AS TotalInvoice
FROM grouped_data gd
ORDER BY gd.InvoiceId, gd.SeasonId;

代码说明

  1. 先用CTEgrouped_data完成筛选和分组聚合:筛选出AccountId=1的数据,按AccountId、InvoiceId、SeasonId分组,对每组的AmountSold求和,得到各季的销售合计。
  2. 主查询中使用窗口函数SUM() OVER (PARTITION BY InvoiceId),对每个InvoiceId下的所有分组求和,得到该发票的全额合计TotalInvoice,并与分组结果关联展示。

内容的提问来源于stack exchange,提问作者Spoelle

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.23 12:10:22