PostgreSQL - 将多行与多个属性组合成一行?

问题描述 投票:2回答:3

我有这样一张桌子:

DATE        ID    ScoreA    ScoreB    ScoreC
20180101    001   91        92        25
20180101    002   81        82        35
20180101    003   71        52        45
20180102    001   82        15        66
20180102    002   69        67        77
...
20180131    003   88        65        73

以一个月的数据为例,我想将它们汇总成MAX和MIN得分报告,每个ID只有一行。就像:

ID    ScoreA       Date_A              ScoreB        Date_B            ...
001   MAX(ScoreA)  MAX(ScoreA).DATE    MAX(ScoreB)   MAX(ScoreB).DATE  ...
002   MAX(ScoreA)  MAX(ScoreA).DATE    MAX(ScoreB)   MAX(ScoreB).DATE  ...
003   MAX(ScoreA)  MAX(ScoreA).DATE    MAX(ScoreB)   MAX(ScoreB).DATE  ...

当相应的MAX或MIN分数出现时,MAX(ScoreA).DATE表示DATE(如果MAX分数出现在多个日期,则只需随机选择一个)

与常见的组合行情况不同,它同时涉及多个列。而且因为会有很多IDs和qincexswpois(我的意思是有Score ScroeA ... ScroreB ... ScoreZ Score1 ... Score2 ...),我希望避免使用消耗操作,例如Score100表。那么有什么好主意吗?

sql postgresql greatest-n-per-group greenplum
3个回答
2
投票

如果你想避免加入,我会提供这样的结构

JOIN

当你需要日期或其他一些max / min值的属性时,使用行编号而不是聚合函数是合理的:WITH cte AS ( SELECT DATE, ID, ScoreA, ScoreB, ScoreC, row_number() over (partition by ID order by ScoreA desc) rnA, row_number() over (partition by ID order by ScoreB desc) rnB, row_number() over (partition by ID order by ScoreC desc) rnC, FROM ... WHERE DATE BETWEEN ... AND ... ), ids AS ( SELECT DISTINCT ID FROM cte ) SELECT ID, (SELECT ScoreA FROM cte t2 WHERE t2.ID = t.ID AND rnA = 1) ScoreA, (SELECT DATE FROM cte t2 WHERE t2.ID = t.ID AND rnA = 1) Date_A, (SELECT ScoreB FROM cte t2 WHERE t2.ID = t.ID AND rnB = 1) ScoreB, (SELECT DATE FROM cte t2 WHERE t2.ID = t.ID AND rnB = 1) Date_B, (SELECT ScoreC FROM cte t2 WHERE t2.ID = t.ID AND rnC = 1) ScoreC, (SELECT DATE FROM cte t2 WHERE t2.ID = t.ID AND rnC = 1) Date_C FROM ids t 后跟条件row_number() over (...) as rn

UPD

一旦@TaurusDang想要生成代码,我的解决方案就是允许postgres完成几乎所有的工作:

rn = 1

只需将生成的行复制到初始查询中:前半部分为cte,第二部分为主查询


0
投票

试试这个

WITH cols AS
(
    SELECT column_name
    FROM information_schema.columns
    WHERE table_schema = 'your_schema'
      AND table_name   = 'your_table'
      AND column_name like 'Score%'
)
-- first part: rows for cte subquery
SELECT ',row_number() over (partition by ID order by ' || column_name || ' desc) rn' || column_name
FROM cols
UNION ALL
-- second part: rows for final query
SELECT ',(SELECT ' || column_name || ' FROM cte t2 WHERE t2.ID = t.ID AND rn' || column_name || ' = 1) ' || column_name || ', (SELECT DATE FROM cte t2 WHERE t2.ID = t.ID AND rn' || column_name || ' = 1) Date_' || column_name
FROM cols

0
投票

这是另一个代码示例,它将为您提供所需的所有数据:

with max_score as
(
    Select    distinct id
        , max(ScoreA) over( partition by id ) as max_ScoreA
        , max(ScoreB) over( partition by id ) as max_ScoreB
        , max(ScoreC) over( partition by id ) as max_Scorec
    from TABLE_NAME
)
Select 
    cte.id
    , max_ScoreA, tbl_a.DATE
    , max_ScoreB, tbl_b.DATE
    , max_ScoreC, tbl_c.DATE
from 
max_score cte
join TABLE_NAME tbl_a
on cte.id = tbl_a.id
and cte.max_ScoreA = tbl_a.ScoreA
join TABLE_NAME tbl_b
on cte.id = tbl_b.id
and cte.max_ScoreB = tbl_b.ScoreB
join TABLE_NAME tbl_c
on cte.id = tbl_c.id
and cte.max_ScoreC = tbl_c.ScoreC
order by 1

请注意,有3个单独的子查询,每个分数列一个。关于select * from (select distinct on (id) id, first_value(scorea) over w as a_min, last_value(scorea) over w as a_max, first_value(date) over w as a_min_d, last_value(date) over w as a_max_d from the_table window w as (partition by id order by scorea) order by 1,3 desc) a join (select distinct on (id) id, first_value(scoreb) over w as b_min, last_value(scoreb) over w as b_max, first_value(date) over w as b_min_d, last_value(date) over w as b_max_d from the_table window w as (partition by id order by scoreb) order by 1,3 desc) b using(id) join (select distinct on (id) id, first_value(scorec) over w as c_min, last_value(scorec) over w as c_max, first_value(date) over w as c_min_d, last_value(date) over w as c_max_d from the_table window w as (partition by id order by scorec) order by 1,3 desc) c using(id) ,这里有一些神奇的东西可供阅读。这里一个棘手的部分是,如果放在同一个查询中,各个分区会相互干扰(至少在我的第9.3.22页)。

© www.soinside.com 2019 - 2024. All rights reserved.