此表的“符号”列为:
我想将其转换为一个表格,该表格将Symbol的列值(不同)作为新的Column Name。这些列的类型应为bool或INT(0/1)。结果表应如下所示:
P.S。如果列集是由另一个查询动态生成的,怎么办?
答案 0 :(得分:1)
只需使用case
:
select (case when symbol = 'ACRX' then 1 else 0 end) as acrx,
(case when symbol = 'ANF' then 1 else 0 end) as anf,
(case when symbol = 'NVDA' then 1 else 0 end) as nvda,
(case when symbol = 'rsx' then 1 else 0 end) as rsx,
partofday, isprofitable
from t;
答案 1 :(得分:1)
你可以做
select
IIF('ACRX', 1, 0) as acrx,
IIF('ANF', 1, 0) as anf,
IIF('NVDA', 1, 0) as nvda,
IIF('RXS', 1, 0) as rsx,
partofday,
isprofitable
from t;
答案 2 :(得分:1)
如果需要动态列,则需要动态SQL
示例
Declare @SQL varchar(max) = Stuff((Select Distinct ',' + QuoteName(Symbol)+'=case when symbol='''+Symbol+''' then 1 else 0 end ' From Yourtable Order by 1 For XML Path('')),1,1,'')
Select @SQL = '
Select '+@SQL+'
,PartOfDay
,isProfitable
From YourTable
'
Exec(@SQL)
返回
答案 3 :(得分:1)
您还可以将数据透视运算符应用于动态SQL。
例如:
create table test03
(
Symbol varchar(30),
PartOfDay int,
isProfitable int
)
insert into test03 values
('ACRX',3,0),
('ACRX',3,0),
('ANF',2,1),
('ANF',2,1),
('ANF',2,1),
('NVDA',3,1),
('RSX',3,0),
('RSX',3,0)
--Query
DECLARE @V_COLUMNS VARCHAR(MAX)
DECLARE @V_SQL VARCHAR(MAX)
SET @V_COLUMNS=STUFF((SELECT DISTINCT ','+QUOTENAME(Symbol) FROM test03 FOR XML PATH('')),1,1,'')
--print @V_COLUMNS
SET @V_SQL='
;WITH CTE AS
(
select
ROW_NUMBER() over (order by Symbol) as RN,
Symbol,
1 AS V_VALUE,
PartOfDay,
isProfitable
from test03
)
SELECT '+@V_COLUMNS+',PartOfDay,isProfitable
FROM CTE
PIVOT
(
COUNT(V_VALUE) FOR Symbol IN ('+@V_COLUMNS+')
) PVT'
--PRINT @V_SQL
EXEC(@V_SQL)
--Output
/*
ACRX ANF NVDA RSX PartOfDay isProfitable
----------- ----------- ----------- ----------- ----------- ------------
1 0 0 0 3 0
1 0 0 0 3 0
0 1 0 0 2 1
0 1 0 0 2 1
0 1 0 0 2 1
0 0 1 0 3 1
0 0 0 1 3 0
0 0 0 1 3 0
*/