Добавить строки без вставки в - PullRequest
0 голосов
/ 05 ноября 2019

Вопрос в том, что ..

У меня есть таблица с именами "table_1", и в таблице записывается количество производств в час.

Но в таблице не было строкис нулевым или нулевым производством, как в приведенной ниже таблице.

table_1
-------------------------------------
|  column  |  hour   |  productions |
-------------------------------------
|    1     |    00   |      10      |
|    2     |    02   |      15      |
|    3     |    04   |      18      |
|    4     |    05   |       1      |
|    5     |    12   |      15      |
|    6     |    15   |      13      |
|    7     |    21   |      16      |
|    8     |    23   |       2      |
-------------------------------------

Есть ли в любом случае возможность выбрать строки без "вставки в" из таблицы_1, и результат, как показано ниже:

-------------------------------------
|  column  |  hour   |  productions |
-------------------------------------
|    1     |    00   |      10      |
|    2     |    01   |       0      |
|    3     |    02   |      15      |
|    4     |    03   |       0      |
|    5     |    04   |      18      |
|    6     |    05   |       1      |
|    7     |    06   |       0      |
|    8     |    07   |       0      |
|    9     |    08   |       0      |
|    10    |    09   |       0      |
|    11    |    10   |       0      |
|    12    |    11   |       0      |
|    13    |    12   |      15      |
|    14    |    13   |       0      |
|    15    |    14   |       0      |
|    16    |    15   |      13      |
|    17    |    16   |       0      |
|    18    |    17   |       0      |
|    19    |    18   |       0      |
|    20    |    19   |       0      |
|    21    |    20   |       0      |
|    22    |    21   |      16      |
|    23    |    22   |       0      |
|    24    |    23   |       2      |

Спасибомного.

Ответы [ 3 ]

1 голос
/ 05 ноября 2019

Вы можете выбрать все часы из рекурсивного подзапроса и затем присоединить к нему свою таблицу:

select 
    num_hour as hour,
    nvl(productions, 0) as productions
from
    (select  level-1 as num_hour from dual connect by level <= 24 order by level) t2
left join 
    table_1 t1
on 
    num_hour = to_number(hour)
order by num_hour
0 голосов
/ 05 ноября 2019

Если вы создадите календарь (строки 12-16) и внешне присоедините его к текущим данным, то вы получите

SQL> with
  2  table_1 (col, hour, productions) as
  3    (select 1, '00', 10 from dual union all
  4     select 2, '02', 15 from dual union all
  5     select 3, '04', 18 from dual union all
  6     select 4, '05',  1 from dual union all
  7     select 5, '12',  1 from dual union all
  8     select 6, '15', 13 from dual union all
  9     select 7, '21', 16 from dual union all
 10     select 8, '23',  2 from dual
 11    ),
 12  cal as
 13    (select lpad(to_char(0 + level - 1), 2, '0') hour
 14     from dual
 15     connect by level <= 24
 16    )
 17  select row_number() over (order by c.hour) col,
 18         c.hour,
 19         nvl(t.productions, 0) productions
 20  from cal c left join table_1 t on c.hour = t.hour
 21  order by c.hour;

       COL HO PRODUCTIONS
---------- -- -----------
         1 00          10
         2 01           0
         3 02          15
         4 03           0
         5 04          18
         6 05           1
         7 06           0
         8 07           0
         9 08           0
        10 09           0
        11 10           0
        12 11           0
        13 12           1
        14 13           0
        15 14           0
        16 15          13
        17 16           0
        18 17           0
        19 18           0
        20 19           0
        21 20           0
        22 21          16
        23 22           0
        24 23           2

24 rows selected.

SQL>
0 голосов
/ 05 ноября 2019

Вам нужно начать со списка часов. Вы можете поместить это непосредственно в запрос, используя производную таблицу:

select h.hour, coalesce(sum(t1.productions), 0) as productions
from (select '00' as hour from dual union all
      select '01' as hour from dual union all
      . . .
      select '23' as hour from dual
     ) h left join
     table_1 t1
     on t1.hour = h.hour
group by h.hour
order by h.hour;
...