【问题标题】:How to pivot table to create binary data?如何透视表以创建二进制数据?
【发布时间】:2019-03-08 17:02:13
【问题描述】:

我对 sql 还很陌生,不知道如何透视表,这会导致分类数据列中的二进制数据。

这是我当前的表:

+---------+------------------+--------------------+----------------+
| User ID | Cell Phone Brand | Purchased Platform | Recorded Usage |
+---------+------------------+--------------------+----------------+
|    1001 | Apple            | Retail             |              4 |
|    1001 | Samsung          | Online             |              4 |
|    1002 | Samsung          | Retail             |              5 |
|    1003 | Google           | Online             |              3 |
|    1003 | LG               | Online             |              3 |
|    1004 | LG               | Online             |              6 |
|    1005 | Apple            | Online             |              3 |
|    1006 | Google           | Retail             |              5 |
|    1007 | Goohle           | Online             |              3 |
|    1008 | Samsung          | Retail             |              4 |
|    1009 | LG               | Retail             |              4 |
|    1009 | Apple            | Retail             |              3 |
|    1010 | Apple            | Retail             |              6 |
+---------+------------------+--------------------+----------------+

我希望得到以下结果,其中包含聚合的 Recorded Usage 和设备的二进制数据:

+---------+--------------------+----------------+-------+---------+--------+----+
| User ID | Purchased Platform | Recorded Usage | Apple | Samsung | Google | LG |
+---------+--------------------+----------------+-------+---------+--------+----+
|    1001 | Retail             |              4 |     1 |       0 |      0 |  0 |
|    1001 | Online             |              4 |     0 |       1 |      0 |  0 |
|    1002 | Retail             |              5 |     0 |       1 |      0 |  0 |
|    1003 | Online             |              3 |     0 |       0 |      1 |  0 |
|    1003 | Online             |              3 |     0 |       0 |      0 |  1 |
|    1004 | Online             |              6 |     0 |       0 |      0 |  1 |
|    1005 | Online             |              3 |     1 |       0 |      0 |  0 |
|    1006 | Retail             |              5 |     0 |       0 |      1 |  0 |
|    1007 | Online             |              3 |     0 |       0 |      1 |  0 |
|    1008 | Retail             |              4 |     0 |       1 |      0 |  0 |
|    1009 | Retail             |              4 |     0 |       0 |      0 |  1 |
|    1009 | Retail             |              3 |     1 |       0 |      0 |  0 |
|    1010 | Retail             |              6 |     1 |       0 |      0 |  0 |
+---------+--------------------+----------------+-------+---------+--------+----+

【问题讨论】:

  • 为什么用户1003在预期结果中有2行在线,GoogleLG分别标记为1?在这样的设计中,我希望它们都是1 的单行。
  • 你说得对,对不起,我编造了结果表,但那我该如何证明Purchase Platform 的合理性呢?

标签: sql sql-server tsql pivot


【解决方案1】:

您可以使用case when 语句:

declare @tmp table (UserID int, CellPhoneBrand varchar(10),    PurchasedPlatform varchar(10), RecordedUsage int)

insert into @tmp
values
 (1001,'Apple'  ,'Retail', 4)
,(1001,'Samsung','Online', 4)
,(1002,'Samsung','Retail', 5)
,(1003,'Google' ,'Online', 3)
,(1003,'LG'     ,'Online', 3)
,(1004,'LG'     ,'Online', 6)
,(1005,'Apple'  ,'Online', 3)
,(1006,'Google' ,'Retail', 5)
,(1007,'Goohle' ,'Online', 3)
,(1008,'Samsung','Retail', 4)
,(1009,'LG'     ,'Retail', 4)
,(1009,'Apple'  ,'Retail', 3)
,(1010,'Apple'  ,'Retail', 6)

select UserID, PurchasedPlatform, RecordedUsage
,case when CellPhoneBrand ='Apple' then 1 else 0 end as Apple
,case when CellPhoneBrand ='Samsung' then 1 else 0 end as Samsung
,case when CellPhoneBrand ='Google' then 1 else 0 end as Google
,case when CellPhoneBrand ='LG' then 1 else 0 end as LG


from @tmp

结果:

【讨论】:

    【解决方案2】:

    这会为您提供您在预期结果中所追求的结果。就像我在评论中提到的那样,我更可能期望这里有一个汇总的枢轴:

    WITH VTE AS(
        SELECT *
        FROM (VALUES(1001,'Apple  ','Retail',4),
                    (1001,'Samsung','Online',4),
                    (1002,'Samsung','Retail',5),
                    (1003,'Google ','Online',3),
                    (1003,'LG     ','Online',3),
                    (1004,'LG     ','Online',6),
                    (1005,'Apple  ','Online',3),
                    (1006,'Google ','Retail',5),
                    (1007,'Goohle ','Online',3),
                    (1008,'Samsung','Retail',4),
                    (1009,'LG     ','Retail',4),
                    (1009,'Apple  ','Retail',3),
                    (1010,'Apple  ','Retail',6)) V(ID, Brand, Platform, Usage))
    SELECT ID,
           Platform,
           Usage,
           CASE WHEN Brand = 'Apple' THEN 1 ELSE 0 END AS Apple,
           CASE WHEN Brand = 'Samsung' THEN 1 ELSE 0 END AS Samsung,
           CASE WHEN Brand = 'Google' THEN 1 ELSE 0 END AS Google,
           CASE WHEN Brand = 'LG' THEN 1 ELSE 0 END AS LG
    FROM VTE;
    

    【讨论】:

      【解决方案3】:

      因为您在描述中使用了一个词枢轴。这是一个解决方案,展示了如何使用PIVOT 语句在 sqlserver 中透视数据

      declare  @temp TABLE
      (
        [UserID] varchar(50), 
        [CellPhoneBrand] varchar(50), 
        [PurchasedPlatform] varchar(50),
        [RecordedUsage] int
      );
      
      INSERT INTO @temp
      (
        [UserID], 
        [CellPhoneBrand], 
        [PurchasedPlatform],
        [RecordedUsage]
      
      )
      VALUES
      (1001,'Apple',        'Retail',        4),
      (1001,'Samsung',      'Online',        4),
      (1002,'Samsung',      'Retail',        5),
      (1003,'Google',       'Online',        3),
      (1003,'LG',           'Online',        3),
      (1004,'LG',           'Online',        6),
      (1005,'Apple',        'Online',        3),
      (1006,'Google',       'Retail',        5),
      (1007,'Goohle',       'Online',        3),
      (1008,'Samsung',      'Retail',        4),
      (1009,'LG',           'Retail',        4),
      (1009,'Apple',        'Retail',        3),
      (1010,'Apple',        'Retail',        6)
      
      
      select *
      from 
      (
        select [UserID], [PurchasedPlatform], [RecordedUsage],[CellPhoneBrand]
        from @temp
      ) src
      pivot
      (
        count(CellPhoneBrand)
        for [CellPhoneBrand] in ([Apple], [Samsung],[Google],[LG])
      ) piv;
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2023-03-16
        • 2013-03-27
        • 1970-01-01
        • 1970-01-01
        • 2014-08-14
        相关资源
        最近更新 更多