如何创建检查多个表中重复项的SQL查询
我想创建一个SQL查询,在其中检查多个不同的表中是否存在重复项。我用Teradata来做这件事。我希望输出如下所示如何创建检查多个表中重复项的SQL查询,sql,teradata,Sql,Teradata,我想创建一个SQL查询,在其中检查多个不同的表中是否存在重复项。我用Teradata来做这件事。我希望输出如下所示 |Table A| |Table B| |Table C| |Table D| |Table D| 对于这些列中的每一列,我们都可以得到值Y或N (Y表示存在重复项,N表示不存在重复项) 如何创建此脚本 我已成功编写了如何检查表中重复项的代码: SELECT Customer_Id, Year_Month_Id, count(*) FROM A GROUP BY 1,2
|Table A| |Table B| |Table C| |Table D| |Table D|
对于这些列中的每一列,我们都可以得到值Y
或N
(Y表示存在重复项,N表示不存在重复项)
如何创建此脚本
我已成功编写了如何检查表中重复项的代码:
SELECT Customer_Id, Year_Month_Id, count(*)
FROM A
GROUP BY 1,2
HAVING count(*)>1)
编辑#2:
编辑:
要在单个表中查找重复项,请执行以下操作:
select
customer_id,
year_month_id,
case when cnt >1 then 'Y' else 'N' end
from
(
select
customer_id,
year_month_id,
count (*) as Cnt
from
table1
group by 1,2
) t
如果要在两个表之间查找重复项,我可能会使用union all,类似这样的:
select
customer_Id,
year_month_id
case when count(*) > 1 then 'Y' else 'N' end
from
(
select distinct
customer_id,
year_month_id
from
table1
group by 1,2
UNION ALL
select distinct
customer_id,
year_month_id
from
table2 )t
group by 1,2
哪种脚本语言?脚本语言是SQLSQL不是脚本语言。。。当您说在多个表中重复时,您是指在一个表内还是表之间?好的,那么我想创建一个SQL查询。我是说在一张桌子里。因此,输出决定了不同表中是否存在重复项。我不是在两个表之间寻找重复项。我正在tables.Np中查找重复项。我很感谢你的帮助。这是我的。但是,我只需要一行Y或N。这个值应该解释表中是否有重复项。我怎样才能解决这个问题?即使我在交叉连接之前只尝试第一部分,它仍然不起作用。我得到错误消息:语法错误。在单词tbl1和“.”之间应该有“Except”关键字或“减号”关键字。这很奇怪,因为当我删除交叉连接时,tbl1是我最后要写的东西。你知道问题出在哪里吗?不要关闭paren和别名,现在试试。
select
customer_Id,
year_month_id
case when count(*) > 1 then 'Y' else 'N' end
from
(
select distinct
customer_id,
year_month_id
from
table1
group by 1,2
UNION ALL
select distinct
customer_id,
year_month_id
from
table2 )t
group by 1,2