使用来自不同行的值的 CASE 表达式
CASE expressions using values from different rows
我有以下table
WITH TMP AS (
SELECT 'XX' AS AGG, 50 AS VALUE, 0 AS MONTH
UNION ALL
SELECT 'XX' AS AGG, 150 AS VALUE, 1 AS MONTH
UNION ALL
SELECT 'XX' AS AGG, 300 AS VALUE, 2 AS MONTH
UNION ALL
SELECT 'YY' AS AGG, 25 AS VALUE, 0 AS MONTH
UNION ALL
SELECT 'YY' AS AGG, 50 AS VALUE, 1 AS MONTH
UNION ALL
SELECT 'YY' AS AGG, 75 AS VALUE, 2 AS MONTH
UNION ALL
SELECT 'ZZ' AS AGG, 500 AS VALUE, 0 AS MONTH
UNION ALL
SELECT 'ZZ' AS AGG, 600 AS VALUE, 1 AS MONTH
UNION ALL
SELECT 'ZZ' AS AGG, 700 AS VALUE, 2 AS MONTH
)
-- CASE WHEN VALUE OF 'XX' > 0 THEN VALUE OF 'ZZ' ELSE 0 END
SELECT
*
FROM TMP
WHERE 1 = 1
我需要始终考虑月份来应用以下业务逻辑。
CASE WHEN VALUE OF 'XX' > 0 THEN VALUE OF 'ZZ' ELSE 0 END
此逻辑应映射到新的 table。结果应该类似于下面的 table。
我想避免加入相同的 table。这是否可以使用 window 函数或类似的东西以某种方式实现?这个想法是在同一个 sql 语句中构建多个 case when 语句。
IE1
MONTH
500
00
600
01
700
02
透视(有条件地聚合)您的数据,然后 return 根据您的表达式需要的值:
WITH TMP AS
(SELECT 'XX' AS AGG,
50 AS VALUE,
0 AS MONTH
UNION ALL
SELECT 'XX' AS AGG,
150 AS VALUE,
1 AS MONTH
UNION ALL
SELECT 'XX' AS AGG,
300 AS VALUE,
2 AS MONTH
UNION ALL
SELECT 'YY' AS AGG,
25 AS VALUE,
0 AS MONTH
UNION ALL
SELECT 'YY' AS AGG,
50 AS VALUE,
1 AS MONTH
UNION ALL
SELECT 'YY' AS AGG,
75 AS VALUE,
2 AS MONTH
UNION ALL
SELECT 'ZZ' AS AGG,
500 AS VALUE,
0 AS MONTH
UNION ALL
SELECT 'ZZ' AS AGG,
600 AS VALUE,
1 AS MONTH
UNION ALL
SELECT 'ZZ' AS AGG,
700 AS VALUE,
2 AS MONTH),
--Solution
/*WITH */Pvt AS(
SELECT Month,
MAX(CASE Agg WHEN 'XX' THEN VALUE END) AS XX,
--MAX(CASE Agg WHEN 'YY' THEN VALUE END) AS YY,--Not needed hence commented out
MAX(CASE Agg WHEN 'ZZ' THEN VALUE END) AS ZZ
FROM TMP
GROUP BY MONTH)
SELECT CASE WHEN XX > 0 THEN ZZ ELSE 0 END AS IE1,
MONTH
FROM Pvt;
我有以下table
WITH TMP AS (
SELECT 'XX' AS AGG, 50 AS VALUE, 0 AS MONTH
UNION ALL
SELECT 'XX' AS AGG, 150 AS VALUE, 1 AS MONTH
UNION ALL
SELECT 'XX' AS AGG, 300 AS VALUE, 2 AS MONTH
UNION ALL
SELECT 'YY' AS AGG, 25 AS VALUE, 0 AS MONTH
UNION ALL
SELECT 'YY' AS AGG, 50 AS VALUE, 1 AS MONTH
UNION ALL
SELECT 'YY' AS AGG, 75 AS VALUE, 2 AS MONTH
UNION ALL
SELECT 'ZZ' AS AGG, 500 AS VALUE, 0 AS MONTH
UNION ALL
SELECT 'ZZ' AS AGG, 600 AS VALUE, 1 AS MONTH
UNION ALL
SELECT 'ZZ' AS AGG, 700 AS VALUE, 2 AS MONTH
)
-- CASE WHEN VALUE OF 'XX' > 0 THEN VALUE OF 'ZZ' ELSE 0 END
SELECT
*
FROM TMP
WHERE 1 = 1
我需要始终考虑月份来应用以下业务逻辑。
CASE WHEN VALUE OF 'XX' > 0 THEN VALUE OF 'ZZ' ELSE 0 END
此逻辑应映射到新的 table。结果应该类似于下面的 table。
我想避免加入相同的 table。这是否可以使用 window 函数或类似的东西以某种方式实现?这个想法是在同一个 sql 语句中构建多个 case when 语句。
IE1 | MONTH |
---|---|
500 | 00 |
600 | 01 |
700 | 02 |
透视(有条件地聚合)您的数据,然后 return 根据您的表达式需要的值:
WITH TMP AS
(SELECT 'XX' AS AGG,
50 AS VALUE,
0 AS MONTH
UNION ALL
SELECT 'XX' AS AGG,
150 AS VALUE,
1 AS MONTH
UNION ALL
SELECT 'XX' AS AGG,
300 AS VALUE,
2 AS MONTH
UNION ALL
SELECT 'YY' AS AGG,
25 AS VALUE,
0 AS MONTH
UNION ALL
SELECT 'YY' AS AGG,
50 AS VALUE,
1 AS MONTH
UNION ALL
SELECT 'YY' AS AGG,
75 AS VALUE,
2 AS MONTH
UNION ALL
SELECT 'ZZ' AS AGG,
500 AS VALUE,
0 AS MONTH
UNION ALL
SELECT 'ZZ' AS AGG,
600 AS VALUE,
1 AS MONTH
UNION ALL
SELECT 'ZZ' AS AGG,
700 AS VALUE,
2 AS MONTH),
--Solution
/*WITH */Pvt AS(
SELECT Month,
MAX(CASE Agg WHEN 'XX' THEN VALUE END) AS XX,
--MAX(CASE Agg WHEN 'YY' THEN VALUE END) AS YY,--Not needed hence commented out
MAX(CASE Agg WHEN 'ZZ' THEN VALUE END) AS ZZ
FROM TMP
GROUP BY MONTH)
SELECT CASE WHEN XX > 0 THEN ZZ ELSE 0 END AS IE1,
MONTH
FROM Pvt;