gpt4 book ai didi

mysql - 值内的 SQL 求和

转载 作者:可可西里 更新时间:2023-11-01 08:38:52 26 4
gpt4 key购买 nike

我有这样的结果,但我想总结一下值里面的一些值。对不起我的英语不好。请参阅图片以便更好地理解。

Before sum up

预期结果:

Expected Result

我的sql查询:

     SELECT
itmnocate.Source as Local,
itmnocate.GradeCategory,
sum(sales_data.QUANTITY/1000) AS UnitMT
FROM
sales_data
INNER JOIN itmnocate
ON sales_data.ITEM = itmnocate.ItemNumber
WHERE
sales_data.unit = 'KG'
AND
sales_data.CUSTOMERACCOUNT not in ('CT1008','CT1009')
AND itmnocate.Source in ('local','by product')
GROUP BY itmnocate.GradeCategory

最佳答案

WITH your_query
AS (SELECT 'BY PRODUCT' AS local,
'BY PRODUCT' AS "GradeCategory 1",
3380.59 AS unitmt

UNION ALL
SELECT 'LOCAL' AS local,
'LOCAL OTHERS' AS "GradeCategory 1",
2754.19 AS unitmt

UNION ALL
SELECT 'LOCAL' AS local,
'SUPER 15' AS "GradeCategory 1",
19598.17 AS unitmt

UNION ALL
SELECT 'LOCAL' AS local,
'TENDER/RAMPASAN' AS "GradeCategory 1",
53.65 AS unitmt
)
select MAX(local), sub_category,SUM(unitmt) from (SELECT local,
"GradeCategory 1",
unitmt,
CASE "GradeCategory 1" WHEN 'SUPER 15' THEN 'SUPER 15' ELSE 'LOCAL
OTHERS' END
AS sub_category
FROM your_query) processd_data group by sub_category

关于mysql - 值内的 SQL 求和,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50959438/

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