在数据库中有两个表com_estimation,com_jyoutai  
表com_estimation的结构  
com_id    com_reg_code   com_id    com_estimation_kbn    com_d_no     com_ing    
 2             5            1             10               10              1  
 2             5            1             10               20              1  
 2             5            1             10               30              1  
 2             5            1             10               40              1  
 2             5            1             20               10              1  
 2             5            1             20               20              1  
 2             5            1             20               30              1  
 2             5            1             20               40              1    
数据表中有这样的数据com_estimation_kbn=10表示中间结果,=20表示最终结果。  
前五个为主键  
表com_jyoutai的结构:  
com_id        com_reg_code    com_id_d    com_title_name  
  2                5             1             数学  
  2                5             2             语文  
前三个字段为主键,和上表一样  
现在想得到这样的形式  
com_title_name         中间结果           最终结果  
 
请问sql语句应该怎么写啊?

解决方案 »

  1.   

    你试试看:
    select com_jyoutai.com_title_name ,(select com_estimation_kbn from com_estimation where com_estimation.com_id =com_jyoutai.com_id and com_estimation.com_reg_code=  com_jyoutai.com_reg_code and  com_estimation.com_id_d = com_jyoutai.com_id_d and com_estimation_kbn=10) as 中间结果,(select com_estimation_kbn from com_estimation where com_estimation.com_id =com_jyoutai.com_id and com_estimation.com_reg_code=  com_jyoutai.com_reg_code and  com_estimation.com_id_d = com_jyoutai.com_id_d and com_estimation_kbn=20) as 最终结果 from com_jyoutai
      

  2.   

    sorry!露了第一个表
    你试试看:
    select com_jyoutai.com_title_name ,(select com_estimation_kbn from com_estimation where com_estimation.com_id =com_jyoutai.com_id and com_estimation.com_reg_code=  com_jyoutai.com_reg_code and  com_estimation.com_id_d = com_jyoutai.com_id_d and com_estimation_kbn=10) as 中间结果,(select com_estimation_kbn from com_estimation where com_estimation.com_id =com_jyoutai.com_id and com_estimation.com_reg_code=  com_jyoutai.com_reg_code and  com_estimation.com_id_d = com_jyoutai.com_id_d and com_estimation_kbn=20) as 最终结果 from com_jyoutai,com_estimation
      

  3.   

    select 
    a.com_title_name,
    '中间结果'=case a.com_estimation_kbn when 10 then 10 else 0 end,
    '最终结果'=case a.com_estimation_kbn when 20 then 20 else 0 endfrom com_estimation a left join com_jyoutai b
    on a.com_id=b.com_id and
    a.com_reg_code=b.com_reg_code and 
    a.com_id_d=b.com_id_d
    大概应该是这个样子,修改一下
      

  4.   

    加个sum(com_ing)该怎么弄??
      

  5.   

    select 
    a.com_title_name,
    '中间结果'=sum(case a.com_estimation_kbn when 10 then com_ing else 0 end),
    '最终结果'=sum(case a.com_estimation_kbn when 20 then com_ing else 0 end)from com_estimation a left join com_jyoutai b
    on a.com_id=b.com_id and
    a.com_reg_code=b.com_reg_code and 
    a.com_id_d=b.com_id_d
    group by a.com_title_name