日期:2014-05-16  浏览次数:20646 次

请教一个关于行转列的问题
表结构如下:
---------------------------------------
HT_DATE  HT_GROSS  HT_COUNT  HT_NAME
 2010     20.00      30       aa
 2011     21.00      31       bb
 2012     23.00      33       cc
 2013     25.00      35       dd
----------------------------------------
现在想要这样的效果:
---------------------------------------
  2010     2011      2012     2013 ……     (HT_DATE)  
  20.00    21.00     23.00    25.00       (HT_GROSS)   
  30        31        33       35         (HT_COUNT)  
  aa        bb        cc       dd         (HT_NAME)
 --------------------------------------
请教大虾们,这个语句该怎么写!!在此感激不尽!!
------解决方案--------------------
 create table t1
  (
   ht_date char(4),
   ht_gross  decimal(4,2),
   ht_count  int,
   ht_name char(4)
  )
  
  insert into t1 
  select '2010',20.00,30,'aa'
  union all select '2011',21.00,31,'bb'
   union all select '2012',23.00,33,'cc'
    union all select '2013',25.00,31,'dd'
    
    select * from t1
    
    select ht_name,ht_count ,sum(case when ht_date='2010' then ht_gross else 0 end)  n2010
    ,
    sum(case when ht_date='2011' then ht_gross else 0 end)  n2011,
    sum(case when ht_date='2012' then ht_gross else 0 end)  n2011,
    sum(case when ht_date='2013' then ht_gross else 0 end)  n2011
    from t1
    group by ht_name,ht_count
   


------解决方案--------------------
2010     2011      2012     2013 ……     (HT_DATE)  
   20.00    21.00     23.00    25.00       (HT_GROSS)   
   30        31        33       35 &nbs