简体   繁体   English

根据从时间戳和其他列值派生的日期将同一表的两行合并为单行

[英]Combine two rows of same table as single row based upon date derived from timestamp and other columns values

I have a table in postgresql table which contains IN / OUT timestamp of several employees.我在 postgresql 表中有一个表,其中包含多个员工的 IN / OUT 时间戳。

I want to derive a table with IN and OUT timestamp of the day for each employee in same rows with null value if the OUT timestamp does not exist for that particular day.如果特定日期的 OUT 时间戳不存在,我想为具有 null 值的同一行中的每个员工派生一个包含当天 IN 和 OUT 时间戳的表。

CREATE TABLE employee ( id bigint PRIMARY KEY, date_time timestamp, type varchar, name varchar); CREATE TABLE employee (id bigint PRIMARY KEY, date_time timestamp, type varchar, name varchar);

The table entries are as follows:

 id |      date_time      | type |   name    
----+---------------------+------+-----------
  1 | 2022-12-01 09:00:00 | IN   | emp1
  2 | 2022-12-01 09:00:00 | IN   | emp2
  3 | 2022-12-01 10:00:00 | OUT  | emp1
  4 | 2022-12-01 11:00:00 | OUT  | emp2
  5 | 2022-12-02 09:30:00 | IN   | emp1
  6 | 2022-12-02 09:15:00 | IN   | emp2
  7 | 2022-12-02 10:30:00 | OUT  | emp1

Final Output should be :

       in_time       |      out_time       |   name    
---------------------+---------------------+-----------
 2022-12-01 09:00:00 | 2022-12-01 10:00:00 | emp1
 2022-12-01 09:00:00 | 2022-12-01 11:00:00 | emp2
 2022-12-02 09:30:00 | 2022-12-02 10:30:00 | emp1
 2022-12-02 09:15:00 |                     | emp2

One of my attempted solution is as follows:我尝试的解决方案之一如下:

select a.date_time as in_time, b.date_time as out_time, a.name 
from 
(select * from customers where type='IN') a
left join 
(select * from customers where type='OUT') b
on a.date_time::date=b.date_time::date and a.name=b.name;

I am looking for a more better solution (less time consuming) of above mentioned problem.我正在寻找上述问题的更好解决方案(耗时更少)。

You could use row_number function to define groups for (IN, OUT) for each employee as the following:您可以使用 row_number function 为每个员工定义 (IN, OUT) 的组,如下所示:

select max(date_time) filter (where type='IN') as in_time,
       max(date_time) filter (where type='OUT') as out_time,
       name
from
(
  select *,
    row_number() over (partition by name order by date_time) as grp
  from table_name
) T
group by name, (grp-1)/2
order by name, in_time

group by name, (grp-1)/2 will group every two consecutive rows for an employee together. group by name, (grp-1)/2会将员工的每两个连续行分组在一起。

See demo看演示

Try this:尝试这个:

SELECT Max(date_time) FILTER (WHERE type = 'IN') AS in_time
     , Max(date_time) FILTER (WHERE type = 'OUT') AS out_time
     , name
  FROM customers
 GROUP BY name

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

相关问题 如何根据其他列中的值(对于同一行)更新表的所有行中的列? - How do I update a column in all rows of a table based on values in other columns (for the same row)? SQL-根据两行的派生值将两行合并为一 - SQL - Combine Two Rows Into One, Based Upon a Derived Value of Both Rows 如何根据创建的时间戳将最接近的两行合并为一行 - How to combine two closest rows into one row based on the created timestamp 如何将两个表(具有相同的模式)组合成一个具有不同值的表,并指出每行来自哪个表? - How to combine two tables (with same schema) into a single table with distinct values and indicating which table each row came from? 基于两列值选择单行 - Selecting a single rows based on two columns values 将两个表与日期显示中的多行合并为另一表中的一行 - Combine two tables with multiple rows in a date show into one row in other table 将具有相同名称的所有行组合成一个具有多列值的行 - Combine all rows with the same name into a single row with multiple columns for the value 根据同一张表中的两行检索一行 - Retrieve one row based on two rows from the same table 将2个日期行合并为1行中的2列 - Combine 2 Date Rows To 2 Columns In 1 Row PHP - SQL:将两行合并为多列的单行 - PHP - SQL: Combine two rows into single row with multiple columns
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM