我有一个customer表,它包含了多个月的客户数据。
我的任务是显示重复的客户与那里的细节,我需要从第二次发生获取行,但我的查询是抓取第一次发生也客户表
,我想要一个查询来得到这个结果。结果
SELECT * FROM (SELECT ROW_NUMBER()OVER(PARTITION BY customer_id ORDER BY Id ASC) Sno,* FROM @tbl) a WHERE a s;1
你可以试试这样
SELECT *
FROM table_name
WHERE customer_id IN (
SELECT customer_id
FROM table_name
WHERE customer_id IS NOT NULL
GROUP BY customer_id
HAVING COUNT(*) > 1
)
AND (customer_id, transaction_date) NOT IN (
SELECT customer_id, MIN(transaction_date)
FROM table_name
WHERE customer_id IS NOT NULL
GROUP BY customer_id
)