如何获取与同一行中主键匹配的列中的值数据?

huangapple go评论53阅读模式
英文:

How to get data with values in columns matching to primary key in same row?

问题

我有下面的表格:

如何获取与同一行中主键匹配的列中的值数据?

我需要的数据是"带有任何不为null的列的账号号码"

条件:

  1. 账号号码列不会有"null",它始终有值。
  2. 空列应该被忽略。
  3. 在每一行(在表中),账号号码和非null列应该形成一条记录。
  4. 代码应该是SQL而不是PL/SQL。

以下是示例输出格式:

如何获取与同一行中主键匹配的列中的值数据?

表格语法:

CREATE TABLE demo (acc_no number primary key, id_no varchar2(20), id_no2 varchar2(20), id_no3 number, lice_no number)

INSERT INTO demo (acc_no, id_no, id_no2) VALUES ('452', '452', '452');
INSERT INTO demo (acc_no, id_no3, lice_no) VALUES ('411', '568', '987');
英文:

I am having below table

如何获取与同一行中主键匹配的列中的值数据?

and i need data like "account_number with any column which doesn't have null "

conditions:

  1. Account number column will not have "null". it always have values
  2. Null columns should be ignored.
  3. In each row ( in table ) account number and not null columns should form a one record .
  4. Code should be in SQl not in PL/SQL.

below is the sample output format

如何获取与同一行中主键匹配的列中的值数据?

Table syntax:

CREATE TABLE demo (acc_no number primary key, id_no varchar2(20), id_no2 varchar2(20), id_no3 number, lice_no number)

INSERT INTO demo (acc_no,id_no,id_no2) VALUES ( '452', '452', '452');
INSERT INTO demo (acc_no,id_no3,lice_no) VALUES ('411','568','987');

答案1

得分: 2

这是Oracle数据库,没有text数据类型,所以我改用了varchar2

SQL> desc demo
 Name                                      Null?    Type
 ----------------------------------------- -------- ----------------------------
 ACC_NO                                    NOT NULL NUMBER(38)
 ID_NO                                              VARCHAR2(20)
 ID_NO2                                             VARCHAR2(20)
 ID_NO3                                             NUMBER(38)
 LICE_NO                                            NUMBER(38)
SQL> select * From demo;

    ACC_NO ID_NO                ID_NO2                   ID_NO3    LICE_NO
---------- -------------------- -------------------- ---------- ----------
   452      452                  452
   411                                                  568        987

一个简单的选项是使用union操作来获取行,因为数据类型必须匹配,所以我在integer列上使用了to_char

SQL> select acc_no, id_no            from demo where id_no   is not null
  2  union all
  3  select acc_no, id_no2           from demo where id_no2  is not null
  4  union all
  5  select acc_no, to_char(id_no3)  from demo where id_no3  is not null
  6  union all
  7  select acc_no, to_char(lice_no) from demo where lice_no is not null;

    ACC_NO ID_NO
---------- ----------------------------------------
   452      452
   452      452
   411      568
   411      987

SQL>
英文:

This is Oracle, and there's no text datatype so I switched to varchar2.

SQL> desc demo
 Name                                      Null?    Type
 ----------------------------------------- -------- ----------------------------
 ACC_NO                                    NOT NULL NUMBER(38)
 ID_NO                                              VARCHAR2(20)
 ID_NO2                                             VARCHAR2(20)
 ID_NO3                                             NUMBER(38)
 LICE_NO                                            NUMBER(38)

SQL> select * From demo;

    ACC_NO ID_NO                ID_NO2                   ID_NO3    LICE_NO
---------- -------------------- -------------------- ---------- ----------
       452 452                  452
       411                                                  568        987

A trivial option is to union columns to get rows; as datatypes must match, I used to_char on integer columns:

SQL> select acc_no, id_no            from demo where id_no   is not null
  2  union all
  3  select acc_no, id_no2           from demo where id_no2  is not null
  4  union all
  5  select acc_no, to_char(id_no3)  from demo where id_no3  is not null
  6  union all
  7  select acc_no, to_char(lice_no) from demo where lice_no is not null;

    ACC_NO ID_NO
---------- ----------------------------------------
       452 452
       452 452
       411 568
       411 987

SQL>

huangapple
  • 本文由 发表于 2023年7月17日 14:01:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/76701837.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定