我想获得每个article_id
的merchant_id
的重复数量,其中zip_code
相同。请参见下面的示例:
表格
merchant_id article_id zip_code
1 4555 1000
1 4555 1003
1 4555 1000
1 3029 1000
2 7539 1005
2 7539 1005
2 7539 1002
2 1232 1006
3 5555 1000
3 5555 1001
3 5555 1001
3 5555 1001
输出表
merchant_id count_duplicate zip_code
1 2 1000
2 2 1005
3 3 1001
这是我当前正在使用的查询,但我正在努力包含邮政编码条件:
SELECT merchant_id
,duplicate_count
FROM main_table mt
JOIN(select article_id, count(*) AS duplicate_count
from main_table
group by article_id
having count(article_id) =1) mt_1
ON mt.article_id ON mt_1.article_id = mt.article_id
这似乎返回您想要的。我不确定为什么article_id
不包含在结果集中:
select merchant_id, zip_code, count(*)
from main_table
group by merchant_id, article_id, zip_code
having count(*) > 1