我有一张商店信息表,描述了哪些商店相互关联。数据可能如下所示:
| store_id | link_num | linked_store |
|???1? | 1 | 10 |
|???1? | 1 | 10 |
|???1? | 2 | 11 |
|???1? | 3 | 12 |
|???1? | 3 | 13 |
|???1? | 4 | 14 |
Run Code Online (Sandbox Code Playgroud)
我想检查是否有一家商店链接到同一个 link_num 的不同商店。是否有可能输出如下内容的查询?
| store_id | link_num | count | check |
|???1? | 1 | 2 | same |
|???1? | 2 | 1 | (null) |
|???1? | 3 | 2 | diff |
|???1? | 4 | 1 | (null) |
Run Code Online (Sandbox Code Playgroud)
任何帮助表示赞赏。谢谢
您可以count(distinct ..)为此使用:
select store_id, link_num, count(*) as count,
case
when count(distinct linked_store) = 1 and count(*) > 1 then 'same'
when count(distinct linked_store) > 1 and count(*) > 1 then 'diff'
end as "check"
from the_table
group by store_id, link_num;
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
46 次 |
| 最近记录: |