public inbox for [email protected]help / color / mirror / Atom feed
Change detection 8+ messages / 4 participants [nested] [flat]
* Change detection @ 2022-12-09 12:55 Shaozhong SHI <[email protected]> 0 siblings, 2 replies; 8+ messages in thread From: Shaozhong SHI @ 2022-12-09 12:55 UTC (permalink / raw) To: pgsql-sql <[email protected]> A record shows all staff member worked in different departments. For instance, Tom was in sales for several years and got promoted to management. How to detect the time of this change? Data Staff_ID Name Department Year 1 Tom Sales 1990 2 Tom Sales 1991 3 Tom Sales 1991 4 Tom Management 1992 4 Tom Management 1992 Regards, David ^ permalink raw reply [nested|flat] 8+ messages in thread
* AW: Change detection @ 2022-12-09 13:02 Stöcker, Martin <[email protected]> parent: Shaozhong SHI <[email protected]> 1 sibling, 0 replies; 8+ messages in thread From: Stöcker, Martin @ 2022-12-09 13:02 UTC (permalink / raw) To: Shaozhong SHI <[email protected]>; pgsql-sql <[email protected]> select Name, Department, min(year), max(year) from data group by Name, Department The meaning of Staff_ID is obscure because it’s not unique. Mit freundlichen Grüßen Martin Stöcker ----------------------------------------- ETL Datenservice GmbH Widdersdorfer Str. 415 | D-50933 Köln Telefon: +49(0)2219544010 Fax: +49(0)2219544015 Email: [email protected] [etlds] ETL Datenservice GmbH Widdersdorfer Str. 415 · 50933 Köln Geschäftsführer: Dr. Dirk Goldner, ppa. Melanie Lillich, ppa. Udo Heuschmann Amtsgericht Köln · HRB 75439 · USt.-Id: DE 122 805 685 www.etl-datenservice.de<http://www.etl-datenservice.de; Email: [email protected]<mailto:[email protected]> Von: Shaozhong SHI <[email protected]> Gesendet: Freitag, 9. Dezember 2022 13:55 An: pgsql-sql <[email protected]> Betreff: Change detection A record shows all staff member worked in different departments. For instance, Tom was in sales for several years and got promoted to management. How to detect the time of this change? Data Staff_ID Name Department Year 1 Tom Sales 1990 2 Tom Sales 1991 3 Tom Sales 1991 4 Tom Management 1992 4 Tom Management 1992 Regards, David Attachments: [image/png] image001.png (1.9K, 3-image001.png) download | view image ^ permalink raw reply [nested|flat] 8+ messages in thread
* Re: Change detection @ 2022-12-09 13:06 Marcos Pegoraro <[email protected]> parent: Shaozhong SHI <[email protected]> 1 sibling, 2 replies; 8+ messages in thread From: Marcos Pegoraro @ 2022-12-09 13:06 UTC (permalink / raw) To: Shaozhong SHI <[email protected]>; +Cc: pgsql-sql <[email protected]> > > Data > > Staff_ID Name Department Year > 1 Tom Sales 1990 > 2 Tom Sales 1991 > 3 Tom Sales 1991 > 4 Tom Management 1992 > 4 Tom Management 1992 > > select *, coalesce(lag(department) over(order by year), department) <> department Changed from (Values (1, 'Tom', 'Sales', 1990),(2, 'Tom', 'Sales', 1991),(3, 'Tom', 'Sales', 1991),(4, 'Tom', 'Management', 1992),(4, 'Tom', 'Management', 1992)) as x(Staff_ID, Name, Department, Year); staff_id | name | department | year | changed ----------+------+------------+------+--------- 1 | Tom | Sales | 1990 | f 2 | Tom | Sales | 1991 | f 3 | Tom | Sales | 1991 | f 4 | Tom | Management | 1992 | t 4 | Tom | Management | 1992 | f (5 rows) ^ permalink raw reply [nested|flat] 8+ messages in thread
* Re: Change detection @ 2022-12-09 13:14 Shaozhong SHI <[email protected]> parent: Marcos Pegoraro <[email protected]> 1 sibling, 0 replies; 8+ messages in thread From: Shaozhong SHI @ 2022-12-09 13:14 UTC (permalink / raw) To: Marcos Pegoraro <[email protected]>; +Cc: pgsql-sql <[email protected]> Thanks, Marcos. It worked well. Regards, David On Fri, 9 Dec 2022 at 13:06, Marcos Pegoraro <[email protected]> wrote: > Data >> >> Staff_ID Name Department Year >> 1 Tom Sales 1990 >> 2 Tom Sales 1991 >> 3 Tom Sales 1991 >> 4 Tom Management 1992 >> 4 Tom Management 1992 >> >> select *, coalesce(lag(department) over(order by year), department) <> > department Changed from (Values (1, 'Tom', 'Sales', 1990),(2, 'Tom', > 'Sales', 1991),(3, 'Tom', 'Sales', 1991),(4, 'Tom', 'Management', 1992),(4, > 'Tom', 'Management', 1992)) as x(Staff_ID, Name, Department, Year); > staff_id | name | department | year | changed > ----------+------+------------+------+--------- > 1 | Tom | Sales | 1990 | f > 2 | Tom | Sales | 1991 | f > 3 | Tom | Sales | 1991 | f > 4 | Tom | Management | 1992 | t > 4 | Tom | Management | 1992 | f > (5 rows) > > ^ permalink raw reply [nested|flat] 8+ messages in thread
* Re: Change detection @ 2022-12-09 14:15 Shaozhong SHI <[email protected]> parent: Marcos Pegoraro <[email protected]> 1 sibling, 1 reply; 8+ messages in thread From: Shaozhong SHI @ 2022-12-09 14:15 UTC (permalink / raw) To: Marcos Pegoraro <[email protected]>; +Cc: pgsql-sql <[email protected]> How about finding all changes for all people in a large record set? See the follwoing: David 1 Tom Sales 1990 2 Tom Sales 1991 3 Tom Sales 1991 4 Tom Management 1992 5 Tom Management 1992 6 Tim finance 1982 7 Tim finance 1983 8 Tim management 1984 9 Tim management 1985 On Fri, 9 Dec 2022 at 13:06, Marcos Pegoraro <[email protected]> wrote: > Data >> >> Staff_ID Name Department Year >> 1 Tom Sales 1990 >> 2 Tom Sales 1991 >> 3 Tom Sales 1991 >> 4 Tom Management 1992 >> 4 Tom Management 1992 >> >> select *, coalesce(lag(department) over(order by year), department) <> > department Changed from (Values (1, 'Tom', 'Sales', 1990),(2, 'Tom', > 'Sales', 1991),(3, 'Tom', 'Sales', 1991),(4, 'Tom', 'Management', 1992),(4, > 'Tom', 'Management', 1992)) as x(Staff_ID, Name, Department, Year); > staff_id | name | department | year | changed > ----------+------+------------+------+--------- > 1 | Tom | Sales | 1990 | f > 2 | Tom | Sales | 1991 | f > 3 | Tom | Sales | 1991 | f > 4 | Tom | Management | 1992 | t > 4 | Tom | Management | 1992 | f > (5 rows) > > ^ permalink raw reply [nested|flat] 8+ messages in thread
* Re: Change detection @ 2022-12-09 17:00 Marcos Pegoraro <[email protected]> parent: Shaozhong SHI <[email protected]> 0 siblings, 1 reply; 8+ messages in thread From: Marcos Pegoraro @ 2022-12-09 17:00 UTC (permalink / raw) To: Shaozhong SHI <[email protected]>; +Cc: pgsql-sql <[email protected]> just change lag(department) over(order by year) to lag(department) over(partition by name order by year) Atenciosamente, Em sex., 9 de dez. de 2022 às 11:15, Shaozhong SHI <[email protected]> escreveu: > How about finding all changes for all people in a large record set? > > See the follwoing: > > David > > 1 Tom Sales 1990 > 2 Tom Sales 1991 > 3 Tom Sales 1991 > 4 Tom Management 1992 > 5 Tom Management 1992 > 6 Tim finance 1982 > 7 Tim finance 1983 > 8 Tim management 1984 > 9 Tim management 1985 > > On Fri, 9 Dec 2022 at 13:06, Marcos Pegoraro <[email protected]> wrote: > >> Data >>> >>> Staff_ID Name Department Year >>> 1 Tom Sales 1990 >>> 2 Tom Sales 1991 >>> 3 Tom Sales 1991 >>> 4 Tom Management 1992 >>> 4 Tom Management 1992 >>> >>> select *, coalesce(lag(department) over(order by year), department) <> >> department Changed from (Values (1, 'Tom', 'Sales', 1990),(2, 'Tom', >> 'Sales', 1991),(3, 'Tom', 'Sales', 1991),(4, 'Tom', 'Management', 1992),(4, >> 'Tom', 'Management', 1992)) as x(Staff_ID, Name, Department, Year); >> staff_id | name | department | year | changed >> ----------+------+------------+------+--------- >> 1 | Tom | Sales | 1990 | f >> 2 | Tom | Sales | 1991 | f >> 3 | Tom | Sales | 1991 | f >> 4 | Tom | Management | 1992 | t >> 4 | Tom | Management | 1992 | f >> (5 rows) >> >> > ^ permalink raw reply [nested|flat] 8+ messages in thread
* Re: Change detection @ 2022-12-09 19:32 Shaozhong SHI <[email protected]> parent: Marcos Pegoraro <[email protected]> 0 siblings, 1 reply; 8+ messages in thread From: Shaozhong SHI @ 2022-12-09 19:32 UTC (permalink / raw) To: Marcos Pegoraro <[email protected]>; +Cc: pgsql-sql <[email protected]> That works well. I just wonder whether we can tell Tom or Tim has worked in more than 1 department. Apparently, PostgreSQL does not allow count(distinct department) when window function is used. Given this data set, can we do something like count(distinct) to provide an answer to how many different department someone has worked in? Regards, David On Fri, 9 Dec 2022 at 17:00, Marcos Pegoraro <[email protected]> wrote: > just change lag(department) over(order by year) to lag(department) > over(partition by name order by year) > > Atenciosamente, > > > > > Em sex., 9 de dez. de 2022 às 11:15, Shaozhong SHI <[email protected]> > escreveu: > >> How about finding all changes for all people in a large record set? >> >> See the follwoing: >> >> David >> >> 1 Tom Sales 1990 >> 2 Tom Sales 1991 >> 3 Tom Sales 1991 >> 4 Tom Management 1992 >> 5 Tom Management 1992 >> 6 Tim finance 1982 >> 7 Tim finance 1983 >> 8 Tim management 1984 >> 9 Tim management 1985 >> >> On Fri, 9 Dec 2022 at 13:06, Marcos Pegoraro <[email protected]> wrote: >> >>> Data >>>> >>>> Staff_ID Name Department Year >>>> 1 Tom Sales 1990 >>>> 2 Tom Sales 1991 >>>> 3 Tom Sales 1991 >>>> 4 Tom Management 1992 >>>> 4 Tom Management 1992 >>>> >>>> select *, coalesce(lag(department) over(order by year), department) <> >>> department Changed from (Values (1, 'Tom', 'Sales', 1990),(2, 'Tom', >>> 'Sales', 1991),(3, 'Tom', 'Sales', 1991),(4, 'Tom', 'Management', 1992),(4, >>> 'Tom', 'Management', 1992)) as x(Staff_ID, Name, Department, Year); >>> staff_id | name | department | year | changed >>> ----------+------+------------+------+--------- >>> 1 | Tom | Sales | 1990 | f >>> 2 | Tom | Sales | 1991 | f >>> 3 | Tom | Sales | 1991 | f >>> 4 | Tom | Management | 1992 | t >>> 4 | Tom | Management | 1992 | f >>> (5 rows) >>> >>> >> ^ permalink raw reply [nested|flat] 8+ messages in thread
* Re: Change detection @ 2022-12-09 19:38 Rob Sargent <[email protected]> parent: Shaozhong SHI <[email protected]> 0 siblings, 0 replies; 8+ messages in thread From: Rob Sargent @ 2022-12-09 19:38 UTC (permalink / raw) To: [email protected] On 12/9/22 12:32, Shaozhong SHI wrote: > That works well. > > I just wonder whether we can tell Tom or Tim has worked in more than 1 > department. Apparently, PostgreSQL does not allow count(distinct > department) when window function is used. > > Given this data set, can we do something like count(distinct) to > provide an answer to how many different department someone has worked in? > > Regards, > > David Use the working query in a CTE and simply do the count(distinct department) on that, else there's no good place to put the number of departments per person. Pretty sure you've been asked before but Please don't top post. ^ permalink raw reply [nested|flat] 8+ messages in thread
end of thread, other threads:[~2022-12-09 19:38 UTC | newest] Thread overview: 8+ messages (download: mbox mbox.gz follow: Atom feed) -- links below jump to the message on this page -- 2022-12-09 12:55 Change detection Shaozhong SHI <[email protected]> 2022-12-09 13:02 ` AW: Change detection Stöcker, Martin <[email protected]> 2022-12-09 13:06 ` Marcos Pegoraro <[email protected]> 2022-12-09 13:14 ` Shaozhong SHI <[email protected]> 2022-12-09 14:15 ` Shaozhong SHI <[email protected]> 2022-12-09 17:00 ` Marcos Pegoraro <[email protected]> 2022-12-09 19:32 ` Shaozhong SHI <[email protected]> 2022-12-09 19:38 ` Rob Sargent <[email protected]>
This inbox is served by agora; see mirroring instructions for how to clone and mirror all data and code used for this inbox