简体   繁体   中英

how to join 2 tables in SQL oracle

please help me, I want to join from skyteam1 (ID 1, comno 1, ename Boeing737, country USA) into comm1 (id 1, comno (all), ename (all), ddate(all), SUM (total price) of the aircraft

SQL> SELECT * FROM SKYTEAM1;

    ID;       COMNO;     ENAME;              COUNTRY;

     1          1       Boeing737              USA
     2          2       Boeing777              USA
     3          3       AirBus320              RUSSIA

SQL> SELECT * FROM COMM1;

    ID;     COMNO; ENAME;      DDATE;           SAL;      DEPTNO;

     1        1 Engine        20.02.81         900          20
     1        2 Wings         22.02.81        35000         30
     1        3 Battery       09.06.81        84800         20
     1        4 APU           17.11.81        8400          10
     1        5 EECU          03.12.81        8400          20
     1        6 Generator     23.01.82        40000         20

EXAMPLE; I need this ANSWER

    ID;   COMNO;   ENAME;   ENAME;      DDATE;           SAL;     SUM_TOTAL;

     1       1    Boeing737  Engine        20.02.81         900          
             2               Wings         22.02.81        35000         
             3               Battery       09.06.81        84800         
             4               APU           17.11.81        8400          
             5               EECU          03.12.81        8400          
             6               Generator     23.01.82        40000      17750;

Here's an example:

SQL> with skyteam1 (id, comno, ename) as
  2    (select 1, 1, 'boeing 737' from dual union all
  3     select 2, 2, 'boeing 777' from dual union all
  4     select 3, 3, 'airbus 320' from dual
  5    ),
  6  comm1 (id, comno, ename, ddate, sal) as
  7    (select 1, 1, 'engine',    '20.02.81', 900   from dual union all
  8     select 1, 2, 'wings',     '22.02.81', 35000 from dual union all
  9     select 1, 3, 'battery',   '09.06.81', 84800 from dual union all
 10     select 1, 4, 'apu',       '17.11.81', 8400  from dual union all
 11     select 1, 5, 'eecu',      '03.12.81', 8400  from dual union all
 12     select 1, 6, 'generator', '23.01.81', 40000 from dual
 13    )
 14  select c.id,
 15         c.comno,
 16         s.ename,
 17         c.ename,
 18         c.ddate,
 19         c.sal,
 20         sum(c.sal) over (order by null) total
 21  from skyteam1 s join comm1 c on c.id = s.id
 22  where s.id = 1
 23  order by c.comno;

        ID      COMNO ENAME      ENAME     DDATE           SAL      TOTAL
---------- ---------- ---------- --------- -------- ---------- ----------
         1          1 boeing 737 engine    20.02.81        900     177500
         1          2 boeing 737 wings     22.02.81      35000     177500
         1          3 boeing 737 battery   09.06.81      84800     177500
         1          4 boeing 737 apu       17.11.81       8400     177500
         1          5 boeing 737 eecu      03.12.81       8400     177500
         1          6 boeing 737 generator 23.01.81      40000     177500

6 rows selected.

SQL>

Depending on a tool you use, it can be made prettier. For example, in SQL*Plus:

SQL> break on id on s_ename
SQL> compute sum of sal on id
SQL>
SQL> with skyteam1 (id, comno, ename) as
  2    (select 1, 1, 'boeing 737' from dual union all
  3     select 2, 2, 'boeing 777' from dual union all
  4     select 3, 3, 'airbus 320' from dual
  5    ),
  6  comm1 (id, comno, ename, ddate, sal) as
  7    (select 1, 1, 'engine',    '20.02.81', 900   from dual union all
  8     select 1, 2, 'wings',     '22.02.81', 35000 from dual union all
  9     select 1, 3, 'battery',   '09.06.81', 84800 from dual union all
 10     select 1, 4, 'apu',       '17.11.81', 8400  from dual union all
 11     select 1, 5, 'eecu',      '03.12.81', 8400  from dual union all
 12     select 1, 6, 'generator', '23.01.81', 40000 from dual
 13    )
 14  select c.id,
 15         c.comno,
 16         s.ename s_ename,
 17         c.ename c_ename,
 18         c.ddate,
 19         c.sal
 20  from skyteam1 s join comm1 c on c.id = s.id
 21  where s.id = 1
 22  order by c.comno;

        ID      COMNO S_ENAME    C_ENAME   DDATE           SAL
---------- ---------- ---------- --------- -------- ----------
         1          1 boeing 737 engine    20.02.81        900
                    2            wings     22.02.81      35000
                    3            battery   09.06.81      84800
                    4            apu       17.11.81       8400
                    5            eecu      03.12.81       8400
                    6            generator 23.01.81      40000
**********            **********                    ----------
sum                                                     177500

6 rows selected.

SQL>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM