編輯#2:
SELECT
*
FROM
(SELECT
CASE WHEN MAX(cnt) >1 THEN 'Y' ELSE 'N' END AS [Table1]
FROM
(
SELECT
customer_id,
year_month_id,
COUNT (*) AS Cnt
FROM
table1
GROUP BY 1,2
) tbl1
) t1
CROSS JOIN
(SELECT
CASE WHEN MAX(cnt) >1 THEN 'Y' ELSE 'N' END AS [Table2]
FROM
(
SELECT
customer_id,
year_month_id,
COUNT (*) AS Cnt
FROM
table2
GROUP BY 1,2
) tbl2
) t2
編輯: 用於查找單個表內的重複項:
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
其中的腳本語言? – jarlh 2015-03-13 12:31:03
腳本lanuage是SQL – 2015-03-13 12:33:38
SQL不是腳本語言......當你說在多個表中重複時,你的意思是在一個表內還是在表之間? – jarlh 2015-03-13 12:43:22