简体   繁体   English

唯一行的SQL语句

[英]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 | AccountID | PhoneType | 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. 我不知道如何做到这一点,因为我只有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. 我已经尝试了几个IF语句,但没有任何东西可以让我达到我想要的。

Something like this would work. 像这样的东西会起作用。 If you had more phone types, this could get long: 如果你有更多的手机类型,这可能会变长:

Edit: Based on xQbert's response, he was correct. 编辑:根据xQbert的回复,他是对的。 You need to add a group by on Account ID, otherwise you will still get multiple rows. 您需要在帐户ID上添加一个组,否则您仍将获得多行。

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. 只是建立在CodeLikeBeaker的响应上。

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. 但是,通过在每个案例和组中使用max()聚合,我们可以组合记录。

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

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

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