我有一个查询,查询了三年(2016年,2017年和2018年)的结果,现在我需要包括第四年(2019年),我不知道该怎么做。
我尝试复制查询部分以更改年份,但出现很多错误。
SELECT MAX(qnt2016) as qnt2016, MAX(qnt2017) as qnt2017, MAX(qnt2018) as qnt2018, empresa FROM
(SELECT CASE WHEN COUNT(cliente) IS NULL THEN 0
ELSE COUNT(cliente) END AS qnt2016, 0 as qnt2017, 0 as qnt2018, empresa, rep
FROM
(SELECT E026RAM.CODRAM tipo, E026RAM.DESRAM nome, E085HCL.CODCLI cliente, E085HCL.CODREP rep, E090REP.APEREP empresa
FROM
E085HCL, E090REP, E085CLI, E026RAM, E070EMP, E007UFS
WHERE
((E085CLI.SITCLI = 'A')) AND
((E026RAM.CODRAM in (1,2,3))) AND
(((E085CLI.DATCAD >= '2000-01-01') AND (E085CLI.DATCAD <= '2016-12-31'))) AND
((E085HCL.CODEMP = 1)) AND
((E085CLI.SIGUFS = E007UFS.SIGUFS) AND
(E085HCL.CODREP = E090REP.CODREP) AND
(E085CLI.CODRAM = E026RAM.CODRAM) AND
(E085HCL.CODCLI = E085CLI.CODCLI) AND
(E085HCL.CODEMP = E070EMP.CODEMP))) C2018
where tipo not in (10, 15)
GROUP BY rep, empresa
union all
SELECT 0 as qnt2016, CASE WHEN COUNT(cliente) IS NULL THEN 0
ELSE COUNT(cliente) END AS qnt2017, 0 as qnt2018, empresa, rep
FROM
(SELECT E026RAM.CODRAM tipo, E026RAM.DESRAM nome, E085HCL.CODCLI cliente, E085HCL.CODREP rep, E090REP.APEREP empresa
FROM
E085HCL, E090REP, E085CLI, E026RAM, E070EMP, E007UFS
WHERE
((E085CLI.SITCLI = 'A')) AND
((E026RAM.CODRAM in (1,2,3))) AND
(((E085CLI.DATCAD >= '2000-01-01') AND (E085CLI.DATCAD <= '2017-12-31'))) AND
((E085HCL.CODEMP = 1)) AND
((E085CLI.SIGUFS = E007UFS.SIGUFS) AND
(E085HCL.CODREP = E090REP.CODREP) AND
(E085CLI.CODRAM = E026RAM.CODRAM) AND
(E085HCL.CODCLI = E085CLI.CODCLI) AND
(E085HCL.CODEMP = E070EMP.CODEMP))) C2018
where tipo not in (10, 15)
GROUP BY rep, empresa
union all
SELECT 0 as qnt2016, 0 as qnt2017, CASE WHEN COUNT(cliente) IS NULL THEN 0
ELSE COUNT(cliente) END AS qnt2018, empresa, rep
FROM
(SELECT
E026RAM.CODRAM tipo, E026RAM.DESRAM nome, E085HCL.CODCLI cliente, E085HCL.CODREP rep, E090REP.APEREP empresa
FROM
E085HCL, E090REP, E085CLI, E026RAM, E070EMP, E007UFS
WHERE
((E085CLI.SITCLI = 'A')) AND
((E026RAM.CODRAM in (1,2,3))) AND
(((E085CLI.DATCAD >= '2000-01-01') AND (E085CLI.DATCAD <= '2018-12-31'))) AND
((E085HCL.CODEMP = 1)) AND
((E085CLI.SIGUFS = E007UFS.SIGUFS) AND
(E085HCL.CODREP = E090REP.CODREP) AND
(E085CLI.CODRAM = E026RAM.CODRAM) AND
(E085HCL.CODCLI = E085CLI.CODCLI) AND
(E085HCL.CODEMP = E070EMP.CODEMP))) C2018
where tipo not in (10, 15)
GROUP BY rep, empresa, rep) select2
GROUP BY empresa, rep
order by rep"
答案 0 :(得分:2)
您需要在关闭子查询之前添加另一个UNION ALL
,并将新字段添加到所有SELECT
语句中
SELECT MAX(qnt2016) as qnt2016, MAX(qnt2017) as qnt2017, MAX(qnt2018) as qnt2018, MAX(qnt2019) as qnt2019, empresa FROM
(SELECT CASE WHEN COUNT(cliente) IS NULL THEN 0
ELSE COUNT(cliente) END AS qnt2016, 0 as qnt2017, 0 as qnt2018, 0 as qnt2019, empresa, rep
FROM
...
union all
SELECT 0 as qnt2016, 0 as qnt2017, , 0 as qnt2018, CASE WHEN COUNT(cliente) IS NULL THEN 0
ELSE COUNT(cliente) END AS qnt2019, empresa, rep
FROM
(SELECT
E026RAM.CODRAM tipo, E026RAM.DESRAM nome, E085HCL.CODCLI cliente, E085HCL.CODREP rep, E090REP.APEREP empresa
FROM
E085HCL, E090REP, E085CLI, E026RAM, E070EMP, E007UFS
WHERE
((E085CLI.SITCLI = 'A')) AND
((E026RAM.CODRAM in (1,2,3))) AND
(((E085CLI.DATCAD >= '2000-01-01') AND (E085CLI.DATCAD <= '2019-12-31'))) AND
((E085HCL.CODEMP = 1)) AND
((E085CLI.SIGUFS = E007UFS.SIGUFS) AND
(E085HCL.CODREP = E090REP.CODREP) AND
(E085CLI.CODRAM = E026RAM.CODRAM) AND
(E085HCL.CODCLI = E085CLI.CODCLI) AND
(E085HCL.CODEMP = E070EMP.CODEMP))) C2018
where tipo not in (10, 15)
GROUP BY rep, empresa, rep) select2
GROUP BY empresa, rep
order by rep
我回答了您需要向查询添加什么数据,但是正如注释中所述,这是进行此查询的一种糟糕方法。了解pivoting,我认为是重新处理此查询的正确方法。