Jos*_*rns 13 sql postgresql plpgsql window-functions gaps-and-islands
使用PostgreSQL 9.0.
比方说,我有一个包含字段的表:company
,profession
和year
.我想返回一个包含独特公司和专业的结果,但基于数字序列聚合(到一个数组中很好)年份:
示例表:
+-----------------------------+
| company | profession | year |
+---------+------------+------+
| Google | Programmer | 2000 |
| Google | Sales | 2000 |
| Google | Sales | 2001 |
| Google | Sales | 2002 |
| Google | Sales | 2004 |
| Mozilla | Sales | 2002 |
+-----------------------------+
Run Code Online (Sandbox Code Playgroud)
我对一个输出类似于以下行的查询感兴趣:
+-----------------------------------------+
| company | profession | year |
+---------+------------+------------------+
| Google | Programmer | [2000] |
| Google | Sales | [2000,2001,2002] |
| Google | Sales | [2004] |
| Mozilla | Sales | [2002] |
+-----------------------------------------+
Run Code Online (Sandbox Code Playgroud)
基本特征是只有连续的年份才能组合在一起.
a_h*_*ame 19
识别非连续值总是有点棘手,涉及几个嵌套的子查询(至少我不能提出更好的解决方案).
第一步是确定年份的非连续值:
select company,
profession,
year,
case
when row_number() over (partition by company, profession order by year) = 1 or
year - lag(year,1,year) over (partition by company, profession order by year) > 1 then 1
else 0
end as group_cnt
from qualification
Run Code Online (Sandbox Code Playgroud)
这将返回以下结果:
company | profession | year | group_cnt ---------+------------+------+----------- Google | Programmer | 2000 | 1 Google | Sales | 2000 | 1 Google | Sales | 2001 | 0 Google | Sales | 2002 | 0 Google | Sales | 2004 | 1 Mozilla | Sales | 2002 | 1
现在使用group_cnt值,我们可以为连续年份的每个组创建"组ID":
select company,
profession,
year,
sum(group_cnt) over (order by company, profession, year) as group_nr
from (
select company,
profession,
year,
case
when row_number() over (partition by company, profession order by year) = 1 or
year - lag(year,1,year) over (partition by company, profession order by year) > 1 then 1
else 0
end as group_cnt
from qualification
) t1
Run Code Online (Sandbox Code Playgroud)
这将返回以下结果:
company | profession | year | group_nr ---------+------------+------+---------- Google | Programmer | 2000 | 1 Google | Sales | 2000 | 2 Google | Sales | 2001 | 2 Google | Sales | 2002 | 2 Google | Sales | 2004 | 3 Mozilla | Sales | 2002 | 4 (6 rows)
正如您所看到的,每个"group"都有自己的group_nr,我们最终可以通过添加另一个派生表来聚合:
select company,
profession,
array_agg(year) as years
from (
select company,
profession,
year,
sum(group_cnt) over (order by company, profession, year) as group_nr
from (
select company,
profession,
year,
case
when row_number() over (partition by company, profession order by year) = 1 or
year - lag(year,1,year) over (partition by company, profession order by year) > 1 then 1
else 0
end as group_cnt
from qualification
) t1
) t2
group by company, profession, group_nr
order by company, profession, group_nr
Run Code Online (Sandbox Code Playgroud)
这将返回以下结果:
company | profession | years ---------+------------+------------------ Google | Programmer | {2000} Google | Sales | {2000,2001,2002} Google | Sales | {2004} Mozilla | Sales | {2002} (4 rows)
如果我没弄错的话,这正是你想要的.
And*_*y M 11
@ a_horse_with_no_name的答案有很多价值,作为一个正确的解决方案,就像我在评论中已经说过的那样,是学习如何在PostgreSQL中使用不同类型的窗口函数的好材料.
然而,我不禁感到在这个答案中所采用的方法对于像这样的问题来说有点过分了.基本上,在进行数组聚合年之前,您需要的是一个额外的分组标准.你已经有了company
和profession
,现在你只需要一些东西来区分属于不同的序列年.
这就是上面提到的答案提供的内容,而这正是我认为可以用更简单的方式完成的.这是如何做:
WITH MarkedForGrouping AS (
SELECT
company,
profession,
year,
year - ROW_NUMBER() OVER (
PARTITION BY company, profession
ORDER BY year
) AS seqID
FROM atable
)
SELECT
company,
profession,
array_agg(year) AS years
FROM MarkedForGrouping
GROUP BY
company,
profession,
seqID
Run Code Online (Sandbox Code Playgroud)
对于带有聚合/Windows 函数的普通 SQL 来说,这个问题相当难以处理。虽然循环通常比使用普通 SQL 的基于集合的解决方案慢,但是使用 PL/pgSQL 的过程解决方案可以通过对表进行单次顺序扫描(循环的隐式游标)来完成FOR
,并且在这种特殊情况下应该要快得多:
测试表:
CREATE TEMP TABLE tbl (company text, profession text, year int);
INSERT INTO tbl VALUES
('Google', 'Programmer', 2000)
, ('Google', 'Sales', 2000)
, ('Google', 'Sales', 2001)
, ('Google', 'Sales', 2002)
, ('Google', 'Sales', 2004)
, ('Mozilla', 'Sales', 2002)
;
Run Code Online (Sandbox Code Playgroud)
功能:
CREATE OR REPLACE FUNCTION f_periods()
RETURNS TABLE (company text, profession text, years int[])
LANGUAGE plpgsql AS
$func$
DECLARE
r tbl; -- use table type as row variable
r0 tbl;
BEGIN
FOR r IN
SELECT * FROM tbl t ORDER BY t.company, t.profession, t.year
LOOP
IF ( r.company, r.profession, r.year)
<> (r0.company, r0.profession, r0.year + 1) THEN -- not true for first row
RETURN QUERY
SELECT r0.company, r0.profession, years; -- output row
years := ARRAY[r.year]; -- start new array
ELSE
years := years || r.year; -- add to array - year can be NULL, too
END IF;
r0 := r; -- remember last row
END LOOP;
RETURN QUERY -- output last iteration
SELECT r0.company, r0.profession, years;
END
$func$;
Run Code Online (Sandbox Code Playgroud)
称呼:
SELECT * FROM f_periods();
Run Code Online (Sandbox Code Playgroud)
db<>在这里摆弄
产生所请求的结果。