在一个test表里面字段和数据如下:name            teacher    class    price
初级会计务实    李真       精讲班   200    
初级会计务实    李真       通关班   300  
初级会计务实    李真       协议班   800 
经济法基础      陆宝       精讲班   200   
经济法基础      陆宝       通关班   300  
经济法基础      陆宝       协议班   800  我想查询出这样的效果:
课程          老师      精讲班   通关班    协议班 
初级会计务实  李真    200      300       800 
经济法基础    陆宝    200      300       800本人小菜鸟,在线等大神的帮助

解决方案 »

  1.   

    SELECT name, teacher, sum("精讲班"), sum("通关班"), sum("协议班")
      FROM (select name,
                   teacher,
                   case
                     when a.class = '精讲班' then
                      a.price
                   END "精讲班",
                   case
                     when a.class = '通关班' then
                      a.price
                   END "通关班",
                   case
                     when a.class = '协议班' then
                      a.price
                   END "协议班"
              from test a) b
     group by name, teacher;
      

  2.   


    SELECT name, teacher, sum("精讲班"), sum("通关班"), sum("协议班")
      FROM (select name,
                   teacher,
                   case
                     when a.class = '精讲班' then
                      a.price
                   END "精讲班",
                   case
                     when a.class = '通关班' then
                      a.price
                   END "通关班",
                   case
                     when a.class = '协议班' then
                      a.price
                   END "协议班"
              from test_1 a) b
     group by name, teacher;
      

  3.   

    select name ,teacher ,case when price='200' then '精讲班' end ,case  when price='300' then '通关班' end,case  when price='800' then '协议班' end from  table
      

  4.   

    with c as
     (select '初级会计务实' name, '李真' teacher, '精讲班' class, 200 price
        from dual
      union all
      select '初级会计务实', '李真', '通关班', 300
        from dual
      union all
      select '初级会计务实', '李真', '协议班', 800
        from dual
      union all
      select '经济法基础', '陆宝', '精讲班', 200
        from dual
      union all
      select '经济法基础', '陆宝', '通关班', 300
        from dual
      union all
      select '经济法基础', '陆宝', '协议班', 800 from dual)
    select c.name,c.teacher,max(decode(c.class,'精讲班',price,null)) 精讲班,
    max(decode(c.class,'通关班',price,null)) 通关班,
    max(decode(c.class,'协议班',price,null)) 协议班
    from c c
    group by c.name,c.teacher
      

  5.   


      看这情况 应该是用sum吧? sum(decode(c.class,'精讲班',price,0)) 精讲班
      

  6.   

    在一个test表里面字段和数据如下:name teacher class price
    初级会计务实 李真 精讲班 200   
    初级会计务实 李真 通关班 300   
    初级会计务实 李真 协议班 800  
    经济法基础 陆宝 精讲班 200   
    经济法基础 陆宝 通关班 300   
    经济法基础 陆宝 协议班 800   
    select name,teacher,sum(decode(class,'精讲班',price,'0')),sum(decode(class,'通关班,price,'0')),sum(decode(class,'协议班',price,'0')) from test
      

  7.   

    with temp1 as (select distinct name,teacher from test) 
    select name as 课程,teacher as 老师,
    (select price from test where name=a.name and teacher=a.teacher and class='精讲班')  as 精讲班,
    (select price from test where name=a.name and teacher=a.teacher and class='通关班')  as 通关班,
    (select price from test where name=a.name and teacher=a.teacher and class='协议班')  as 协议班  
    from temp1 a