用于3个子查询的SQL查询,具有自联接功能
请提供一些建议,如何实现以下要求的输出用于3个子查询的SQL查询,具有自联接功能,sql,oracle,Sql,Oracle,请提供一些建议,如何实现以下要求的输出 Table1 data ----------- request_id record_type invoice_number merchant_info transaction_info ---------- ----------- ----------------- ---------------- ----------------------- 123 01
Table1 data
-----------
request_id record_type invoice_number merchant_info transaction_info
---------- ----------- ----------------- ---------------- -----------------------
123 01 NULL NULL Trans1
123 30 NULL Merc1 NULL
123 02 Invoice1 NULL NULL
123 01 NULL NULL Trans2
123 30 NULL Merc2 NULL
123 02 Invoice2 NULL NULL
124 01 NULL NULL Trans3
124 30 NULL Merc3 NULL
124 02 Invoice3 NULL NULL
124 01 NULL NULL Trans4
124 30 NULL Merc4 NULL
124 02 Invoice4 NULL NULL
所需输出
---------------
invoice_number merchant_info transaction_info
-------------- -------------- -----------------
Invoice1 Merc1 Trans1
Invoice2 Merc2 Trans2
Invoice3 Merc3 Trans3
Invoice4 Merc4 Trans4
SELECT xpt.transaction_info,XTG.merchant_info,x.invoice_number
FROM (select * from table1
WHERE record_type='01' )xpt,
(SELECT *
FROM table1
WHERE record_type='30'
) XTG,
(SELECT *
FROM table1
WHERE record_type='02'
and invoice_number is not null
) X
WHERE 1=1
and xtg.request_id=xpt.request_id
and x.request_id=xpt.request_id
只需使用聚合:
select request_id, max(invoice_number) as invoice_number,
max(merchant_info) as merchant_info,
max(transaction_info) as transaction_info
from table1
group by request_id;
select max (decode ( record_type, '01', transaction_info, null)) as transaction_info,
max (decode ( record_type, '30', merchant_info, null)) as merchant_info,
max (decode ( record_type, '02', invoice_number, null)) as invoice_number
from table1
group by request_id
只需使用聚合:
select request_id, max(invoice_number) as invoice_number,
max(merchant_info) as merchant_info,
max(transaction_info) as transaction_info
from table1
group by request_id;
select max (decode ( record_type, '01', transaction_info, null)) as transaction_info,
max (decode ( record_type, '30', merchant_info, null)) as merchant_info,
max (decode ( record_type, '02', invoice_number, null)) as invoice_number
from table1
group by request_id
尝试一些条件聚合:
select request_id, max(invoice_number) as invoice_number,
max(merchant_info) as merchant_info,
max(transaction_info) as transaction_info
from table1
group by request_id;
select max (decode ( record_type, '01', transaction_info, null)) as transaction_info,
max (decode ( record_type, '30', merchant_info, null)) as merchant_info,
max (decode ( record_type, '02', invoice_number, null)) as invoice_number
from table1
group by request_id
尝试一些条件聚合:
select request_id, max(invoice_number) as invoice_number,
max(merchant_info) as merchant_info,
max(transaction_info) as transaction_info
from table1
group by request_id;
select max (decode ( record_type, '01', transaction_info, null)) as transaction_info,
max (decode ( record_type, '30', merchant_info, null)) as merchant_info,
max (decode ( record_type, '02', invoice_number, null)) as invoice_number
from table1
group by request_id
选择发票号…
和按请求分组\u id
?我们不需要分组数据。此外,每个记录都会为一个或多个字段返回空值。我们提供了样本数据。根据这一点,我们要求同样的。不是分组数据。请注意,我看到一个表有3条记录,一个输出有1行,其中包含来自每个表记录的数据,这似乎是分组。如果没有,你能举个更清楚的例子吗?@jarlh。我很尴尬。干杯。我还是不太清楚<代码>发票1和发票2
具有相同的请求id
和记录类型
;那么,我怎么能说Invoice1
匹配Merc1
和Trans1
而不是Merc2
或Trans2
。您的数据中是否有订单?选择发票号…
和按请求分组\u id
?我们不需要分组数据。此外,每个记录都会为一个或多个字段返回空值。我们提供了样本数据。根据这一点,我们要求同样的。不是分组数据。请注意,我看到一个表有3条记录,一个输出有1行,其中包含来自每个表记录的数据,这似乎是分组。如果没有,你能举个更清楚的例子吗?@jarlh。我很尴尬。干杯。我还是不太清楚<代码>发票1和发票2
具有相同的请求id
和记录类型
;那么,我怎么能说Invoice1
匹配Merc1
和Trans1
而不是Merc2
或Trans2
。您的数据中是否有任何排序?