sql - 组合行以创建两列数据

标签 sql oracle oracle10g

我对如何正确执行此查询有点困惑。我有一张看起来像这样的 table 。其中,区 0 表示应应用于所有区(全局)的值。

[ district ] [ code ] [ value ]
     1          A         11
     1          C         12
     2          A         13
     2          B         14
     0          B         15

我构建了一个查询(如下)来组合每个地区的“全局值(value)”。

[ district ] [ code ] [ district value ] [ global value ]
      1         A            11               null        -> row 1
      1         B           null               15         -> row 2
      1         C            12               null        -> row 3
      2         A            13               null        -> row 4
      2         B            14                15         -> row 5
      2         C           null              null        -> row 6 (optional)

我通过加入所有可能的地区/代码列表来做到这一点。

select all_code.district, all_code.code, table_d.value, table_g.value
  from (select distinct b.district, a.code
          from temp_table a
         inner join (select distinct district
                      from temp_table
                     where district <> 0) b
            on 1 = 1) all_code
  left join temp_table table_d
    on table_d.code = all_code.code
   and table_d.district = all_code.district
  left join temp_table table_g
    on table_g.code = all_code.code
   and table_g.district = 0

这个查询效果很好,但看起来很丑。有更好的方法吗? (请注意,我不关心第 6 行是否存在)。

如果需要,这里有一个脚本。

create table temp_table
(
  district VARCHAR2(5) not null,
  code     VARCHAR2(5) not null,
  value    VARCHAR2(5) not null
);


insert into temp_table (district, code, value)
values ('1', 'A', '11');
insert into temp_table (district, code, value)
values ('1', 'C', '12');
insert into temp_table (district, code, value)
values ('2', 'A', '13');
insert into temp_table (district, code, value)
values ('2', 'B', '14');
insert into temp_table (district, code, value)
values ('0', 'B', '15');

最佳答案

这是选项之一。由于您使用的是 10g,因此您可以使用分区外连接(partition by() 子句)来填补空白:

with DCodes(code) as(
  select 'A' from dual union all
  select 'B' from dual union all
  select 'C' from dual
),
DGlobal(code, value1) as(
  select code
       , value
    from temp_table
   where district = 0
)
select tt.district
     , dc.code
     , tt.value
     , dg.value1 as global_value
  from temp_table tt
       partition by(tt.district)
       right join DCodes dc 
               on (dc.code = tt.code)
       left join DGlobal dg
              on (dg.code = dc.code)
 where tt.district != 0
 order by 1, 2

结果:

DISTRICT  CODE  VALUE  GLOBAL_VALUE
--------  ----  -----  ------------
1         A     11                 
1         B            15           
1         C     12                 
2         A     13                 
2         B     14     15           
2         C                       

关于sql - 组合行以创建两列数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28201241/

相关文章:

SQL 查询 - 它们相同吗?

java - 如何通过 Payara5 在 Web 应用程序上显示数据库触发错误

php - 避免删除 API 函数中的 SQL 注入(inject)

sql - Oracle 中带有 CASE 语句的条件 WHERE 子句

oracle10g - 我无法为 Oracle10G 启动 TNSListener 服务

sql - 从多个相关表中删除行

SQL 根据与另一个表的连接更新表

mysql - SQL 仅选择列上具有最大值的行

java - 组织.hibernate.id.IdentifierGenerationException : ids for this class must be manually assigned before calling save(): while id is autogenerate in oracle

sql-server - Oracle 和 SQL Server 中的主键