sql随处查询构建

时间:2014-04-15 10:14:23

标签: sql sqlanywhere

这是我的查询,这根据所需的输出没有帮助

SELECT 
    e.patientid,
    e.indexpatient, 
    e.indexstudy, 
    e.studydatetime, 
    e.lastname, 
    e.firstname,
    p.parameterID,
    sum(p.resultvalue) as ResultValue


FROM dbcreators.gems_examview e, 
    dbcreators.parameter p 

WHERE p.parameterID 
  in
 ('AVA (VTI)', '2D/Ao Root Diam', '2D/Ao asc Diam')

    and 
     e.studydatetime<'2014-02-28' and e.studydatetime>'2009-04-12' and
     p.indexpatient = e.indexpatient and p.indexstudy = e.indexstudy and
     p.indexseries = e.indexseries and  p.resultno = -1 
     group by e.patientid,
    e.indexpatient, 
    e.indexstudy, 
    e.studydatetime, 
    e.lastname, 
    e.firstname,
    p.parameterID
order by patientid desc

此查询的当前结果是

patientid   indexpatient    indexstudy  studydatetime    ID  RV 
---------------------------------------------------------------------------------
AA9805      9805        5   9/10/09 12:51 PM       AVA  0.0001
AA8733      8733        5   4/17/09 2:38 PM        2D   0.0002
A7622       7622        7   5/6/09 9:23 AM         3D   0.0049

但我需要在AVA,2D和3D作为列标题以及低于其值

的输出下方
patientid   indexpatient    indexstudy  studydatetime    AVA     2D     3D 
---------------------------------------------------------------------------------
AA9805      9805        5   9/10/09 12:51 PM 0.0001
AA8733      8733        5   4/17/09 2:38 PM     0.0002
A7622       7622        7   5/6/09 9:23 AM             0.0049

由于

1 个答案:

答案 0 :(得分:0)

您需要case子句,它将值放在右列中。

SELECT 
        e.patientid,
        e.indexpatient, 
        e.indexstudy, 
        e.studydatetime, 
        e.lastname, 
        e.firstname,
        p.parameterID,
        case
             when p.parameterID = 'AVA (VTI)' then sum(p.resultvalue)
        end as AVA,
        case
             when p.parameterID = '2D/Ao Root Diam' then sum(p.resultvalue)
        end as 2D,
        case
             when p.parameterID = '2D/Ao Root Diam' then sum(p.resultvalue)
        end as 3D    

    FROM dbcreators.gems_examview e, 
        dbcreators.parameter p 

    WHERE p.parameterID 
      in
     ('AVA (VTI)', '2D/Ao Root Diam', '2D/Ao asc Diam')

        and 
         e.studydatetime<'2014-02-28' and e.studydatetime>'2009-04-12' and
         p.indexpatient = e.indexpatient and p.indexstudy = e.indexstudy and
         p.indexseries = e.indexseries and  p.resultno = -1 
         group by e.patientid,
        e.indexpatient, 
        e.indexstudy, 
        e.studydatetime, 
        e.lastname, 
        e.firstname,
        p.parameterID
    order by patientid desc