Sql 如何透视表以创建二进制数据?
我对sql相当陌生,不知道如何透视表,从而从分类数据列生成二进制数据 这是我当前的表格:Sql 如何透视表以创建二进制数据?,sql,sql-server,tsql,pivot,Sql,Sql Server,Tsql,Pivot,我对sql相当陌生,不知道如何透视表,从而从分类数据列生成二进制数据 这是我当前的表格: +---------+------------------+--------------------+----------------+ | User ID | Cell Phone Brand | Purchased Platform | Recorded Usage | +---------+------------------+--------------------+----------------
+---------+------------------+--------------------+----------------+
| 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 |
+---------+------------------+--------------------+----------------+
我希望获得以下关于设备的聚合记录使用情况和二进制数据的结果:
+---------+--------------------+----------------+-------+---------+--------+----+
| 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 |
+---------+--------------------+----------------+-------+---------+--------+----+
当
语句:
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
结果:
这会让你得到你期望的结果。正如我在评论中提到的,我更可能期望这里有一个聚合的支点:
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;
因为你在描述中使用了pivot一词。下面是一个解决方案,它展示了如何使用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;
为什么用户1003
在预期结果中有两行用于联机,而Google
和LG
分别标记为1
?在这样的设计中,我希望它们都是1
一行。你是对的,对不起,我编了结果表,但是我如何证明购买平台
?