简体   繁体   English

SQL Server查询-不需要具有相同数据的多行

[英]SQL Server query - don't want multiple rows with identical data

I have a SQL Server database that has the following three tables - this is simplified for this post. 我有一个具有以下三个表的SQL Server数据库-本文对此进行了简化。

Stakeholder table (a table that stores a persons personal data... name, address city, state, zip, etc) Stakeholder表(用于存储人员个人数据...名称,地址城市,州,邮政编码等的表)

Stakeholder_id    full_name
---------------------------------------
1                 Joe Stakeholder
2                 Eric Stakeholder

SH Inquiry table (a table that stores information about when a stakeholder contacts us) SH Inquiry表(存储有关利益相关方何时与我们联系的信息的表)

sh_inquiry_id         inquiry_link_ID
-----------------------------------------------
1                     1
2                     1
3                     2

Sh Contacts (a table that stores information about when we contact a stakeholder) Sh Contacts (存储有关我们何时与利益相关者联系的信息的表)

sh_contact_id            contact_link_id
-----------------------------------------
1                             1
2                             1
3                             2

I want to write a SQL query that shows the stakeholder information once then show all inquiries and all contacts underneath the stakeholder row? 我想编写一个显示利益相关者信息的SQL查询,然后在利益相关者行下方显示所有查询和所有联系人? is that possible with SQL? SQL有可能吗? So in this case joe stakeholder would be shown once and then there would be 4 rows next (2 inquiries and 2 contacts). 因此,在这种情况下,将显示一次joe涉众,然后接下来将有4行(2个查询和2个联系人)。 Eric stakeholder would be shown once with two rows, 1 inquiry and one contact. 埃里克(Eric)利益相关者将被显示一次,其中有两行,1个询问和1个联系人。

Thanks for any assistance in advance. 感谢您的任何提前帮助。

As has already been mentioned, you probably want to handle this in your application code. 如前所述,您可能希望在应用程序代码中处理此问题。 However, you can use a UNION query to sort of do what you want. 但是,您可以使用UNION查询来执行所需的操作。

With the query below, I changed your latter 2 tables to SH_Inquiry and SH_Contacts (replaced spaces with underscores), which is generally a good habit (it's a bad idea to have spaces in your object names). 在下面的查询中,我将后两个表更改为SH_InquirySH_Contacts (带下划线的空格),这通常是一个好习惯(在对象名称中留有空格是个坏主意)。 Also, depending on how your tables are laid out, you might want to merge your Contacts and Inquiry tables (eg have one table, with a contact_type field that identifies it as "inbound" or "outbound"). 另外,根据表的布局方式,您可能希望合并ContactsInquiry表(例如,有一个表,以及一个contact_type字段,将其标识为“入站”或“出站”)。

Anyways, using a CTE and union: 无论如何,使用CTE和联合:

WITH Unionized AS
    (
        SELECT 
            stakeholder_id, 
            full_name,
            NULL AS contact_or_inquiry,
            NULL AS contact_or_inquiry_id
        FROM Stakeholder
        UNION ALL
        SELECT
            inquiry_link_id AS stakeholder_id,
            NULL AS full_name,
            'inquiry' AS contact_or_inquiry,
            sh_inquiry_id AS contact_or_inquiry_id
        FROM SH_Inquiry
        UNION ALL
        SELECT
            contact_link_id AS stakeholder_id,
            NULL AS full_name,
            'contact' AS contact,
            sh_contact_id AS contact_or_inquiry_id
        FROM SH_Contacts
    )
SELECT
    full_name,
    contact_or_inquiry,
    contact_or_inquiry_id
FROM Unionized
ORDER BY 
    stakeholder_id, 
    contact_or_inquiry, 
    contact_or_inquiry_id

giving you these results: 给您这些结果:

+------------------+--------------------+-----------------------+
|    full_name     | contact_or_inquiry | contact_or_inquiry_id |
+------------------+--------------------+-----------------------+
| Joe Stakeholder  | NULL               | NULL                  |
| NULL             | contact            | 1                     |
| NULL             | contact            | 2                     |
| NULL             | inquiry            | 2                     |
| Eric Stakeholder | NULL               | NULL                  |
| NULL             | contact            | 3                     |
| NULL             | inquiry            | 1                     |
| NULL             | inquiry            | 3                     |
+------------------+--------------------+-----------------------+

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM