英文:
How to get data with values in columns matching to primary key in same row?
问题
我有下面的表格:
我需要的数据是"带有任何不为null的列的账号号码"
条件:
- 账号号码列不会有"null",它始终有值。
- 空列应该被忽略。
- 在每一行(在表中),账号号码和非null列应该形成一条记录。
- 代码应该是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:
- Account number column will not have "null". it always have values
- Null columns should be ignored.
- In each row ( in table ) account number and not null columns should form a one record .
- 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>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论