Question

This my query and this is not helping as per required output

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

Current Result of this query is

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

But I need below output where AVA,2D AND 3D as column header and below their value

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

Thanks

Was it helpful?

Solution

You need case clause, which put value in right column.

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
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top