简体   繁体   中英

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.

Stakeholder table (a table that stores a persons personal data... name, address city, state, zip, etc)

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_id         inquiry_link_ID
-----------------------------------------------
1                     1
2                     1
3                     2

Sh Contacts (a table that stores information about when we contact a stakeholder)

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? is that possible with SQL? So in this case joe stakeholder would be shown once and then there would be 4 rows next (2 inquiries and 2 contacts). Eric stakeholder would be shown once with two rows, 1 inquiry and one contact.

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.

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). 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").

Anyways, using a CTE and union:

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                     |
+------------------+--------------------+-----------------------+

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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