展平一列,将其他列保留在POSTGRESQL中

时间:2017-02-01 05:14:15

标签: postgresql postgresql-9.3

我创建了一个目前正在提供如下数据的视图:

practice_name   message_type    message_count
CHC                 ALOG_SYNC   1
CHC                   BULKNT    0
CHC                  PIE_SYNC   1
CHC                 PPRV_SYNC   1
CHC                SYNC_PRACT   3
CHC                 SYNC_PROV   9
CHC                 SYNC_WTXT   3
CHC                  SYNC_XYZ   0
Midtown             ALOG_SYNC   0
Midtown                 BULKNT  0
Midtown             PIE_SYNC    0
Midtown             PPRV_SYNC   0
Midtown            SYNC_PRACT   3
Midtown            SYNC_PROV    0
Midtown            SYNC_WTXT    3
Midtown             SYNC_XYZ    0
NextGen MedicalPractice ALOG_SYNC   0
NextGen MedicalPractice BULKNT  1
NextGen MedicalPractice PIE_SYNC    0
NextGen MedicalPractice PPRV_SYNC   0
NextGen MedicalPractice SYNC_PRACT  3
NextGen MedicalPractice SYNC_PROV   591
NextGen MedicalPractice SYNC_WTXT   3
NextGen MedicalPractice SYNC_XYZ    0

我的观点:

CREATE OR REPLACE VIEW sha.sha_export_queue_view AS 
 SELECT q3.practice_name,
    q3.message_type,
    q3.share_site_org_key,
    COALESCE(q2.message_count, '0'::text) AS message_count
   FROM ( SELECT q1.practice_name,
            mt.message_type,
            q1.share_site_org_key
           FROM sha.message_types mt,
            ( SELECT DISTINCT jsonb_array_elements((ai.result_json -> 'Patient Portal Operational Information'::text) -> 'nxmd_export contents by message type'::text) ->> 'Practice Name'::text AS practice_name,
                    ai.share_site_org_key
                   FROM sha.sha_share_site_view ssv
                     LEFT JOIN ( SELECT mytable2.assessment_id,
                            mytable2.result_json,
                            mytable2.share_site_org_key,
                            mytable2.rnk
                           FROM ( SELECT assessment_info.assessment_id,
                                    assessment_info.result_json,
                                    assessment_info.share_site_org_key,
                                    dense_rank() OVER (PARTITION BY assessment_info.share_site_org_key ORDER BY assessment_info.modified_datetime DESC) AS rnk
                                   FROM sha.assessment_info
                                  WHERE assessment_info.assessment_id = 8::numeric) mytable2
                          WHERE mytable2.rnk = 1) ai ON ssv.share_site_org_key = ai.share_site_org_key) q1) q3
     LEFT JOIN ( SELECT jsonb_array_elements((ai.result_json -> 'Patient Portal Operational Information'::text) -> 'nxmd_export contents by message type'::text) ->> 'Practice Name'::text AS practice_name,
            jsonb_array_elements((ai.result_json -> 'Patient Portal Operational Information'::text) -> 'nxmd_export contents by message type'::text) ->> 'Message Type'::text AS message_type,
            jsonb_array_elements((ai.result_json -> 'Patient Portal Operational Information'::text) -> 'nxmd_export contents by message type'::text) ->> 'Message Count'::text AS message_count
           FROM sha.sha_share_site_view ssv
             LEFT JOIN ( SELECT mytable2.assessment_id,
                    mytable2.result_json,
                    mytable2.share_site_org_key,
                    mytable2.rnk
                   FROM ( SELECT assessment_info.assessment_id,
                            assessment_info.result_json,
                            assessment_info.share_site_org_key,
                            dense_rank() OVER (PARTITION BY assessment_info.share_site_org_key ORDER BY assessment_info.modified_datetime DESC) AS rnk
                           FROM sha.assessment_info
                          WHERE assessment_info.assessment_id = 8::numeric) mytable2
                  WHERE mytable2.rnk = 1) ai ON ssv.share_site_org_key = ai.share_site_org_key) q2 ON q3.message_type::text = q2.message_type AND q3.practice_name = q2.practice_name
  ORDER BY q3.practice_name;

我希望第二列展平:

Practice    Time Stamp  <<message type 1>>  <<message type 2>>  <<message type 3>>  <<message type 4 >> <<message type 5>>  <<message type 6>>  <<message type 7>>  <<message type 8>>
Practice Name 1 21-12-2016 10:00    23  25  27  29  31  33  35  37
Practice Name 2 21-12-2016 10:00    24  26  28  30  32  34  36  38
Practice Name 3 21-12-2016 13:00    25  27  29  31  33  35  37  39
Practice Name 4 21-12-2016 13:00    26  28  30  32  34  36  38  40
Practice Name 5 24-12-2016 13:00    27  29  31  33  35  37  39  41
Practice Name 6 27-12-2016 13:00    28  30  32  34  36  38  40  42
Practice Name 7 30-12-2016 13:00    29  31  33  35  37  39  41  43
Practice Name 8 02-01-2017 13:00    30  32  34  36  38  40  42  44
Practice Name 1 05-01-2017 13:00    31  33  35  37  39  41  43  45
Practice Name 2 08-01-2017 13:00    32  34  36  38  40  42  44  46
Practice Name 3 11-01-2017 13:00    33  35  37  39  41  43  45  47

有什么办法可以实现吗? 抱歉小对齐问题。 值是对应的消息类型值

1 个答案:

答案 0 :(得分:1)

查询示例(评论中的想法):

SELECT
     practice_name,
     sum("ALOG_SYNC") AS "ALOG_SYNC",
     sum("BULKNT") AS "BULKNT",
     ...
FROM (
        SELECT
             practice_name,
             CASE WHEN q3.message_type = 'ALOG_SYNC' THEN sum(message_count) END AS "ALOG_SYNC",
             CASE WHEN q3.message_type = 'BULKNT' THEN sum(message_count) END AS "BULKNT"
        FROM
            <your from + where clause>
     ) AS A
GROUP BY 1

可能您的查询可能已经过优化。

或者您可以使用交叉表功能(https://www.postgresql.org/docs/current/static/tablefunc.html