select CodeName FROM CodeDictionary where CodeCategory_ID=138
结果:
sql数据行转列

 


开始转换:
DECLARE
@sql VARCHAR(8000) SELECT @sql=isnull(@sql+',','')+CodeName FROM CodeDictionary where CodeCategory_ID=138 GROUP BY CodeName SET @sql='select * from (select ch.HousebillID,ch.Chargeamount,cd.CodeCategory_ID,cd.CodeName from Housebill hb left join Chargebill ch on hb.ID=ch.HousebillID left join CodeDictionary cd on ch.Chargeitem=cd.ID where cd.CodeCategory_ID=138) tb pivot (max(tb.Chargeamount) for tb.CodeName in ('+@sql+'))a' exec(@sql)
sql数据行转列

 

结果就类似这样的语句:
select
* from ( select ch.HousebillID,ch.Chargeamount,cd.CodeCategory_ID,cd.CodeName from Housebill hb left join Chargebill ch on hb.ID=ch.HousebillID left join CodeDictionary cd on ch.Chargeitem=cd.ID where cd.CodeCategory_ID=138 ) tb pivot (sum(tb.Chargeamount) for tb.CodeName in (保洁费,材料费,电费,服务费,宽带,其他,燃气费,水费,维修费,卫生,物业费,押金,优惠,有线电视,租金) )a where CodeCategory_ID=138

 



 

 

 

 

相关文章:

  • 2021-07-15
  • 2021-07-15
  • 2022-02-12
  • 2022-12-23
  • 2022-12-23
  • 2021-09-14
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2021-10-08
  • 2022-02-09
  • 2021-08-12
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案