Mat*_*att 2 sql group-by exists where
有没有更好的写作方式
Select distinct id_no
from revenue_table
where (exists (select * from revenue_table i
where revenue_type = 'Shipping' and i.id_no = r.id_no)
and exists(select * from revenue_table i
where revenue_type = 'Reproduction' and i.id_no = r.id_no)
and exists(select * from revenue_table i
where revenue_type = 'Tape' and i.id_no = r.id_no))
Run Code Online (Sandbox Code Playgroud)
id_no表示一个表单,该表单为表单上的每个收入项输入一次.相同的revenue_type可以多次出现.有许多使用OR逻辑的高级函数,但我似乎无法使用AND集理论找到任何函数.如果GROUP BY有一些功能可以将像id_no这样的组比较为(装运,复制,磁带)这样的集合,那将是非常好的.
这存在吗?
另一种选择是
SELECT id_no
FROM revenue_table
WHERE revenue_type IN ( 'Tape', 'Shipping', 'Reproduction' )
GROUP BY id_no
HAVING COUNT(DISTINCT revenue_type) = 3
Run Code Online (Sandbox Code Playgroud)
你必须测试它是否是一种改进.
| 归档时间: |
|
| 查看次数: |
1030 次 |
| 最近记录: |