更新或选择进入ORACLE

时间:2012-03-22 03:47:06

标签: sql oracle

我使用以下声明;

SELECT RESV_ID, BOOKING_CUS_ID, ACC_ID,
    (SELECT F.FLI_PRICE FROM FLIGHT F WHERE F.FLI_ID = R.IN_FLIGHT_ID) AS DEPART_FLIGHT_PRICE, 
    (SELECT F1.FLI_PRICE FROM FLIGHT F1 WHERE F1.FLI_ID = R.OUT_FLIGHT_ID) AS RETURN_FLIGHT_PRICE, 
    (SELECT AC.ACC_PRICEPN FROM ACCOMMODATION AC WHERE AC.ACC_ID = R.ACC_ID) AS ACCOMMODATION_PRICE
    FROM HOLIDAY_RESERVATION R;

产生以下结果;

   RESV_ID BOOKING_CUS_ID     ACC_ID DEPART_FLIGHT_PRICE RETURN_FLIGHT_PRICE ACCOMMODATION_PRICE
---------- -------------- ---------- ------------------- ------------------- -------------------
         1              1          2                 520                 450                 350
         2              3          4                 250                 150                 150
         3              5          6                 290                 300                 450
         4              7          7                 399                 450                 650
         5              9                            365                 345
         6             11                            558                 460
         7             13                            250                 250
         8             15                            550                 550
         9             17         25                                                         250
        10             19         19                                                         450

10 rows selected.

问题: 我如何总结价格字段,某些价格无法提供因为预订仅限住宿或仅限航班,因此两个值都不会一直存在,这就是问题所在< / p>

DEPART_FLIGHT_PRICE RETURN_FLIGHT_PRICE ACCOMMODATION_PRICE

此外: 我希望将这三个值的SUM插入或更新到预订表的SUBTOTAL中,也许通过使用select into或update,我花了一整天的时间尝试这样做,但我的技能有限。任何帮助将不胜感激。

航班表

FLI_ID FLI_CO FLI_AIRCRA DEPT_AIRPORT ARRV_AIRPORT DEPT_TIME                      ARRV_TIME               FLI_PRICE

     1 BD425  Boeing 707            1           12 18-MAR-12 02.24.00 AM          18-MAR-12 06.24.00 AM         520
     2 LX345  Beriev 30             6            7 20-MAR-12 03.30.00 PM          20-MAR-12 04.20.00 PM         250
     3 NZ4445 Boeing 720            9           14 25-MAR-12 09.00.00 AM          25-MAR-12 05.00.00 PM         290
     4 TP351  Boeing 767           10           15 25-MAR-12 11.25.00 AM          25-MAR-12 03.35.00 PM         399
     5 BA472  Boeing 720            5           14 26-MAR-12 01.05.00 PM          26-MAR-12 04.15.00 PM         365

住宿

ACC_ID ACC_TYPE_CODE ACC_DESC                                           ACC_PRICEPN  ACC_ROOMS  RESORT_ID ACC_ADDR                          CITY_ID

     1             1 Three bedroom bungalow near theme park                     500          3          1
     2             1 Two bedroom bungalow next to disney house                  350          2          1
     3             1 One bedroom bungalow with lake view                        250          2          2
     4             2 One bedroom chalet near the lake                           150          1          2
     5             2 Four bedroom chalet near the tree house                    600          4          3

预订

RESV_ID     EMP_ID BOOKING_CUS_ID RESV_DATE HOLIDAY_S HOLIDAY_E IN_FLIGHT_ID OUT_FLIGHT_ID IN_FLIGHT_SEATS_NO OUT_FLIGHT_SEATS_NO     ACC_ID   SUBTOTAL

     1        338              1 16-FEB-12 18-MAR-12 20-APR-12            1            11                  2                   2          2
     2        335              3 10-JAN-12 20-MAR-12 22-APR-12            2            12                  2                   2          4
     3        338              5 05-MAR-12 25-MAR-12 26-APR-12            3            13                  2                   2          6
     4        328              7 02-JAN-12 25-MAR-12 25-APR-12            4            14                  2                   2          7
     5        311              9 20-JAN-12 26-MAR-12 21-APR-12            5            15                  2                   2
     6        317             11 07-JAN-12 27-MAR-12 22-APR-12            6            16                  2                   2
     7        344             13 29-FEB-12 15-MAR-12 12-APR-12            7            17                  2                   2
     8        326             15 11-JAN-12 18-MAR-12 12-APR-14            8            18                  2                   2
     9        329             17 16-JAN-12 19-MAR-12 17-APR-12                                                                   25
    10        323             19 18-FEB-12 20-MAR-12 21-APR-12                                                                   19

好的,我设法得到了我想要的结果

SELECT HR.RESV_ID, F_IN.FLI_ID, F_IN.FLI_PRICE, F_OUT.FLI_ID, F_OUT.FLI_PRICE, AC.ACC_ID, AC.ACC_PRICEPN, NVL(F_IN.FLI_PRICE,0)+NVL(F_OUT.FLI_PRICE,0)+NVL(AC.ACC_PRICEPN,0) AS TOTAL
FROM HOLIDAY_RESERVATION HR 
LEFT JOIN FLIGHT F_IN ON HR.IN_FLIGHT_ID = F_IN.FLI_ID
LEFT JOIN FLIGHT F_OUT ON HR.OUT_FLIGHT_ID = F_OUT.FLI_ID
LEFT JOIN ACCOMMODATION AC ON HR.ACC_ID = AC.ACC_ID
ORDER BY HR.RESV_ID;

产额

   RESV_ID     FLI_ID  FLI_PRICE     FLI_ID  FLI_PRICE     ACC_ID ACC_PRICEPN      TOTAL
---------- ---------- ---------- ---------- ---------- ---------- ----------- ----------
         1          1        500         11        555          2         350       1405
         2          2        150         12        253          4         150        553
         3          3        300         13        345          6         450       1095
         4          4        450         14        343          7         650       1443
         5          5        345         15        242                               587
         6          6        460         16        460                               920
         7          7        250         17        250                               500
         8          8        550         18        550                              1100
         9                                                     25         250        250
        10                                                     19         450        450

以下声明是更新预订表。 感谢来自DBA stackxchange的Leigh Riffel提供以下代码

UPDATE HOLIDAY_RESERVATION R SET SUBTOTAL = 
   NVL((SELECT F.FLI_PRICE FROM FLIGHT F WHERE F.FLI_ID = R.IN_FLIGHT_ID), 0) +
   NVL((SELECT F.FLI_PRICE FROM FLIGHT F WHERE F.FLI_ID = R.OUT_FLIGHT_ID), 0) +
   NVL((SELECT AC.ACC_PRICEPN FROM ACCOMMODATION AC WHERE AC.ACC_ID = R.ACC_ID), 0);

现在,使用从上面执行的总和中获得的值填充小计&gt;&gt;

 RESV_ID     EMP_ID BOOKING_CUS_ID RESV_DATE HOLIDAY_S HOLIDAY_E IN_FLIGHT_ID OUT_FLIGHT_ID IN_FLIGHT_SEATS_NO OUT_FLIGHT_SEATS_NO     ACC_ID   SUBTOTAL
---------- ---------- -------------- --------- --------- --------- ------------ ------------- ------------------ ------------------- ---------- ----------
         1        338              1 16-FEB-12 18-MAR-12 20-APR-12            1            11                  2                   2          2       1405
         2        335              3 10-JAN-12 20-MAR-12 22-APR-12            2            12                  2                   2          4        553
         3        338              5 05-MAR-12 25-MAR-12 26-APR-12            3            13                  2                   2          6       1095
         4        328              7 02-JAN-12 25-MAR-12 25-APR-12            4            14                  2                   2          7       1443
         5        311              9 20-JAN-12 26-MAR-12 21-APR-12            5            15                  2                   2                   587
         6        317             11 07-JAN-12 27-MAR-12 22-APR-12            6            16                  2                   2                   920
         7        344             13 29-FEB-12 15-MAR-12 12-APR-12            7            17                  2                   2                   500
         8        326             15 11-JAN-12 18-MAR-12 12-APR-14            8            18                  2                   2                  1100
         9        329             17 16-JAN-12 19-MAR-12 17-APR-12                                                                   25        250
        10        323             19 18-FEB-12 20-MAR-12 21-APR-12                                                                   19        450

随后,代码被添加到触发器(这是最初的意图)

CREATE OR REPLACE TRIGGER HR_SUBTOTAL
BEFORE INSERT OR UPDATE ON HOLIDAY_RESERVATION
FOR EACH ROW
BEGIN
   SELECT 
      NVL((SELECT F.Fli_Price FROM Flight F WHERE F.Fli_ID = :new.In_Flight_ID), 0) +
      NVL((SELECT F.Fli_Price FROM Flight F WHERE F.Fli_ID = :new.Out_Flight_ID), 0) +
      NVL((SELECT AC.Acc_PricePn FROM Accomodation AC WHERE AC.Acc_ID = :new.Acc_ID), 0)
      INTO :new.Subtotal
   FROM dual;
END;
/

2 个答案:

答案 0 :(得分:2)

对于SUM,假设您希望将NULL值视为0,则只需要对数字NVL进行操作

NVL( DEPART_FLIGHT_PRICE, 0 ) +
NVL( RETURN_FLIGHT_PRICE, 0 ) +
NVL( ACCOMMODATION_PRICE, 0 )

至于UPDATE,听起来你只需要一个相关的UPDATE语句。

UPDATE reservation r
   SET subtotal = (SELECT (SELECT NVL( DEPART_FLIGHT_PRICE, 0 ) +
                          NVL( RETURN_FLIGHT_PRICE, 0 ) +
                          NVL( ACCOMMODATION_PRICE, 0 )
                     FROM (SELECT RESV_ID, 
                                  BOOKING_CUS_ID, 
                                  ACC_ID,
                                  (SELECT F.FLI_PRICE 
                                     FROM FLIGHT F 
                                    WHERE F.FLI_ID = R.IN_FLIGHT_ID) AS DEPART_FLIGHT_PRICE, 
                                  (SELECT F1.FLI_PRICE 
                                     FROM FLIGHT F1 
                                    WHERE F1.FLI_ID = R.OUT_FLIGHT_ID) AS RETURN_FLIGHT_PRICE, 
                                  (SELECT AC.ACC_PRICEPN 
                                     FROM ACCOMMODATION AC 
                                    WHERE AC.ACC_ID = R.ACC_ID) AS ACCOMMODATION_PRICE
                             FROM dual));

答案 1 :(得分:1)

你在问:

  

如何总结价格字段,因为您可以看到其中一些字段可以为空。

DEPART_FLIGHT_PRICE RETURN_FLIGHT_PRICE ACCOMMODATION_PRICE

将它们包含在NVL函数中,如下所示:

NVL(DEPART_FLIGHT_PRICE, 0)

然后将它们总结起来。

对于第二部分,您需要的是MERGE语句。一个很好的例子是http://www.oracle-developer.net/display.php?id=203