zoukankan      html  css  js  c++  java
  • sql数据行转列

    select CodeName FROM CodeDictionary where CodeCategory_ID=138
    结果:
    
    
    开始转换:
    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)
    结果就类似这样的语句:
    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
    
    
    
    
    
     
  • 相关阅读:
    如何在自定义端口上运行 Spring Boot 应用程序?
    FileUpload拦截器
    UI标签
    OGNL
    你对测试最大的兴趣在哪里?为什么?
    举例说明同步和异步。
    git 克隆分支
    JQuery 选择器
    JQuery对象和Dom对象
    简单的JQuery之Ready
  • 原文地址:https://www.cnblogs.com/wt-vip/p/8983746.html
Copyright © 2011-2022 走看看