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)
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;
4条答案
按热度按时间zzwlnbp81#
如果需要动态列,则需要动态SQL
示例
退货
9rbhqvlz2#
只需使用
case
:sg3maiej3#
您可以
eni9jsuy4#
您还可以将透视运算符应用于动态SQL。
例如: