我有一个与此类似的位置(地址)表:
location_id int identity (primary key)
contact_id int (foreign key to contact table; one-to-many relationship from contacts to locations)
<address info>
is_primary int (1 if this is the contact's primary address, zero otherwise)
每个联系人只能有一个主要地址。由于最近的数据上传出错,此规则已被违反,我现在有多个联系人拥有多个主要地址。我需要帮助制定查询以找出哪些查询。理想情况下,我希望输出为:
contact_id count(is_primary)
我不知道该怎么做,非常感谢您的帮助。谢谢。
答案 0 :(得分:2)
SELECT contact_id, SUM(IS_PRIMARY)
FROM table_name
GROUP BY contact_id
HAVING SUM(IS_PRIMARY) > 1