我有下表
Emp_ID Emp_Name Insert_Time
001 AAA 12:00
002 BBB 12:00
002 BBB 12:00
003 CCC 12:00
004 DDD 12:00
004 DDD 12:00
现在,我想增加表本身中重复记录的Insert_Time并显示所有记录。
Emp_ID Emp_Name Insert_Time
001 AAA 12:00
002 BBB 12:01
002 BBB 12:02
003 CCC 12:00
004 DDD 12:01
004 DDD 12:02
如何执行此操作?。
答案 0 :(得分:1)
CREATE TABLE #Table111
([Emp_ID] int, [Emp_Name] varchar(3), [Insert_Time] varchar(5))
;
INSERT INTO #Table111
([Emp_ID], [Emp_Name], [Insert_Time])
VALUES
(001, 'AAA', '12:00'),
(002, 'BBB', '12:00'),
(002, 'BBB', '12:00'),
(003, 'CCC', '12:00'),
(004, 'DDD', '12:00'),
(004, 'DDD', '12:00')
;
select * from #Table111
WITH CTE AS
(
SELECT *,ROW_NUMBER() OVER (PARTITION BY [EMP_NAME],[EMP_ID] ORDER BY [EMP_ID]) AS RN FROM #TABLE111
)
UPDATE CTE
SET [INSERT_TIME]= STUFF([INSERT_TIME],4,5,RN)
输出
EMP_ID EMP_NAME INSERT_TIME
1 AAA 12:1
2 BBB 12:1
2 BBB 12:2
3 CCC 12:1
4 DDD 12:1
4 DDD 12:2
答案 1 :(得分:0)
您可以同时使用use count
和row_number
函数以获得所需的结果:
with tab(Emp_ID, Emp_Name, Insert_Time) as
(
select '001','AAA','12:00' from dual union all
select '002','BBB','12:00' from dual union all
select '002','BBB','12:00' from dual union all
select '003','CCC','12:00' from dual union all
select '004','DDD','12:00' from dual union all
select '004','DDD','12:00' from dual
)
select Emp_ID, Emp_Name,
to_char(to_date(Insert_Time,'hh24:mi') +rn/24/60,'hh24:mi') as Insert_Time
from
(
select Emp_ID, Emp_Name, Insert_Time,
count(1) over (partition by Emp_ID order by Emp_ID) cnt,
row_number() over (partition by Emp_ID order by Emp_ID) rn
from tab t
)
where cnt > 1;
EMP_ID EMP_NAME INSERT_TIME
002 BBB 12:01
002 BBB 12:02
004 DDD 12:01
004 DDD 12:02
要根据您的评论更新记录,请使用:
update tab t
set t.insert_time= (
select to_char(to_date(Insert_Time,'hh24:mi') +rn/24/60,'hh24:mi') as Insert_Time
from
(
select Emp_ID, Emp_Name, Insert_Time,
count(1) over (partition by Emp_ID order by Emp_ID) cnt,
row_number() over (partition by Emp_ID order by Emp_ID) rn
from tab t
) q
where cnt > 1
and t.Emp_ID = q.Emp_ID
and t.Emp_Name = q.Emp_Name
and t.rowid = q.rowid
);
获取:
select * from tab;
EMP_ID EMP_NAME INSERT_TIME
001 AAA
002 BBB 12:01
002 BBB 12:02
003 CCC
004 DDD 12:01
004 DDD 12:02
答案 2 :(得分:0)
假设您有一个如下表:
create table yourTable(Emp_ID, Emp_Name, Insert_Time) as (
select '001', 'AAA', to_date('01/01/0001 12:00', 'dd/mm/yyyy hh24:mi') from dual union all
select '002', 'BBB', to_date('01/01/0001 12:00', 'dd/mm/yyyy hh24:mi') from dual union all
select '002', 'BBB', to_date('01/01/0001 12:00', 'dd/mm/yyyy hh24:mi') from dual union all
select '003', 'CCC', to_date('01/01/0001 12:00', 'dd/mm/yyyy hh24:mi') from dual union all
select '004', 'DDD', to_date('01/01/0001 12:00', 'dd/mm/yyyy hh24:mi') from dual union all
select '004', 'DDD', to_date('01/01/0001 12:00', 'dd/mm/yyyy hh24:mi') from dual
)
您可以使用:
select Emp_ID, Emp_Name,
Insert_Time +
row_number() over (partition by Emp_ID, Emp_Name order by 1) -- the row number
* ( count(*) over (partition by Emp_ID, Emp_Name ) -1 ) -- to decide whether to add a minute or not
/ 24 / 60 -- to get minutes
as Insert_Time
from yourTable
结果:
001 AAA 01/01/0001 12:00:00
002 BBB 01/01/0001 12:01:00
002 BBB 01/01/0001 12:02:00
003 CCC 01/01/0001 12:00:00
004 DDD 01/01/0001 12:01:00
004 DDD 01/01/0001 12:02:00
要更新表格,您可以使用MERGE
:
merge into yourTable T
using (
select rowid, Emp_ID, Emp_Name,
Insert_Time +
row_number() over (partition by Emp_ID, Emp_Name order by 1) -- the row number
* ( count(*) over (partition by Emp_ID, Emp_Name ) -1 ) -- to decide wheter to add a minute or not
/ 24 / 60 as Insert_Time
from yourTable
) X
on (t.rowid = x.rowid)
when matched then
update set t.Insert_Time = x.Insert_Time