在 Teradata SQL 中使用 UNION 删除重复行
Removing duplicate row using UNION in Teradata SQL
我正在使用 Teradata sql 通过 UNION 提取数据。
SEL CAST(a.dttm AS DATE), count(a.cs) FROM cin.cell a
LEFT JOIN cin.comm c ON a.cs_sk = c.cs_sk
LEFT JOIN CIN.CID d ON a.cn_cd = d.CN_CD
WHERE CAST(a.dttm AS DATE) >= CURRENT_DATE-10
GROUP BY 1
UNION
SEL CAST(a.dttm AS DATE), count(a.cs) FROM cin_ps.cell a
LEFT JOIN cin_ps.comm c ON a.cs_sk = c.cs_sk
LEFT JOIN CIN_ps.CID d ON a.cn_cd = d.CN_CD
WHERE CAST(a.dttm AS DATE) >= CURRENT_DATE-10
GROUP BY 1
但我在第一列中得到重复的行,如下所示请注意,对于任何第一组 table 或第二组 [=23],可能存在任何特定日期没有行的情况=]
当前结果:
N. PROCESSED_DTTM Count(cs)
1 4/8/2022 40
2 4/8/2022 66
3 4/9/2022 49
4 4/9/2022 71
5 4/10/2022 117
6 4/10/2022 1430
7 4/11/2022 261
8 4/11/2022 841
所需结果:
N. PROCESSED_DTTM Count(cs)
1 4/8/2022 106
2 4/9/2022 120
5 4/10/2022 1547
7 4/11/2022 1102
您没有得到重复项,您在两组中都有 non-unique 个结果。如果您要将联合封装在子查询中并且 select 不同,您将收到相同的数据集。您要做的是在 Count 列上使用 SUM 聚合数据:
SELECT PROCESSED_DTTM,
SUM([Count(cs)]) [Count(cs)]
FROM
(
SEL CAST(a.dttm AS DATE) PROCESSED_DTTM,
count(a.cs) [Count(cs)]
FROM cin.cell a
LEFT JOIN cin.comm c ON a.cs_sk = c.cs_sk
LEFT JOIN CIN.CID d ON a.cn_cd = d.CN_CD
WHERE CAST(a.dttm AS DATE) >= CURRENT_DATE-10
GROUP BY 1
UNION ALL
SEL CAST(a.dttm AS DATE), count(a.cs)
FROM cin_ps.cell a
LEFT JOIN cin_ps.comm c ON a.cs_sk = c.cs_sk
LEFT JOIN CIN_ps.CID d ON a.cn_cd = d.CN_CD
WHERE CAST(a.dttm AS DATE) >= CURRENT_DATE-10
GROUP BY 1
) AS TBL1
GROUP BY PROCESSED_DTTM
我正在使用 Teradata sql 通过 UNION 提取数据。
SEL CAST(a.dttm AS DATE), count(a.cs) FROM cin.cell a
LEFT JOIN cin.comm c ON a.cs_sk = c.cs_sk
LEFT JOIN CIN.CID d ON a.cn_cd = d.CN_CD
WHERE CAST(a.dttm AS DATE) >= CURRENT_DATE-10
GROUP BY 1
UNION
SEL CAST(a.dttm AS DATE), count(a.cs) FROM cin_ps.cell a
LEFT JOIN cin_ps.comm c ON a.cs_sk = c.cs_sk
LEFT JOIN CIN_ps.CID d ON a.cn_cd = d.CN_CD
WHERE CAST(a.dttm AS DATE) >= CURRENT_DATE-10
GROUP BY 1
但我在第一列中得到重复的行,如下所示请注意,对于任何第一组 table 或第二组 [=23],可能存在任何特定日期没有行的情况=]
当前结果:
N. PROCESSED_DTTM Count(cs)
1 4/8/2022 40
2 4/8/2022 66
3 4/9/2022 49
4 4/9/2022 71
5 4/10/2022 117
6 4/10/2022 1430
7 4/11/2022 261
8 4/11/2022 841
所需结果:
N. PROCESSED_DTTM Count(cs)
1 4/8/2022 106
2 4/9/2022 120
5 4/10/2022 1547
7 4/11/2022 1102
您没有得到重复项,您在两组中都有 non-unique 个结果。如果您要将联合封装在子查询中并且 select 不同,您将收到相同的数据集。您要做的是在 Count 列上使用 SUM 聚合数据:
SELECT PROCESSED_DTTM,
SUM([Count(cs)]) [Count(cs)]
FROM
(
SEL CAST(a.dttm AS DATE) PROCESSED_DTTM,
count(a.cs) [Count(cs)]
FROM cin.cell a
LEFT JOIN cin.comm c ON a.cs_sk = c.cs_sk
LEFT JOIN CIN.CID d ON a.cn_cd = d.CN_CD
WHERE CAST(a.dttm AS DATE) >= CURRENT_DATE-10
GROUP BY 1
UNION ALL
SEL CAST(a.dttm AS DATE), count(a.cs)
FROM cin_ps.cell a
LEFT JOIN cin_ps.comm c ON a.cs_sk = c.cs_sk
LEFT JOIN CIN_ps.CID d ON a.cn_cd = d.CN_CD
WHERE CAST(a.dttm AS DATE) >= CURRENT_DATE-10
GROUP BY 1
) AS TBL1
GROUP BY PROCESSED_DTTM