[英]Return all records with more than one occurance
I have a table that looks like this: 我有一个看起来像这样的表:
ID UNIT_ID MEMBER_ID CORPORATION_ID
16 1138 0 2
18 1139 0 2
47 7007 0 3
56 1672 0 7
I need two queries: one that will give me a distinct list of the corporation_ids that occur ONCE in this table, and one that returns all corporation_ids that occurs 2 or more times. 我需要两个查询:一个将为我提供此表中一次出现的corporate_id的不同列表,另一个查询将返回出现两次或更多次的所有corporate_id。
I will be passing in a sequence of corporation_ids that should be the basis of the search, basically an IN (2,3,7). 我将传递应该作为搜索基础的corporate_ids序列,基本上是一个IN(2,3,7)。
Query 1 should return 3 and 7 (both are single entry corporation_ids) Query 2 should return 2 (multiple entry corporation_id) 查询1应该返回3和7(均为单一条目corporate_id)查询2应该返回2(多个条目corporate_id)
The reason for not doing a JOIN is that the list of corporations are in one database, and the connections (this table) is in another. 不执行JOIN的原因是公司列表在一个数据库中,而连接(此表)在另一个数据库中。
Thanks for any help I can get! 感谢您的任何帮助!
Post aggregation check is useful here 汇总后检查在这里很有用
SELECT corporation_id FROM t WHERE ID IN(2,3,7)
GROUP BY corporation_id
HAVING COUNT(*)=1
SELECT corporation_id FROM t WHERE ID IN(2,3,7)
GROUP BY corporation_id
HAVING COUNT(*)>1
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.