简体   繁体   中英

SQL statement for unique rows

I am attempting to extract information from a table that holds account numbers, phone numbers, and emails. However, I'm not sure what whoever designed this table was thinking. The table is set up like this.

AccountID | PhoneType | PhoneNumber

Now you might be asking where are the emails stored. They are stored under phone number. An example of this database.

AccountID | PhoneType | PhoneNumber
123456        WORK        111-1111
123456        MOBILE      111-4567
123456        EMAIL     exampleemail@email.com
222222        EMAIL      stupid@table.com

I'm not sure how to do this as I have just a rudimentary knowledge of SQL. We would ideally want things like:

AccountID | WorkPhone | MobilePhone | Email
123456       111-1111    111-4567     example@email.com
222222                                stupid@table.com

What is a good way to go about doing this? I've tried a couple IF statements but nothing that gets me to what I would want.

Something like this would work. If you had more phone types, this could get long:

Edit: Based on xQbert's response, he was correct. You need to add a group by on Account ID, otherwise you will still get multiple rows.

SELECT
    AccountId
    , MAX(CASE WHEN PhoneType = 'WorkPhone' THEN
        PhoneNumber
      ELSE
        NULL
    END) AS 'WorkPhone'
    , MAX(CASE WHEN PhoneType = 'MobilePhone' THEN
        PhoneNumber
      ELSE
        NULL
    END) AS 'MobilePhone'
    , MAX(CASE WHEN PhoneType = 'Email' THEN
        PhoneNumber
      ELSE
        NULL
    END) AS 'Email'
FROM
    MyTable
GROUP BY AccountId

Just building on CodeLikeBeaker's response.

Distinct won't do it as each record is unique when pivoted using the case. However by using a max() aggregate on each case and a group by we can combine the records.

SELECT AccountId
     , max(CASE WHEN PhoneType = 'WorkPhone' THEN PhoneNumber END) AS 'WorkPhone'
     , max(CASE WHEN PhoneType = 'MobilePhone' THEN PhoneNumber END) AS 'MobilePhone'
     , max(CASE WHEN PhoneType = 'Email' THEN PhoneNumber END) AS 'Email'
FROM MyTable
GROUP BY accountID

If phone types are limited known set, then simply Join. Something like this:

SELECT DISTINCT a.AccountId,
       wp.PhoneNumber as WorkPhone,
       mp.PhoneNumber as MobilePhone,
       em.PhoneNumber as Email
    FROM MyTable As a
    LEFT JOIN (SELECT AccountId, PhoneNumber FROM MyTable
               WHERE PhoneType = 'WORK') As wp
         ON a.AccountId = wp.AccountId
    LEFT JOIN (SELECT AccountId, PhoneNumber FROM MyTable
               WHERE PhoneType = 'MOBILE') As mp
         ON a.AccountId = mp.AccountId
    LEFT JOIN (SELECT AccountId, PhoneNumber FROM MyTable
               WHERE PhoneType = 'EMAIL') As em
         ON a.AccountId = em.AccountId

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