根据日期插入子查询日期

时间:2016-06-12 04:37:53

标签: date plsql oracle11g subquery insert-select

我想基于它来插入子查询日期。另外,每个日期只能使用四次。一旦达到第四次,第五个值将使用同一天的另一个日期。换句话说,使用下周一的星期日。示例Monday 6 JUNE 2016Monday 13 JUNE 2016(您可以查看日历)。

我询问是否根据presentationdatestartpresentationdateend来自presentation表格获取日期列表:

select   a.presentationid,
         a.presentationday,
         to_char (a.presentationdatestart + delta, 'DD-MM-YYYY', 'NLS_CALENDAR=GREGORIAN') list_date
from     presentation a,
         (select     level - 1 as delta
          from       dual
          connect by level - 1 <= (select max (presentationdateend - presentationdatestart)
                                   from   presentation))
where    a.presentationdatestart + delta <= a.presentationdateend
and      a.presentationday = to_char(a.presentationdatestart + delta, 'fmDay')
order by a.presentationdatestart + delta,
         a.presentationid; --IMPORTANT!!!--

例如,

presentationday    presentationdatestart    presentationdateend
     Monday              01-05-2016             04-06-2016
    Tuesday              01-05-2016             04-06-2016
   Wednesday             01-05-2016             04-06-2016
   Thursday              01-05-2016             04-06-2016

查询结果将列出01-05-201604-06-2016之间的所有可能日期:

Monday  02-05-2016
Tuesday 03-05-2016
Wednesday   04-05-2016
Thursday    05-05-2016
....
Monday      30-05-2016
Tuesday     31-05-2016
Wednesday   01-06-2016
Thursday    02-06-2016 (20 rows)

这是我的INSERT查询:

insert into CSP600_SCHEDULE (studentID,
                             studentName, 
                             projectTitle,
                             supervisorID,
                             supervisorName,
                             examinerID,
                             examinerName,
                             exavailableID,
                             availableday,
                             availablestart,
                             availableend,
                             availabledate)   
select  '2013816591',
        'mong',
        'abc',
        '1004',
        'Sue',
        '1002',
        'hazlifah',
          2,
        'Monday', //BASED ON THIS DAY
        '12:00:00',
        '2:00:00',
         to_char (a.presentationdatestart + delta, 'DD-MM-YYYY', 'NLS_CALENDAR=GREGORIAN') list_date //FOR AVAILABLEDATE
from     presentation a,
         (select     level - 1 as delta
          from       dual
          connect by level - 1 <= (select max (presentationdateend - presentationdatestart)
                                   from   presentation))
where    a.presentationdatestart + delta <= a.presentationdateend
and      a.presentationday = to_char(a.presentationdatestart + delta, 'fmDay')
order by a.presentationdatestart + delta,
         a.presentationid;

此查询成功添加了20行,因为所有可能的日期都是20行。我想修改查询以便能够根据availableDay插入,并且每个日期只能为每个不同的studentID使用四次。

CSP600_SCHEDULE中的可能结果(我删除了不相关的列以提高可读性):

StudentID   StudentName     availableDay      availableDate
  2013         abc             Monday           01-05-2016
  2014         def             Monday           01-05-2016
  2015         ghi             Monday           01-05-2016
  2016         klm             Monday           01-05-2016
  2010         nop            Tuesday           02-05-2016
  2017         qrs            Tuesday           02-05-2016
  2018         tuv            Tuesday           02-05-2016
  2019         wxy            Tuesday           02-05-2016
  .....
  2039         rrr             Monday           09-05-2016
  ..... 

您可以查看日历:)

1 个答案:

答案 0 :(得分:0)

认为您要求的是列出您的学生,然后以4人为一组进行批处理 - 然后将每个批次分配到某个日期。是吗?

在这种情况下,这样的事情应该有效(我使用表格列表作为学生姓名,因此我不需要将任何数据插入自定义表格中):

WITH students AS
(SELECT table_name 
 FROM all_tables
 WHERE rownum < 100
 )
SELECT 
 table_name
,SYSDATE + (CEIL(rownum/4) -1)
FROM
 students
;

我希望能帮到你

...好的,根据您的意见,我认为这可能是一个更好的解决方案:

WITH students AS
(SELECT table_name student_name
 FROM all_tables
 WHERE rownum < 100
 )
, dates AS
(SELECT TRUNC(sysdate) appointment_date from dual UNION
 SELECT TRUNC(sysdate+2) from dual UNION
 SELECT TRUNC(sysdate+4) from dual UNION
 SELECT TRUNC(sysdate+6) from dual UNION
 SELECT TRUNC(sysdate+8) from dual UNION
 SELECT TRUNC(sysdate+10) from dual UNION
 SELECT TRUNC(sysdate+12) from dual UNION
 SELECT TRUNC(sysdate+14) from dual 
)
SELECT 
 s.student_name
,d.appointment_date
FROM
 --get a list of students each with a sequential row number, ordered by student name 
 (SELECT
   student_name
  ,ROW_NUMBER() OVER (ORDER BY student_name) rn
 FROM students
 )     s
 --get a list of available dates with a sequential row number, ordered by date
,(SELECT
   appointment_date
  ,ROW_NUMBER() OVER (ORDER BY appointment_date) rn
 FROM dates
 ) d
WHERE 1=1
--allocate the first four students to date rownumber1, next four students to date rownumber 2...
AND CEIL(s.rn/4) = d.rn 
;