- 爱易网页
 
                        - 
                            MSSQL教程
 
                        - 一个数据库的列变行有关问题 
 
                         
                    
                    
                    日期:2014-05-18  浏览次数:20639 次 
                    
                        
                         一个数据库的列变行问题
从表1得到下面的结果 
 表1    
 年份      月份      销售量      天数 
 2001         5                  400            4       
 2001         6                  400            8    
 2001         7                  400            5    
 2002         5                  400            10    
 2003         2                  400            12 
  
 得到    <font   color=#ff0033> 每年每个月每天销售数量(表1销售量/天数)    </font>  
 如2001   年 
 年份      1月(每天销售量)   2月   3月      4月         5月      6月      7月   8月   9月   10月      11月   12月 
 2001                                                                                                   100      50         80       
  
------解决方案--------------------
create table T1(年份 int, 月份 int,  销售量 int, 天数 int) 
  
 insert into T1 
  
 select 2001,   5,      400,    4   union all 
 select 2001,   6,      400,    8   union all 
 select 2001,   7,      400,    5   union all 
 select 2002,   5,      400,    10   union all 
 select 2003,   2,      400,    12 
  
  
 select  
       年份, 
       cast(sum(case when 月份=1 then 销售量 else null end)*1.0/max(case when 月份=1 then 天数 else 0 end) as decimal(10,2)) as [1月], 
       cast(sum(case when 月份=2 then 销售量 else null end)*1.0/max(case when 月份=2 then 天数 else 0 end) as decimal(10,2)) as [2月], 
       cast(sum(case when 月份=3 then 销售量 else null end)*1.0/max(case when 月份=3 then 天数 else 0 end) as decimal(10,2)) as [3月], 
       cast(sum(case when 月份=4 then 销售量 else null end)*1.0/max(case when 月份=4 then 天数 else 0 end) as decimal(10,2)) as [4月], 
       cast(sum(case when 月份=5 then 销售量 else null end)*1.0/max(case when 月份=5 then 天数 else 0 end) as decimal(10,2)) as [5月], 
       cast(sum(case when 月份=6 then 销售量 else null end)*1.0/max(case when 月份=6 then 天数 else 0 end) as decimal(10,2)) as [6月], 
       cast(sum(case when 月份=7 then 销售量 else null end)*1.0/max(case when 月份=7 then 天数 else 0 end) as decimal(10,2)) as [7月], 
       cast(sum(case when 月份=8 then 销售量 else null end)*1.0/max(case when 月份=8 then 天数 else 0 end) as decimal(10,2)) as [8月], 
       cast(sum(case when 月份=9 then 销售量 else null end)*1.0/max(case when 月份=9 then 天数 else 0 end) as decimal(10,2)) as [9月], 
       cast(sum(case when 月份=10 then 销售量 else null end)*1.0/max(case when 月份=10 then 天数 else 0 end) as decimal(10,2)) as [10月], 
       cast(sum(case when 月份=11 then 销售量 else null end)*1.0/max(case when 月份=11 then 天数 else 0 end) as decimal(10,2)) as [11月], 
       cast(sum(case when 月份=12 then 销售量 else null end)*1.0/max(case when 月份=12 then 天数 else 0 end) as decimal(10,2)) as [12月] 
 from T1 
 group by 年份 
  
 drop table T1
------解决方案--------------------
--创建测试环境 
 create table T1(年份 int, 月份 int,  销售量 int, 天数 int) 
  
  
 --追加测试数据 
 insert into T1 
 select 2001,   5,      400,    4   union all 
 select 2001,   6,      400,    8   union all 
 select 2001,   7,      400,    5   union all 
 select 2002,   5,      400,    10   union all 
 select 2003,   2,      400,    12