SQL SELECT查询 - 使用某些逻辑从其他表加入和返回字段

时间:2017-08-02 19:57:37

标签: sql sql-server

我需要编写一个执行以下操作的SQL查询。我有一个表Contact和一个表ContactTranslations。我需要从表Contact中选择所有联系人,并使用以下逻辑加入表ContactTranslations

对于每个联系人:

  1. 如果主语言有翻译(语言ID = 1),则返回该翻译的标题(Title =" PrimaryLanguageTitle")。
  2. 如果主要语言没有翻译,则返回当前联系人的第一个翻译标题(标题=" FirstTitle")。
  3. 如果没有翻译,则返回'无翻译' (标题="没有翻译")。
  4. 我的表格如下:

    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
    

    此代码的问题在于,如果一个联系人有很多翻译,那么它将返回一个联系人的所有翻译。它不会遵循上述逻辑。

2 个答案:

答案 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
相关问题