Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Identifying non-consecutive values is always a bit tricky and involves several nested sub-queries (at least I cannot come up with a better solution).</p> <p>The first step is to identify non-consecutive values for the year:</p> <h3>Step 1) Identify non-consecutive values</h3> <pre><code>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) &gt; 1 then 1 else 0 end as group_cnt from qualification </code></pre> <p>This returns the following result:</p> <pre> 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 </pre> <p>Now with the group_cnt value we can create "group IDs" for each group that has consecutive years:</p> <h3>Step 2) Define group IDs</h3> <pre><code>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) &gt; 1 then 1 else 0 end as group_cnt from qualification ) t1 </code></pre> <p>This returns the following result:</p> <pre> 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) </pre> <p>As you can see each "group" got its own group_nr and this we can finally use to aggregate over by adding yet another derived table:</p> <h3>Step 3) Final query</h3> <pre><code>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) &gt; 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 </code></pre> <p>This returns the following result:</p> <pre> company | profession | years ---------+------------+------------------ Google | Programmer | {2000} Google | Sales | {2000,2001,2002} Google | Sales | {2004} Mozilla | Sales | {2002} (4 rows) </pre> <p>Which is exactly what you wanted, if I'm not mistaken.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload