MSSQL行转列 (练习练习)

原始数据:

目标效果:

SQL语句:

select bb.sshop_c,
bb.sshop_n,
case sweek6 when 0 then '×' else '' end saturday , case sweek7 when 0 then '×' else '' end sunday from ( select j.sshop_c,j.sshop_n,
isnull(sweek6,0)sweek6 ,
isnull(sweek7,0)sweek7
from pub_jshop as j left join ( select sShop_c,
sum(case when sDay='Saturday' and bBusiness='' then 1 else 0 end) as sWeek6 , sum(case when sDay='Sunday' and bBusiness='' then 1 else 0 end ) as sWeek7 from swt_jOpeningDate group by sShop_c ) as bt on j.sshop_c=bt.sshop_c ) bb
--分组进行判断的时候把√ or x 用0 和1 代替,着用,分组聚合函数的时候好区分不同的情况而不影响结果。

原文地址:https://www.cnblogs.com/pigddyou/p/3867281.html