gpt4 book ai didi

mysql - 按多个参数分组的累积和

转载 作者:行者123 更新时间:2023-11-29 17:58:50 24 4
gpt4 key购买 nike

我需要“订单数量”的累计金额。

当我使用此代码并选择字段作为“Sales_Year”、“Sales_Billing_Date”时,“Order_Qty”正在得到我需要的结果。

使用的代码:

"Sales_Year",
"Sales_Billing_Date",
"Order_Qty" ,
SUM ("Order_Qty") OVER ( ORDER BY "Sales_Billing_Date") AS "SUM_VAL"
FROM
( SELECT
"Sales_Year",
"Sales_Billing_Date" ,
SUM("Order_Qty") AS "Order_Qty" FROM "PS_WARRANTY_ANALYTICS"."PS_PAL_DLR_SALES"
GROUP BY "Sales_Year","Sales_Billing_Date"
)

但是我选择了更多字段(添加了“Equipment_ID”),但它没有被分组为其唯一字段。如何实现包括该字段在内的累计值添加代码

"Equipment_ID"
SELECT
"Sales_Year",
"Sales_Billing_Date",
"Equipment_ID",
"Order_Qty" ,
SUM ("Order_Qty") OVER ( ORDER BY "Sales_Billing_Date") AS "SUM_VAL"
FROM
( SELECT
"Sales_Year",
"Sales_Billing_Date" ,
"Equipment_ID",
SUM("Order_Qty") AS "Order_Qty" FROM "PS_WARRANTY_ANALYTICS"."PS_PAL_DLR_SALES"
GROUP BY "Sales_Year","Sales_Billing_Date","Equipment_ID"
)

累计:

没有累积:

最佳答案

假设您的子查询返回类似这样的内容

yyyy        orderdate               productid   sumqty
----------- ----------------------- ----------- -----------
2005 2005-07-01 00:00:00.000 707 24
2005 2005-08-01 00:00:00.000 707 58
2005 2005-09-01 00:00:00.000 707 55
2005 2005-07-01 00:00:00.000 708 27
2005 2005-08-01 00:00:00.000 708 56
2005 2005-09-01 00:00:00.000 708 57
2005 2005-07-01 00:00:00.000 709 38
2005 2005-08-01 00:00:00.000 709 134
2005 2005-09-01 00:00:00.000 709 79
2005 2005-07-01 00:00:00.000 710 5
2005 2005-08-01 00:00:00.000 710 13
2005 2005-09-01 00:00:00.000 710 6
2005 2005-07-01 00:00:00.000 711 33
2005 2005-08-01 00:00:00.000 711 64
2005 2005-09-01 00:00:00.000 711 49
2005 2005-07-01 00:00:00.000 712 40
2005 2005-08-01 00:00:00.000 712 103
2005 2005-09-01 00:00:00.000 712 83
2005 2005-07-01 00:00:00.000 714 16
2005 2005-08-01 00:00:00.000 714 37
2005 2005-09-01 00:00:00.000 714 35
2005 2005-07-01 00:00:00.000 715 49
2005 2005-08-01 00:00:00.000 715 114
2005 2005-09-01 00:00:00.000 715 72
2005 2005-07-01 00:00:00.000 716 19
2005 2005-08-01 00:00:00.000 716 48
2005 2005-09-01 00:00:00.000 716 40
2005 2005-07-01 00:00:00.000 722 8
2005 2005-08-01 00:00:00.000 722 20
2005 2005-09-01 00:00:00.000 722 7
2005 2005-07-01 00:00:00.000 725 15
2005 2005-08-01 00:00:00.000 725 38
2005 2005-09-01 00:00:00.000 725 29
2005 2005-07-01 00:00:00.000 726 9
2005 2005-08-01 00:00:00.000 726 19
2005 2005-09-01 00:00:00.000 726 7
2005 2005-07-01 00:00:00.000 729 16
2005 2005-08-01 00:00:00.000 729 41
2005 2005-09-01 00:00:00.000 729 25

(39 row(s) affected)

假设 sap 的行为类似于 sqlserver

select * ,
sum(sumqty) over (partition by year(orderdate), orderdate order by productid ) cumtot
from
(
select year(orderdate) yyyy,orderdate,productid,sum(OrderQty) sumqty
from [AdventureWorks2012].[Sales].[SalesOrderHeader] soh
join [AdventureWorks2012].[Sales].[SalesOrderDetail] sod on sod.SalesOrderID = soh.SalesOrderID
where year(orderdate) = 2005 and month(orderdate) in (7,8,9) and day(orderdate) in (1,2,3)
AND PRODUCTID < 730
group by year(orderdate),orderdate,productid
) s
order by yyyy,orderdate,productid

产品

yyyy        orderdate               productid   sumqty      cumtot
----------- ----------------------- ----------- ----------- -----------
2005 2005-07-01 00:00:00.000 707 24 24
2005 2005-07-01 00:00:00.000 708 27 51
2005 2005-07-01 00:00:00.000 709 38 89
2005 2005-07-01 00:00:00.000 710 5 94
2005 2005-07-01 00:00:00.000 711 33 127
2005 2005-07-01 00:00:00.000 712 40 167
2005 2005-07-01 00:00:00.000 714 16 183
2005 2005-07-01 00:00:00.000 715 49 232
2005 2005-07-01 00:00:00.000 716 19 251
2005 2005-07-01 00:00:00.000 722 8 259
2005 2005-07-01 00:00:00.000 725 15 274
2005 2005-07-01 00:00:00.000 726 9 283
2005 2005-07-01 00:00:00.000 729 16 299
2005 2005-08-01 00:00:00.000 707 58 58
2005 2005-08-01 00:00:00.000 708 56 114
2005 2005-08-01 00:00:00.000 709 134 248
2005 2005-08-01 00:00:00.000 710 13 261
2005 2005-08-01 00:00:00.000 711 64 325
2005 2005-08-01 00:00:00.000 712 103 428
2005 2005-08-01 00:00:00.000 714 37 465
2005 2005-08-01 00:00:00.000 715 114 579
2005 2005-08-01 00:00:00.000 716 48 627
2005 2005-08-01 00:00:00.000 722 20 647
2005 2005-08-01 00:00:00.000 725 38 685
2005 2005-08-01 00:00:00.000 726 19 704
2005 2005-08-01 00:00:00.000 729 41 745
2005 2005-09-01 00:00:00.000 707 55 55
2005 2005-09-01 00:00:00.000 708 57 112
2005 2005-09-01 00:00:00.000 709 79 191
2005 2005-09-01 00:00:00.000 710 6 197
2005 2005-09-01 00:00:00.000 711 49 246
2005 2005-09-01 00:00:00.000 712 83 329
2005 2005-09-01 00:00:00.000 714 35 364
2005 2005-09-01 00:00:00.000 715 72 436
2005 2005-09-01 00:00:00.000 716 40 476
2005 2005-09-01 00:00:00.000 722 7 483
2005 2005-09-01 00:00:00.000 725 29 512
2005 2005-09-01 00:00:00.000 726 7 519
2005 2005-09-01 00:00:00.000 729 25 544

(39 row(s) affected)

关于mysql - 按多个参数分组的累积和,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48577382/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com