在sql查询中分配特定值

时间:2014-03-11 14:27:58

标签: sql oracle

我有一个像这样的SQL:

 SELECT temp.inst_id,
       ' ',
       gv.instance_name,
       gv.host_name,
       CASE WHEN component IS NULL THEN 'others' ELSE component END
          component,
       ROUND (SUM (used_mb), 1) used_mb
  FROM (SELECT inst_id,
               CASE
                  WHEN name = 'buffer_cache' THEN 'db_buffer_cache'
                  WHEN name = 'log_buffer' THEN 'log_buffer'
                  ELSE pool
               END
                  component,
               CASE
                  WHEN name = 'buffer_cache'
                  THEN
                     (  (  (  bytes
                            -   (SELECT COUNT (*)
                                   FROM gv$bh
                                  WHERE     inst_id = x.inst_id
                                        AND status = 'free')
                              * (SELECT t.VALUE
                                   FROM gv$parameter t
                                  WHERE     t.inst_id = x.inst_id
                                        AND t.name = 'db_block_size'))
                         / 1024)
                      / 1024)
                  WHEN name != 'free memory'
                  THEN
                     ( (bytes / 1024) / 1024)
               END
                  used_mb
          FROM gv$sgastat x) temp,
       gv$instance gv
 WHERE gv.inst_id = temp.inst_id
GROUP BY component,
       temp.inst_id,
       gv.instance_name,
       gv.host_name

以下是此查询的输出:

1   irisdb1 db_buffer_cache     27,6    27,5
1   irisdb1 java pool            0,2    
1   irisdb1 large pool           0,2     0,1
1   irisdb1 log_buffer           0,2     0,2
1   irisdb1 shared pool          3,7       3
1   irisdb1 others                 0       0
2   irisdb2 db_buffer_cache     27,6    27,4
2   irisdb2 java pool            0,1    
2   irisdb2 large pool           0,4     0,2
2   irisdb2 log_buffer           0,2     0,2
2   irisdb2 shared pool            4     3,4
2   irisdb2 others                 0       0

正如您在输出中看到的,某些行(第2行和第8行)在最后一列中具有空值。我想要的是将0(双倍值)分配给那些空值。

3 个答案:

答案 0 :(得分:3)

如果值为NULL,那么您可以使用coalesce()(或其他几个函数)`来输入值:

 SELECT temp.inst_id,
       ' ',
       gv.instance_name,
       gv.host_name,
       CASE WHEN component IS NULL THEN 'others' ELSE component END
          component,
       COALESE( ROUND (SUM (used_mb), 1), 0.0) used_mb

答案 1 :(得分:1)

如果是NULL,您只需要给它一个默认值:

COALESCE(ROUND (SUM (used_mb), 1),0.0) used_mb

答案 2 :(得分:0)

ELSE

CASE语句中添加used_mb子句
           CASE
              WHEN name = 'buffer_cache'
              THEN
                 (  (  (  bytes
                        -   (SELECT COUNT (*)
                               FROM gv$bh
                              WHERE     inst_id = x.inst_id
                                    AND status = 'free')
                          * (SELECT t.VALUE
                               FROM gv$parameter t
                              WHERE     t.inst_id = x.inst_id
                                    AND t.name = 'db_block_size'))
                     / 1024)
                  / 1024)
              WHEN name != 'free memory'
              THEN
                 ( (bytes / 1024) / 1024)
              ELSE 0
           END
              used_mb