oracle与cte的问题

hof1towb  于 2021-07-26  发布在  Java
关注(0)|答案(1)|浏览(531)

我已经实现了你的改变,并试图做更多。我的目标是对access\u history表进行更新,并为这些记录设置processed=1,从而创建emp\u考勤记录集。
当我试图找到最小值(ts)和最大值(ts)时,我得到以下错误
ora-00937:没有单个组功能
当我添加一个组时,我得到一个不同的错误。我发布了带有错误的代码,不知道您能否告诉我如何在同一个查询中完成删除错误并发布更新。

  1. -- Drop table emp_info purge:
  2. -- Drop table locations purge;
  3. -- Drop table access_histoty purge;
  4. -- Drop table emp_attendance purge;
  5. ALTER SESSION SET NLS_DATE_FORMAT = 'MMDDYYYY HH24:MI:SS';
  6. CREATE TABLE employees
  7. (
  8. employee_id NUMBER(6),
  9. first_name VARCHAR2(20),
  10. last_name VARCHAR2(25) NOT NULL,
  11. card_num varchar2(10) NOT NULL
  12. );
  13. ALTER TABLE employees
  14. ADD ( CONSTRAINT employee_id_pk
  15. PRIMARY KEY (employee_id));
  16. Insert into employees values (1, 'Mike', 'Jones', 'AAA1');
  17. Insert into employees values (2, 'Jane', 'Doe', 'BBB2');
  18. Insert into employees values (3, 'Paul', 'Smith', 'CCC3');
  19. Insert into employees values (4, 'John', 'Henry', 'DDD4');
  20. Create table locations(
  21. location_id NUMBER(4),
  22. location_name varchar2(30),
  23. location_type char(1));
  24. -- A=access T=Time & Attendance
  25. ALTER TABLE locations
  26. ADD ( CONSTRAINT lication_id_pk
  27. PRIMARY KEY (location_id));
  28. Insert into locations values (101, 'South Front Door 1', 'T');
  29. Insert into locations values (102, 'South Front Door 2', 'T');
  30. Insert into locations values (103, 'East Back Door 1', 'T');
  31. Insert into locations values (104, 'East Back Door 2', 'T');
  32. Insert into locations values (105,'Computer Room', 'A');
  33. Insert into locations values (106,'1st Floor North', 'A');
  34. Create table access_history(
  35. employee_id NUMBER(6),
  36. card_num varchar2(10),
  37. location_id number(4),
  38. Access_date date,
  39. ts timestamp default systimestamp,
  40. processed NUMBER(1) default 0
  41. );
  42. INSERT INTO access_history
  43. ( employee_id, card_num,
  44. location_id, Access_date )
  45. VALUES (1, 'AAA1', 101, TO_DATE('06212020 21:02:04', 'MMDDYYYY HH24:MI:SS'));
  46. -- TYpe T no previous data for this
  47. -- empid record INSERT empid,
  48. -- start time ONLY in table below
  49. -- and update last_start_date
  50. -- with DATETIME.
  51. INSERT INTO access_history
  52. ( employee_id, card_num,
  53. location_id, Access_date )
  54. VALUES (1, 'AAA1', 102, TO_DATE('06212020 23:52:14', 'MMDDYYYY HH24:MI:SS'));
  55. -- Type T record empid, start_time
  56. -- set update end_time only in
  57. -- emp_attendance.
  58. INSERT INTO access_history
  59. ( employee_id, card_num,
  60. location_id, Access_date )
  61. VALUES (2, 'BBB2', 103, TO_DATE('06212020 08:32:35', 'MMDDYYYY HH24:MI:SS'));
  62. -- TYpe T INSERT empid, start
  63. -- time ONLY in emp_attendance.
  64. -- update last_start_date with
  65. -- DATETIME on emp_info table
  66. INSERT INTO access_history
  67. ( employee_id, card_num,
  68. location_id, Access_date )
  69. VALUES (2, 'BBB2', 102, TO_DATE('06212020 15:39:05', 'MMDDYYYY HH24:MI:SS'));
  70. -- Type T record empid, start_time
  71. -- set, update end_time only in
  72. -- emp_attendance.
  73. INSERT INTO access_history
  74. ( employee_id, card_num,
  75. location_id, Access_date )
  76. VALUES (3, 'CCC3', 103, TO_DATE('06212020 15:39:05', 'MMDDYYYY HH24:MI:SS'));
  77. -- TYpe T INSERT empid, start
  78. -- time ONLY in emp_attendance.
  79. -- update last_start_date with
  80. -- DATETIME on emp_info table
  81. INSERT INTO access_history
  82. ( employee_id, card_num,
  83. location_id, Access_date )
  84. VALUES (3, 'CCC3', 105, TO_DATE('06212020 18:19:55', 'MMDDYYYY HH24:MI:SS'));
  85. -- Type A record don't do anything to
  86. -- emp_attendance.
  87. INSERT INTO access_history
  88. ( employee_id, card_num,
  89. location_id, Access_date )
  90. VALUES (4, 'DDD4', 102, TO_DATE('06212020 08:49:35', 'MMDDYYYY HH24:MI:SS'));
  91. -- single record with no pair. Set
  92. -- end_date to start_date if end_date
  93. -- is NULL
  94. INSERT INTO access_history
  95. ( employee_id, card_num,
  96. location_id, Access_date )
  97. VALUES (3, 'CCC3', 104, TO_DATE('06222020 04:04:35', 'MMDDYYYY HH24:MI:SS'));
  98. -- Type T record empid, start_time
  99. -- set, update end_time only in
  100. -- emp_attendance.
  101. CREATE TABLE emp_attendance
  102. (seq_num NUMBER(10),
  103. employee_id NUMBER(6),
  104. start_date DATE,
  105. end_date DATE,
  106. create_date DATE DEFAULT SYSDATE
  107. );
  108. Create sequence emp_attendance_seq;
  109. insert into emp_attendance.
  110. (seq_num,
  111. employee_id,
  112. start_date,
  113. end_date)
  114. with
  115. -- error capturing min,max ts
  116. --
  117. prep (min_ts, max_ts, employee_id, start_date, rn, end_date) as (
  118. select MIN(ts), MAX(ts), employee_id, access_date
  119. , row_number() over (partition by card_num order by access_date)
  120. , lead(access_date) over (partition by card_num order by access_date)
  121. from access_history
  122. where location_id in ( select location_id
  123. from locations
  124. where location_type = 'T'
  125. )
  126. AND processed = 0
  127. )
  128. select emp_attendance_seq.nextval,
  129. employee_id,
  130. start_date,
  131. nvl(end_date, start_date)
  132. from prep
  133. where mod(rn, 2) = 1;
1cklez4t

1cklez4t1#

在下面的cte中,我试图将select中的记录插入emp\ U ATSIMENT表,但我似乎无法工作。
如果你描述了你的尝试有什么不对劲,那么帮助就容易多了。因为,就目前的情况来看,它缺乏 INSERT INTO 语句、序列调用和 NVL 功能-至少,这是我理解的。

  1. SQL> insert into emp_attendance (seq_num, employee_id, start_date, end_Date)
  2. 2 with
  3. 3 prep (employee_id, start_date, rn, end_date) as (
  4. 4 select employee_id, access_date
  5. 5 , row_number() over (partition by card_num order by access_date)
  6. 6 , lead(access_date) over (partition by card_num order by access_date)
  7. 7 from access_history
  8. 8 where location_id in ( select location_id
  9. 9 from locations
  10. 10 where location_type = 'T'
  11. 11 )
  12. 12 )
  13. 13 select emp_attendance_seq.nextval,
  14. 14 employee_id,
  15. 15 start_date,
  16. 16 nvl(end_date, start_date)
  17. 17 from prep
  18. 18 where mod(rn, 2) = 1;
  19. 4 rows created.
  20. SQL> select * From emp_attendance;
  21. SEQ_NUM EMPLOYEE_ID START_DATE END_DATE
  22. ---------- ----------- ------------------- -------------------
  23. 1 1 21.06.2020 21:02:04 21.06.2020 23:52:14
  24. 2 2 21.06.2020 08:32:35 21.06.2020 15:39:05
  25. 3 3 21.06.2020 15:39:05 22.06.2020 04:04:35
  26. 4 4 21.06.2020 08:49:35 21.06.2020 08:49:35
  27. SQL>
展开查看全部

相关问题