如何从选择SQL查询ORACLE创建或更新表

时间:2015-07-19 00:21:11

标签: sql oracle

我有两张桌子:

tabledate:

enter image description here

holiday_date:

enter image description here

查询:

select (t.END_DATE - t.START_DATE) - count(c.HOLIDAY_DATE) - 2 * (to_char(t.END_DATE, 'WW') - to_char(t.START_DATE, 'WW')) as business_days, t.START_DATE,t.END_DATE
from tabledate t 
 left join
     holiday c
     on c.HOLIDAY_DATE between t.start_date and t.end_date and
       to_char(c.HOLIDAY_DATE, 'Dy', 'NLS_DATE_LANGUAGE = american' ) not in ('Sat', 'Sun')
group by t.end_date, t.start_date

我正在尝试更新此查询中的所有business_days行或创建另一个表。

例如:

enter image description here

1 个答案:

答案 0 :(得分:0)

如果您需要帮助编写带选择的UPDATE

基本理念:

UPDATE table_date
SET business_days = (SELECT expression1
                     FROM table_holiday
                     WHERE conditions)
WHERE conditions;

COMPLETE:

UPDATE table_date t
SET business_days = 
        SELECT 
           ( t.END_DATE - t.START_DATE) 
           - count(c.HOLIDAY_DATE) 
           - 2 * (to_char(t.END_DATE, 'WW') 
           - to_char(t.START_DATE, 'WW')) as business_days
        FROM 
           holiday c
        WHERE 
            c.HOLIDAY_DATE between t.start_date and t.end_date 
        AND to_char(c.HOLIDAY_DATE, 'Dy', 'NLS_DATE_LANGUAGE = american' ) 
            not in ('Sat', 'Sun')