简体   繁体   English

带有 PIVOT 的 CROSSTAB

[英]CROSSTAB with PIVOT

This is what my data looks like -这就是我的数据的样子 -

id  type  entity  diag  count
9   SER   ORG     a     18
9   SER   ORG     b     5
9   SER   PRAC    b     50

When I run this query -当我运行此查询时 -

select *
    FROM CROSSTAB($$
    SELECT
        id, type, entity,
        diag,
        count
    FROM calc
    $$, $$
    SELECT
       name
    FROM diagnosis
    ORDER by name
    LIMIT 3
$$) AS pivot (
    id INT,
    type  TEXT,
    entity INT, 
    a INT, b INT, c int
);

The output is - output 是 -

id  type  entity  a  b
9   SER   ORG     18 5

What happened to the last record in the input?输入中的最后一条记录发生了什么?

I would expect this output -我希望这个 output -

id  type  entity  a  b
9   SER   ORG     18 5
9   SER   PRAC       50

Try your Crosstab function in this way:以这种方式尝试您的Crosstab表 function:

select 
col[1] "id", 
col[2] "Type", 
col[3] "Entity", 
a, 
b, 
c
    FROM CROSSTAB($$
    SELECT
        ARRAY[id::varchar, type, entity],
        diag,
        count
    FROM calc order by 1
    $$, $$
    SELECT
       name
    FROM diagnosis
    ORDER by name
    LIMIT 3
$$) AS pivot (
col text[], 
    a INT, b INT, c int
);

DEMO 演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM