查询以合并 Oracle/Teradata 中的后续行
Query to Merge subsequent rows in Oracle/Teradata
我有一个table,数据如下
DB DBMS INST SCHEMA TABLE COLUMN HDFT N_Identity Class
IDS TD SBD IDS Data_Val cust_t HIGH
IDS TD SBD IDS Data_Val cust_t GID
IDS TD SBD IDS Data_Val cust_t Phone
IDS TD SBD IDS Data_Val cust_t Account
IDS TD SBD IDS Data_Val cust_t Visa
IDS TD SBD IDS Data_Val cust_t Mail
IDS TD SBD IDS Data_Val cust_t Email
IDS TD SBD IDS Data_Val cust_t Login Yes
TDS TD FDT TDS Expense Exp_t Name LOW
TDS TD FDT TDS Expense Exp_t Yes
我想要如下输出:
DB DBMS INST SCHEMA TABLE COLUMN HDFT N_Identity Class
IDS TD SBD IDS Data_Val cust_t GID Yes HIGH
IDS TD SBD IDS Data_Val cust_t Phone Yes HIGH
IDS TD SBD IDS Data_Val cust_t Account Yes HIGH
IDS TD SBD IDS Data_Val cust_t Visa Yes HIGH
IDS TD SBD IDS Data_Val cust_t Mail Yes HIGH
IDS TD SBD IDS Data_Val cust_t Email Yes HIGH
IDS TD SBD IDS Data_Val cust_t Login Yes HIGH
TDS TD FDT TDS Expense Exp_t Name Yes LOW
N_identity 对于特定的 column.So 将具有 Yes 或 No 值到目前为止我已经尝试使用以下查询但它没有给我想要的结果:
SELECT * FROM
(
SELECT * FROM
(
SELECT DB,DBMS,INST,SCHEMA,TABLE,COLUMN, MAX(HDFT) as HDFT, MAX(N_Identity) as N_Identity, MAX(Class) as Class
FROM Table
GROUP BY DB,DBMS,INST,SCHEMA,TABLE,COLUMN
)a
UNION
SELECT DB,DBMS,INST,SCHEMA,TABLE,COLUMN, HDFT, N_Identity, Class FROM Table
)b
WHERE HDFT IS NOT NULL
AND N_Identity IS NOT NULL
AND Class IS NOT NULL
更新要求:
HDFT 值可以为空,以下是一种情况:
DB DBMS INST SCHEMA TABLE COLUMN HDFT N_Identity Class
IDS TD SBD IDS Data_Val cust_t No INT
IDS TD SBD IDS Data_Val cust_t INT
IDS TD SBD IDS Data_Val cust_t No
预期结果:
DB DBMS INST SCHEMA TABLE COLUMN HDFT N_Identity Class
IDS TD SBD IDS Data_Val cust_t No INT
select db,dbms,inst,schema,table,column,hdft,
case when N_identity>0 then N_Identity else 'Yes' end as N_identity
,case when class>0 then class
when column='cust_t' then 'HIGH'
when column='Exp_t' then 'LOW' end as Class
from table
where hdft>0
我想你需要 nvl()
和 first_value()
分析函数只是考虑在最后一步过滤 hdft is not null
(在这些函数完成子查询操作之后):
with tab2 as
(
select db, dbms, inst, schema, "table", "column", hdft,
first_value(class) over (partition by db) as class,
nvl(N_Identity,'Yes') as N_Identity
from tab
)
select * from tab2 where hdft is not null;
P.S。避免使用保留关键字来命名 table 或 table
、column
等列。
您可以像这样使用 first_value 函数:
with selection as
(
select db, dbms, inst, scheme, table1, column1, hdft,
first_value(Class1) over (partition by db order by Class1) as "class1",
first_value(N_Identity) over (partition by db order by N_Identity) as "N_Identity"
from Y
)
select * from selection where hdft is not null;
我有一个table,数据如下
DB DBMS INST SCHEMA TABLE COLUMN HDFT N_Identity Class
IDS TD SBD IDS Data_Val cust_t HIGH
IDS TD SBD IDS Data_Val cust_t GID
IDS TD SBD IDS Data_Val cust_t Phone
IDS TD SBD IDS Data_Val cust_t Account
IDS TD SBD IDS Data_Val cust_t Visa
IDS TD SBD IDS Data_Val cust_t Mail
IDS TD SBD IDS Data_Val cust_t Email
IDS TD SBD IDS Data_Val cust_t Login Yes
TDS TD FDT TDS Expense Exp_t Name LOW
TDS TD FDT TDS Expense Exp_t Yes
我想要如下输出:
DB DBMS INST SCHEMA TABLE COLUMN HDFT N_Identity Class
IDS TD SBD IDS Data_Val cust_t GID Yes HIGH
IDS TD SBD IDS Data_Val cust_t Phone Yes HIGH
IDS TD SBD IDS Data_Val cust_t Account Yes HIGH
IDS TD SBD IDS Data_Val cust_t Visa Yes HIGH
IDS TD SBD IDS Data_Val cust_t Mail Yes HIGH
IDS TD SBD IDS Data_Val cust_t Email Yes HIGH
IDS TD SBD IDS Data_Val cust_t Login Yes HIGH
TDS TD FDT TDS Expense Exp_t Name Yes LOW
N_identity 对于特定的 column.So 将具有 Yes 或 No 值到目前为止我已经尝试使用以下查询但它没有给我想要的结果:
SELECT * FROM
(
SELECT * FROM
(
SELECT DB,DBMS,INST,SCHEMA,TABLE,COLUMN, MAX(HDFT) as HDFT, MAX(N_Identity) as N_Identity, MAX(Class) as Class
FROM Table
GROUP BY DB,DBMS,INST,SCHEMA,TABLE,COLUMN
)a
UNION
SELECT DB,DBMS,INST,SCHEMA,TABLE,COLUMN, HDFT, N_Identity, Class FROM Table
)b
WHERE HDFT IS NOT NULL
AND N_Identity IS NOT NULL
AND Class IS NOT NULL
更新要求: HDFT 值可以为空,以下是一种情况:
DB DBMS INST SCHEMA TABLE COLUMN HDFT N_Identity Class
IDS TD SBD IDS Data_Val cust_t No INT
IDS TD SBD IDS Data_Val cust_t INT
IDS TD SBD IDS Data_Val cust_t No
预期结果:
DB DBMS INST SCHEMA TABLE COLUMN HDFT N_Identity Class
IDS TD SBD IDS Data_Val cust_t No INT
select db,dbms,inst,schema,table,column,hdft,
case when N_identity>0 then N_Identity else 'Yes' end as N_identity
,case when class>0 then class
when column='cust_t' then 'HIGH'
when column='Exp_t' then 'LOW' end as Class
from table
where hdft>0
我想你需要 nvl()
和 first_value()
分析函数只是考虑在最后一步过滤 hdft is not null
(在这些函数完成子查询操作之后):
with tab2 as
(
select db, dbms, inst, schema, "table", "column", hdft,
first_value(class) over (partition by db) as class,
nvl(N_Identity,'Yes') as N_Identity
from tab
)
select * from tab2 where hdft is not null;
P.S。避免使用保留关键字来命名 table 或 table
、column
等列。
您可以像这样使用 first_value 函数:
with selection as
(
select db, dbms, inst, scheme, table1, column1, hdft,
first_value(Class1) over (partition by db order by Class1) as "class1",
first_value(N_Identity) over (partition by db order by N_Identity) as "N_Identity"
from Y
)
select * from selection where hdft is not null;