SQL 每个结果多行数据透视
SQL Pivot with multiple lines per result
我正在尝试学习如何使用 Advantage Database Server 调整日期。
我有以下代码(使用 CASE),我们的月度销售数据:
SELECT YEAR(date) as MyYear
, SUM(CASE WHEN MONTH(date) = 1 THEN value END) jan_revenue
, SUM(CASE WHEN MONTH(date) = 1 THEN value - (qty*ekp/pe) END) jan_margin
, SUM(CASE WHEN MONTH(date) = 2 THEN value END) feb_revenue
, SUM(CASE WHEN MONTH(date) = 2 THEN value - (qty*ekp/pe) END) feb_margin
, SUM(CASE WHEN MONTH(date) = 3 THEN value END) mar_revenue
, SUM(CASE WHEN MONTH(date) = 3 THEN value - (qty*ekp/pe) END) mar_margin
FROM (SELECT DAB020.*
, EXTRACT(YEAR FROM date) year
, EXTRACT(MONTH FROM date) month
FROM "DAB020.ADT" DAB020
where buart = 'A' and auf_kumsta = 'J' and date >= '2021-01-01'
) mysalesdata
GROUP BY YEAR(date)
这会产生如下所示的结果:
MyYear jan_revenue jan_margin feb_revenue jan_margin_1 mar_revenue jan_margin_2
2021 23123414 12312323 4554535 545345 435345 123123
我真正想要的是收入和利润在不同的线上:
MyYear TYPE jan Feb Mar
2021 Revenue 23123414 12312323 4554535
2021 Margin 545345 435345 123123
(数字是假的,但你明白了)
我该怎么做?我确信 CASE 和分组的组合是解决方案,但我无法解决它。
(编辑:据我所知,ADS中没有主元函数)
逆透视数据以获得不同行的利润和收入。然后像你现在做的那样聚合:
SELECT YEAR(date) as MyYear, type,
SUM(CASE WHEN MONTH(date) = 1 THEN value END) as jan,
SUM(CASE WHEN MONTH(date) = 2 THEN value END) as feb,
SUM(CASE WHEN MONTH(date) = 3 THEN value END) as mar
FROM (SELECT DAB020.*
FROM (SELECT date, 'revenue' as type, value, buart, aufkumsta
FROM "DAB020.ADT" UNION ALL
SELECT date, 'margin' as type, value - (qty*ekp/pe), buart, aufkumsta
FROM "DAB020.ADT"
) DAB020
WHERE buart = 'A' AND auf_kumsta = 'J' AND
date >= '2021-01-01'
) mysalesdata
GROUP BY YEAR(date)
我正在尝试学习如何使用 Advantage Database Server 调整日期。
我有以下代码(使用 CASE),我们的月度销售数据:
SELECT YEAR(date) as MyYear
, SUM(CASE WHEN MONTH(date) = 1 THEN value END) jan_revenue
, SUM(CASE WHEN MONTH(date) = 1 THEN value - (qty*ekp/pe) END) jan_margin
, SUM(CASE WHEN MONTH(date) = 2 THEN value END) feb_revenue
, SUM(CASE WHEN MONTH(date) = 2 THEN value - (qty*ekp/pe) END) feb_margin
, SUM(CASE WHEN MONTH(date) = 3 THEN value END) mar_revenue
, SUM(CASE WHEN MONTH(date) = 3 THEN value - (qty*ekp/pe) END) mar_margin
FROM (SELECT DAB020.*
, EXTRACT(YEAR FROM date) year
, EXTRACT(MONTH FROM date) month
FROM "DAB020.ADT" DAB020
where buart = 'A' and auf_kumsta = 'J' and date >= '2021-01-01'
) mysalesdata
GROUP BY YEAR(date)
这会产生如下所示的结果:
MyYear jan_revenue jan_margin feb_revenue jan_margin_1 mar_revenue jan_margin_2
2021 23123414 12312323 4554535 545345 435345 123123
我真正想要的是收入和利润在不同的线上:
MyYear TYPE jan Feb Mar
2021 Revenue 23123414 12312323 4554535
2021 Margin 545345 435345 123123
(数字是假的,但你明白了)
我该怎么做?我确信 CASE 和分组的组合是解决方案,但我无法解决它。
(编辑:据我所知,ADS中没有主元函数)
逆透视数据以获得不同行的利润和收入。然后像你现在做的那样聚合:
SELECT YEAR(date) as MyYear, type,
SUM(CASE WHEN MONTH(date) = 1 THEN value END) as jan,
SUM(CASE WHEN MONTH(date) = 2 THEN value END) as feb,
SUM(CASE WHEN MONTH(date) = 3 THEN value END) as mar
FROM (SELECT DAB020.*
FROM (SELECT date, 'revenue' as type, value, buart, aufkumsta
FROM "DAB020.ADT" UNION ALL
SELECT date, 'margin' as type, value - (qty*ekp/pe), buart, aufkumsta
FROM "DAB020.ADT"
) DAB020
WHERE buart = 'A' AND auf_kumsta = 'J' AND
date >= '2021-01-01'
) mysalesdata
GROUP BY YEAR(date)