oracle запрос на слияние строк в один - PullRequest
3 голосов
/ 14 января 2020

У меня есть таблица, в которой показаны in_time и out_time с номером шлюза в отдельной строке, но я хотел показать это в одной строке, поэтому мне нужно объединить обе строки в одну, вот результат для этого

   | EMPID |                IN_TIME |               OUT_TIME | IN_GATE | OUT_GATE |
   |-------|------------------------|------------------------|---------|----------|
   |   124 | 01-12-2019 08:49:35 AM |                        |       4 |          |
   |   124 |                        | 01-12-2019 06:53:03 PM |         |        5 |
   |   125 | 01-12-2019 07:49:35 AM |                        |       4 |          |
   |   125 |                        | 01-12-2019 05:53:03 PM |         |        5 |

Вот что я ожидал

   | EMPID |                IN_TIME |               OUT_TIME | IN_GATE | OUT_GATE |
   |-------|------------------------|------------------------|---------|----------|
   |   124 | 01-12-2019 08:49:35 AM | 01-12-2019 06:53:03 PM |       4 |        5 |
   |   125 | 01-12-2019 07:49:35 AM | 01-12-2019 05:53:03 PM |       4 |        5 |

Можете ли вы дать решение для этого

Ответы [ 4 ]

2 голосов
/ 14 января 2020

Вы можете попробовать с group by и aggregation

демо

select empid, max(in_time) as in_time,max(out_time) as out_time,
       max(in_gate) as in_gate,max(out_Gate) as out_Gate
from tablename
group by empid
1 голос
/ 14 января 2020

Нам нужно связать in раз с out раз для каждого сотрудника, что-то вроде следующего должно указать вам правильное направление:

См. Демонстрацию здесь: DB Fiddle

select in_rec.empid, in_rec.in_time, out_rec.out_time, in_rec.in_gate, out_rec.out_gate 
from emp_att in_rec
inner join (
  select empid, in_time, (select min(out_time) 
                        from emp_att 
                        where empid = e.empid and out_time > e.in_time) out_time
  from emp_att e
  where in_time is not null
) in_to_out on in_to_out.empid = in_rec.empid 
               and in_to_out.in_time = in_rec.in_time
inner join emp_att out_rec on out_rec.empid = in_to_out.empid 
                              and out_rec.out_time = in_to_out.out_time
0 голосов
/ 14 января 2020

Я вижу, что здесь нужно keep предложение.

Select emp_id, 
       Min(in_time) as in_time,
       Max(out_time) as out_time,
       Min(in_gate) keep (dense_rank first order by in_time) as in_gate,
       Max(out_gate) keep (dense_rank last order by out_time) as out_gate
  From your_table
Group by emp_id, trunc(in_time)

Ура !!

0 голосов
/ 14 января 2020
create table #temp (EMPID int,IN_TIME varchar(25),OUT_TIME varchar(25),IN_GATE 
int,OUT_GATE int)
insert into #temp values (124,'01-12-2019 08:49:35 AM',NULL,4,NULL)
insert into #temp values (124,NULL,'01-12-2019 06:53:03 PM',NULL,5)

select empid, max(in_time) as in_time,max(out_time) as out_time,
   max(in_gate) as in_gate,max(out_Gate) as out_Gate
   from #temp
   group by empid
drop table #temp
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...