我想知道是否可以在不直接引用IN子句中的值的情况下在存储过程中进行转动。
例如,我有一个如下所示的支点:
select *
from DailyIncome
pivot (avg (IncomeAmount) for IncomeDay in ([MON],[TUE],[WED],[THU],[FRI],[SAT],[SUN])) as AvgIncomePerDay
是否有可能使它能够从以下结果中获取结果:
select *
from DailyIncome
pivot (avg (IncomeAmount) for IncomeDay LIKE ('%MON%','%TUE%','%WED%','%THU%','%FRI%','%SAT%','%SUN%')) as AvgIncomePerDay
答案 0 :(得分:1)
您应格式化源数据。
create table tbl (name varchar(10), value int); insert into tbl values ('mon10', 10), ('mon11', 11), ('mon13', 13), ('tue10', 20), ('tue11', 21), ('tue12', 30); GO
6 rows affected
select * from ( select case when name like '%mon%' then 'mon' when name like '%tue%' then 'tue' end name, value from tbl ) src pivot ( avg(value) for name in ([mon], [tue]) ) pv; GO
mon | tue --: | --: 11 | 23
dbfiddle here