重复条件(SQL)

问题描述 投票:0回答:2

我想获得每个article_idmerchant_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

sql presto
2个回答
0
投票

这似乎返回您想要的。我不确定为什么article_id不包含在结果集中:

select merchant_id, zip_code, count(*)
from main_table
group by merchant_id, article_id, zip_code
having count(*) > 1
© www.soinside.com 2019 - 2024. All rights reserved.