我需要编写一个执行以下操作的SQL查询。我有一个表Contact
和一个表ContactTranslations
。我需要从表Contact
中选择所有联系人,并使用以下逻辑加入表ContactTranslations
:
对于每个联系人:
我的表格如下:
Table: Contact +-----------+--------+ | ContactId | Active | +-----------+--------+ | 1 | 1 | | 2 | 1 | | 3 | 1 | +-----------+--------+ Table: ContactTranslation +----------------------+-----------+-------------+----------------------+ | ContactTranslationId | ContactId | LanguageId | Title | +----------------------+-----------+-------------+----------------------+ | 1 | 1 | 1 (primary) | PrimaryLanguageTitle | | 3 | 2 | 2 | FirstTitle | +----------------------+-----------+-------------+----------------------+
联系人1有主要语言翻译,需要返回' PrimaryLanguageTitle'。
联系人2没有主要语言翻译,但它有另一个翻译,因此它应该返回' FirstTitle'。
联系人3没有翻译,所以它应该返回'没有翻译'
预期结果:
+-----------+----------------------+--------+ | ContactId | Title | Active | +-----------+----------------------+--------+ | 1 | PrimaryLanguageTitle | 1 | | 2 | FirstTitle | 1 | | 3 | No translation | 1 | +-----------+----------------------+--------+
我没有任何SQL代码,除此之外:
SELECT
Contact.ContactId AS ContactId,
IsNull(ContactTranslation.Title, 'No translation') AS Title,
Contact.Active AS Active
FROM
Contact
LEFT JOIN ContactTranslation
ON Contact.ContactId = ContactTranslation.ContactId
此代码的问题在于,如果一个联系人有很多翻译,那么它将返回一个联系人的所有翻译。它不会遵循上述逻辑。
答案 0 :(得分:1)
这将为每个联系人的翻译提供第一种语言
SELECT c.contactid,
ISNULL(title, 'No translation') title,
active
FROM contact c
LEFT OUTER JOIN (SELECT contactid,
languageid,
title
FROM (SELECT contactid,
languageid,
title,
ROW_NUMBER() OVER (PARTITION BY contactid ORDER BY MIN(languageid)) rmin
FROM contacttranslation
GROUP BY contactid,
languageid,
title
) t
WHERE rmin = 1) t
ON c.contactid = t.contactid
答案 1 :(得分:0)
改为FULL OUTER JOIN
。
SELECT
Contact.ContactId AS ContactId,
IsNull(ContactTranslation.Title, 'No translation') AS Title,
Contact.Active AS Active
FROM
Contact
FULL JOIN ContactTranslation
ON Contact.ContactId = ContactTranslation.ContactId