简体   繁体   English

Oracle 分贝。 如何获取同一表的几乎任意数量的列的唯一值的结果集

[英]Oracle db. How to get a result set of unique values of pretty much arbitrary amount of columns of the same table

Having this column structure具有这种列结构

Id, col_1, col_2, col_3
1    A      A      B
2    B      B      B
3    C      C      D

Is it possible to get one-column output with unique values?是否可以获得具有唯一值的一列 output?

res_column
 A
 B
 C
 D

What I've already tried:我已经尝试过的:

  1. Union seems doable only in case there are 2 to 3 columns, which is not the case.只有在有 2 到 3 列的情况下,联合似乎才可行,但事实并非如此。
  2. I've found a pivot/unpivot, but I didn't really grasp it.我找到了一个支点/反支点,但我并没有真正掌握它。
with
  sample_inputs (id, col_1, col_2, col_3) as (
    select 1, 'A', 'A', 'B' from dual union all
    select 2, 'B', 'B', 'B' from dual union all
    select 3, 'C', 'C', 'D' from dual
  )
select  distinct res_column
from    sample_inputs
unpivot (res_column for col in (col_1, col_2, col_3))
order   by res_column    ---   if needed
;

RES_COLUMN
----------
A
B
C
D

If you must handle null also, you can do so with the include nulls option to unpivot .如果您还必须处理null ,则可以使用unpivotinclude nulls nulls 选项来处理。 Without this option, the query above will still work - it will just ignore (discard, exclude) all null from the unpivoted columns.如果没有这个选项,上面的查询仍然有效——它只会忽略(丢弃、排除)来自未透视列的所有null

with
  sample_inputs (id, col_1, col_2, col_3) as (
    select 1, 'A' , 'A' , 'B'  from dual union all
    select 2, 'B' , 'B' , 'B'  from dual union all
    select 3, 'C' , 'C' , 'D'  from dual union all
    select 4, null, null, null from dual
  )
select  distinct res_column
from    sample_inputs
unpivot include nulls (res_column for col in (col_1, col_2, col_3))
order   by res_column    ---   if needed
;


RES_COLUMN
----------
A
B
C
D
 
 
(five rows selected)

Note that the last result has five rows, not four;请注意,最后一个结果有五行,而不是四行; the last value is null , which is invisible, but if you run the query in an editor that shows you the number of rows (or if you use an option to show null as <null> or such), then you will see it.最后一个值是null ,这是不可见的,但是如果您在显示行数的编辑器中运行查询(或者如果您使用选项将null显示为<null>等),那么您将看到它。

NOTE: You can't make this "generic" in plain SQL;注意:您不能在普通 SQL 中将此“通用”; the number of columns to unpivot, and their names, must be hard-coded in the query.要取消透视的列数及其名称必须在查询中进行硬编码。 If you need a "generic" solution you will need to use dynamic SQL;如果您需要“通用”解决方案,则需要使用动态 SQL; that is a much more advanced topic, and certainly not appropriate if you don't even grasp static unpivot yet.这是一个更高级的主题,如果您甚至还没有掌握 static unpivot ,那肯定是不合适的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 甲骨文数据库。 如何从一条记录的多个列中获取运行总计 - Oracle DB. How to get a running total from multiple columns on one record 如何获取唯一值来比较同一张表的两列? - How to get unique values to compare two columns of the same table? Oracle 中同一表中 2 列的唯一索引/约束 - Unique index/constraint on 2 columns in same table in Oracle 将列复制到oracle db中同一表中的另一列。 我需要指定哪些数据在哪里? - Copy a column to another column within a same table in oracle db. Do I need to specify which data goes where? 加入同一张桌子以获得2个不同的金额值 - Joining on the same table to get 2 different amount values Oracle如何获取提交量表? - Oracle How to get amount of commits table? 如何用任意列和行扩展SQL结果集? - How to extend a SQL result set with arbitrary columns and rows? 如何在 Oracle DB 中使用 max(Date) 和 Distinct。 我想在一天内最后插入数据 - How to use max(Date) and Distinct in Oracle DB. I would like to get data inserted very last within a day Oracle SQL-将来自多个表的结果集中的同一列中的值合并 - Oracle SQL - Combine values in the same column in one column in the result set from multiple table 给定一组 boolean 列,我如何获得具有最多真值的前 5 列 - Given a set of boolean columns how can I get the top 5 columns with the most amount of true values
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM