对 DB2 中的求和列应用列过滤器
Apply column filter on summed columns in DB2
我有以下 DB2 table:
COLUMN_NAME
DATA_TYPE
TYPE_NAME
COLUMN_SIZE
COLUMN_TEXT
DMPROD
-2
CHAR () FOR BIT DATA
35
产品代码
DMPTYP
-2
CHAR () FOR BIT DATA
1
期间类型
DM类型
-2
CHAR () FOR BIT DATA
6
数据类型
DMVL01
3
十进制
17
价值周期 1
DMVL02
3
十进制
17
价值周期 2
DMVL03
3
十进制
17
价值周期 3
DMVL04
3
十进制
17
价值周期 4
DMVL05
3
十进制
17
价值周期 5
DMVL06
3
十进制
17
价值周期 6
DMVL07
3
十进制
17
价值周期 7
DMVL08
3
十进制
17
价值周期 8
DMVL09
3
十进制
17
价值周期 9
DMVL10
3
十进制
17
价值周期 10
DMVL11
3
十进制
17
价值周期 11
DMVL12
3
十进制
17
价值周期 12
二年
3
十进制
4
财政年度
下面的查询将 return 每个 DMTYP 的值周期总和:
SELECT
D.DMPROD,
Sum(CASE WHEN DMTYPE = 'SLSGSV' THEN D.DMVL01 + D.DMVL02 + D.DMVL03 + D.DMVL04 + D.DMVL05 + D.DMVL06 + D.DMVL07 + D.DMVL08 + D.DMVL09 + D.DMVL10 + D.DMVL11 + D.DMVL12 END) AS COST,
Sum(CASE WHEN DMTYPE = 'RTNCST' THEN D.DMVL01 + D.DMVL02 + D.DMVL03 + D.DMVL04 + D.DMVL05 + D.DMVL06 + D.DMVL07 + D.DMVL08 + D.DMVL09 + D.DMVL10 + D.DMVL11 + D.DMVL12 END) AS RTNCST,
Sum(CASE WHEN DMTYPE = 'RTNNET' THEN D.DMVL01 + D.DMVL02 + D.DMVL03 + D.DMVL04 + D.DMVL05 + D.DMVL06 + D.DMVL07 + D.DMVL08 + D.DMVL09 + D.DMVL10 + D.DMVL11 + D.DMVL12 END) AS RTNNET,
Sum(CASE WHEN DMTYPE = 'RTNQTY' THEN D.DMVL01 + D.DMVL02 + D.DMVL03 + D.DMVL04 + D.DMVL05 + D.DMVL06 + D.DMVL07 + D.DMVL08 + D.DMVL09 + D.DMVL10 + D.DMVL11 + D.DMVL12 END) AS RTNQTY,
Sum(CASE WHEN DMTYPE = 'RTNVAL' THEN D.DMVL01 + D.DMVL02 + D.DMVL03 + D.DMVL04 + D.DMVL05 + D.DMVL06 + D.DMVL07 + D.DMVL08 + D.DMVL09 + D.DMVL10 + D.DMVL11 + D.DMVL12 END) AS RTNVAL,
Sum(CASE WHEN DMTYPE = 'SLSGSV' THEN D.DMVL01 + D.DMVL02 + D.DMVL03 + D.DMVL04 + D.DMVL05 + D.DMVL06 + D.DMVL07 + D.DMVL08 + D.DMVL09 + D.DMVL10 + D.DMVL11 + D.DMVL12 END) AS SLSGSV,
Sum(CASE WHEN DMTYPE = 'SLSLST' THEN D.DMVL01 + D.DMVL02 + D.DMVL03 + D.DMVL04 + D.DMVL05 + D.DMVL06 + D.DMVL07 + D.DMVL08 + D.DMVL09 + D.DMVL10 + D.DMVL11 + D.DMVL12 END) AS SLSLST,
Sum(CASE WHEN DMTYPE = 'SLSNIV' THEN D.DMVL01 + D.DMVL02 + D.DMVL03 + D.DMVL04 + D.DMVL05 + D.DMVL06 + D.DMVL07 + D.DMVL08 + D.DMVL09 + D.DMVL10 + D.DMVL11 + D.DMVL12 END) AS SLSNIV,
Sum(CASE WHEN DMTYPE = 'SLSNTN' THEN D.DMVL01 + D.DMVL02 + D.DMVL03 + D.DMVL04 + D.DMVL05 + D.DMVL06 + D.DMVL07 + D.DMVL08 + D.DMVL09 + D.DMVL10 + D.DMVL11 + D.DMVL12 END) AS SLSNTN,
Sum(CASE WHEN DMTYPE = 'SLSQTY' THEN D.DMVL01 + D.DMVL02 + D.DMVL03 + D.DMVL04 + D.DMVL05 + D.DMVL06 + D.DMVL07 + D.DMVL08 + D.DMVL09 + D.DMVL10 + D.DMVL11 + D.DMVL12 END) AS SLSQTY,
Sum(CASE WHEN DMTYPE = 'XXD' THEN D.DMVL01 + D.DMVL02 + D.DMVL03 + D.DMVL04 + D.DMVL05 + D.DMVL06 + D.DMVL07 + D.DMVL08 + D.DMVL09 + D.DMVL10 + D.DMVL11 + D.DMVL12 END) AS XXD
FROM
DWM D
WHERE
D.DMYEAR IN (2022)
AND D.DMPTYP = 'M'
GROUP BY
D.DMPROD
ORDER BY
1;
输出是我想要的,只是在不重写所有总和的情况下无法在此查询中轻易更改值周期。例如,假设我只想在 DMYEAR 2022 中查看价值期 1-5,我将需要更改每个案例陈述。是否有更简单的方法(可能使用子查询)来保持 SUM 语句相同但 select 最终输出仅需要所需的值周期?
您可以使用子查询解决此问题:
SELECT
D.DMPROD,
Sum(CASE WHEN DMTYPE = 'SLSGSV' THEN val END) AS COST,
Sum(CASE WHEN DMTYPE = 'RTNCST' THEN val END) AS RTNCST,
Sum(CASE WHEN DMTYPE = 'RTNNET' THEN val END) AS RTNNET,
Sum(CASE WHEN DMTYPE = 'RTNQTY' THEN val END) AS RTNQTY,
Sum(CASE WHEN DMTYPE = 'RTNVAL' THEN val END) AS RTNVAL,
Sum(CASE WHEN DMTYPE = 'SLSGSV' THEN val END) AS SLSGSV,
Sum(CASE WHEN DMTYPE = 'SLSLST' THEN val END) AS SLSLST,
Sum(CASE WHEN DMTYPE = 'SLSNIV' THEN val END) AS SLSNIV,
Sum(CASE WHEN DMTYPE = 'SLSNTN' THEN val END) AS SLSNTN,
Sum(CASE WHEN DMTYPE = 'SLSQTY' THEN val END) AS SLSQTY,
Sum(CASE WHEN DMTYPE = 'XXD' THEN val END) AS XXD
FROM (SELECT D.*,
(D.DMVL01 + D.DMVL02 + D.DMVL03 + D.DMVL04 + D.DMVL05) as val
FROM DWM D
) D
WHERE D.DMYEAR IN (2022) AND D.DMPTYP = 'M'
GROUP BY D.DMPROD
ORDER BY 1;
我试了一下我认为你想要得到的东西:
SELECT 'SLSGSV' as DMTYPE,1 as DMVL01,2 as DMVL02,3 as DMVL03,4 as DMVL04,5 as DMVL05 INTO #temptable
DECLARE @sumvariable INT
SELECT @sumvariable = SUM(D.DMVL01 + D.DMVL02 + D.DMVL03 + D.DMVL04 + D.DMVL05)
FROM
#temptable D
SELECT
CASE WHEN DMTYPE = 'SLSGSV' THEN @sumvariable END AS COST,
CASE WHEN DMTYPE = 'RTNCST' THEN @sumvariable END AS RTNCST,
CASE WHEN DMTYPE = 'RTNNET' THEN @sumvariable END AS RTNNET,
CASE WHEN DMTYPE = 'RTNQTY' THEN @sumvariable END AS RTNQTY,
CASE WHEN DMTYPE = 'RTNVAL' THEN @sumvariable END AS RTNVAL,
CASE WHEN DMTYPE = 'SLSGSV' THEN @sumvariable END AS SLSGSV,
CASE WHEN DMTYPE = 'SLSLST' THEN @sumvariable END AS SLSLST,
CASE WHEN DMTYPE = 'SLSNIV' THEN @sumvariable END AS SLSNIV,
CASE WHEN DMTYPE = 'SLSNTN' THEN @sumvariable END AS SLSNTN,
CASE WHEN DMTYPE = 'SLSQTY' THEN @sumvariable END AS SLSQTY,
CASE WHEN DMTYPE = 'XXD' THEN @sumvariable END AS XXD
FROM #temptable
我的理解是您将根据年份需要设置变量。
我有以下 DB2 table:
COLUMN_NAME | DATA_TYPE | TYPE_NAME | COLUMN_SIZE | COLUMN_TEXT |
---|---|---|---|---|
DMPROD | -2 | CHAR () FOR BIT DATA | 35 | 产品代码 |
DMPTYP | -2 | CHAR () FOR BIT DATA | 1 | 期间类型 |
DM类型 | -2 | CHAR () FOR BIT DATA | 6 | 数据类型 |
DMVL01 | 3 | 十进制 | 17 | 价值周期 1 |
DMVL02 | 3 | 十进制 | 17 | 价值周期 2 |
DMVL03 | 3 | 十进制 | 17 | 价值周期 3 |
DMVL04 | 3 | 十进制 | 17 | 价值周期 4 |
DMVL05 | 3 | 十进制 | 17 | 价值周期 5 |
DMVL06 | 3 | 十进制 | 17 | 价值周期 6 |
DMVL07 | 3 | 十进制 | 17 | 价值周期 7 |
DMVL08 | 3 | 十进制 | 17 | 价值周期 8 |
DMVL09 | 3 | 十进制 | 17 | 价值周期 9 |
DMVL10 | 3 | 十进制 | 17 | 价值周期 10 |
DMVL11 | 3 | 十进制 | 17 | 价值周期 11 |
DMVL12 | 3 | 十进制 | 17 | 价值周期 12 |
二年 | 3 | 十进制 | 4 | 财政年度 |
下面的查询将 return 每个 DMTYP 的值周期总和:
SELECT
D.DMPROD,
Sum(CASE WHEN DMTYPE = 'SLSGSV' THEN D.DMVL01 + D.DMVL02 + D.DMVL03 + D.DMVL04 + D.DMVL05 + D.DMVL06 + D.DMVL07 + D.DMVL08 + D.DMVL09 + D.DMVL10 + D.DMVL11 + D.DMVL12 END) AS COST,
Sum(CASE WHEN DMTYPE = 'RTNCST' THEN D.DMVL01 + D.DMVL02 + D.DMVL03 + D.DMVL04 + D.DMVL05 + D.DMVL06 + D.DMVL07 + D.DMVL08 + D.DMVL09 + D.DMVL10 + D.DMVL11 + D.DMVL12 END) AS RTNCST,
Sum(CASE WHEN DMTYPE = 'RTNNET' THEN D.DMVL01 + D.DMVL02 + D.DMVL03 + D.DMVL04 + D.DMVL05 + D.DMVL06 + D.DMVL07 + D.DMVL08 + D.DMVL09 + D.DMVL10 + D.DMVL11 + D.DMVL12 END) AS RTNNET,
Sum(CASE WHEN DMTYPE = 'RTNQTY' THEN D.DMVL01 + D.DMVL02 + D.DMVL03 + D.DMVL04 + D.DMVL05 + D.DMVL06 + D.DMVL07 + D.DMVL08 + D.DMVL09 + D.DMVL10 + D.DMVL11 + D.DMVL12 END) AS RTNQTY,
Sum(CASE WHEN DMTYPE = 'RTNVAL' THEN D.DMVL01 + D.DMVL02 + D.DMVL03 + D.DMVL04 + D.DMVL05 + D.DMVL06 + D.DMVL07 + D.DMVL08 + D.DMVL09 + D.DMVL10 + D.DMVL11 + D.DMVL12 END) AS RTNVAL,
Sum(CASE WHEN DMTYPE = 'SLSGSV' THEN D.DMVL01 + D.DMVL02 + D.DMVL03 + D.DMVL04 + D.DMVL05 + D.DMVL06 + D.DMVL07 + D.DMVL08 + D.DMVL09 + D.DMVL10 + D.DMVL11 + D.DMVL12 END) AS SLSGSV,
Sum(CASE WHEN DMTYPE = 'SLSLST' THEN D.DMVL01 + D.DMVL02 + D.DMVL03 + D.DMVL04 + D.DMVL05 + D.DMVL06 + D.DMVL07 + D.DMVL08 + D.DMVL09 + D.DMVL10 + D.DMVL11 + D.DMVL12 END) AS SLSLST,
Sum(CASE WHEN DMTYPE = 'SLSNIV' THEN D.DMVL01 + D.DMVL02 + D.DMVL03 + D.DMVL04 + D.DMVL05 + D.DMVL06 + D.DMVL07 + D.DMVL08 + D.DMVL09 + D.DMVL10 + D.DMVL11 + D.DMVL12 END) AS SLSNIV,
Sum(CASE WHEN DMTYPE = 'SLSNTN' THEN D.DMVL01 + D.DMVL02 + D.DMVL03 + D.DMVL04 + D.DMVL05 + D.DMVL06 + D.DMVL07 + D.DMVL08 + D.DMVL09 + D.DMVL10 + D.DMVL11 + D.DMVL12 END) AS SLSNTN,
Sum(CASE WHEN DMTYPE = 'SLSQTY' THEN D.DMVL01 + D.DMVL02 + D.DMVL03 + D.DMVL04 + D.DMVL05 + D.DMVL06 + D.DMVL07 + D.DMVL08 + D.DMVL09 + D.DMVL10 + D.DMVL11 + D.DMVL12 END) AS SLSQTY,
Sum(CASE WHEN DMTYPE = 'XXD' THEN D.DMVL01 + D.DMVL02 + D.DMVL03 + D.DMVL04 + D.DMVL05 + D.DMVL06 + D.DMVL07 + D.DMVL08 + D.DMVL09 + D.DMVL10 + D.DMVL11 + D.DMVL12 END) AS XXD
FROM
DWM D
WHERE
D.DMYEAR IN (2022)
AND D.DMPTYP = 'M'
GROUP BY
D.DMPROD
ORDER BY
1;
输出是我想要的,只是在不重写所有总和的情况下无法在此查询中轻易更改值周期。例如,假设我只想在 DMYEAR 2022 中查看价值期 1-5,我将需要更改每个案例陈述。是否有更简单的方法(可能使用子查询)来保持 SUM 语句相同但 select 最终输出仅需要所需的值周期?
您可以使用子查询解决此问题:
SELECT
D.DMPROD,
Sum(CASE WHEN DMTYPE = 'SLSGSV' THEN val END) AS COST,
Sum(CASE WHEN DMTYPE = 'RTNCST' THEN val END) AS RTNCST,
Sum(CASE WHEN DMTYPE = 'RTNNET' THEN val END) AS RTNNET,
Sum(CASE WHEN DMTYPE = 'RTNQTY' THEN val END) AS RTNQTY,
Sum(CASE WHEN DMTYPE = 'RTNVAL' THEN val END) AS RTNVAL,
Sum(CASE WHEN DMTYPE = 'SLSGSV' THEN val END) AS SLSGSV,
Sum(CASE WHEN DMTYPE = 'SLSLST' THEN val END) AS SLSLST,
Sum(CASE WHEN DMTYPE = 'SLSNIV' THEN val END) AS SLSNIV,
Sum(CASE WHEN DMTYPE = 'SLSNTN' THEN val END) AS SLSNTN,
Sum(CASE WHEN DMTYPE = 'SLSQTY' THEN val END) AS SLSQTY,
Sum(CASE WHEN DMTYPE = 'XXD' THEN val END) AS XXD
FROM (SELECT D.*,
(D.DMVL01 + D.DMVL02 + D.DMVL03 + D.DMVL04 + D.DMVL05) as val
FROM DWM D
) D
WHERE D.DMYEAR IN (2022) AND D.DMPTYP = 'M'
GROUP BY D.DMPROD
ORDER BY 1;
我试了一下我认为你想要得到的东西:
SELECT 'SLSGSV' as DMTYPE,1 as DMVL01,2 as DMVL02,3 as DMVL03,4 as DMVL04,5 as DMVL05 INTO #temptable
DECLARE @sumvariable INT
SELECT @sumvariable = SUM(D.DMVL01 + D.DMVL02 + D.DMVL03 + D.DMVL04 + D.DMVL05)
FROM
#temptable D
SELECT
CASE WHEN DMTYPE = 'SLSGSV' THEN @sumvariable END AS COST,
CASE WHEN DMTYPE = 'RTNCST' THEN @sumvariable END AS RTNCST,
CASE WHEN DMTYPE = 'RTNNET' THEN @sumvariable END AS RTNNET,
CASE WHEN DMTYPE = 'RTNQTY' THEN @sumvariable END AS RTNQTY,
CASE WHEN DMTYPE = 'RTNVAL' THEN @sumvariable END AS RTNVAL,
CASE WHEN DMTYPE = 'SLSGSV' THEN @sumvariable END AS SLSGSV,
CASE WHEN DMTYPE = 'SLSLST' THEN @sumvariable END AS SLSLST,
CASE WHEN DMTYPE = 'SLSNIV' THEN @sumvariable END AS SLSNIV,
CASE WHEN DMTYPE = 'SLSNTN' THEN @sumvariable END AS SLSNTN,
CASE WHEN DMTYPE = 'SLSQTY' THEN @sumvariable END AS SLSQTY,
CASE WHEN DMTYPE = 'XXD' THEN @sumvariable END AS XXD
FROM #temptable
我的理解是您将根据年份需要设置变量。