添加行而不插入

问题是..

我有一个表,名称为“ table_1”,该表记录了每小时的生产数量。

但是表中没有像下面的表那样具有0或null的行。

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      |
-------------------------------------

是否有必要从table_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      |

非常感谢。

avagrace 回答:添加行而不插入

您可以从递归子查询中选择所有时间,然后将表加入其中:

print_inner_date
,

您需要从小时列表开始。您可以使用派生表将其直接放在查询中:

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;
,

如果创建一个 calendar (第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>
本文链接:https://www.f2er.com/3159485.html

大家都在问