基本数据透视表

时间:2017-04-17 17:38:24

标签: tsql

我的数据如下表所示:

select person_name, appointment_id, unique_encounter, enounter_code 
from appointment

person_name appointment_id  unique_encounter    enounter_code   
----------- --------------  ----------------    -------------
Mike        123             A1                  CG
Mike        123             A1                  DT  
John        458             1F                  CG  
Bill        555             AB                  AJ
Joe         197             ZZ                  CG
Joe         197             ZZ                  AJ
Joe         197             ZZ                  DT

我正在尝试编写查询以实现以下结果:

person_name appointment_id  unique_encounter    enounter_code_1 enounter_code_2 enounter_code_3     
----------- --------------  ----------------    -------------   --------------- ---------------
Mike        123             A1                  New                             Deleted
John        458             1F                  New 
Bill        555             AB                                  Updated
Joe         197             ZZ                  New             Updated         Deleted

我的查询无效:可以提供一些帮助。感谢

select person_name, appointment_id, unique_encounter 
    ,Case when enounter_code = 'CG' then 'New' else '' End as enounter_code_1   
    ,Case when enounter_code = 'AJ' then 'Updated' else '' End as enounter_code_2
    ,Case when enounter_code = 'DT' then 'Deleted' else '' End as enounter_code_3

from appointment
group by person_name, appointment_id, unique_encounter 

1 个答案:

答案 0 :(得分:0)

您是否有权转动数据。

尝试这个不同的观点。

add </element> after "</web-app>". It will work.

尝试一下: http://data.stackexchange.com/meta.stackoverflow/query/edit/659696