[英]SQL Query: How to Join Two SQL Queries in Oracle Report
我的任務是將舊的報表程序轉換為Oracle報表,當我需要加入兩個查詢以使報表工作時,我就停了下來。 我對SQL並不陌生,但是在這方面我確實需要幫助。
對於Oracle Reports 11g,報表需要顯示以下兩個查詢的結果,因此,這些查詢需要在一個SQL查詢中結合在一起才能使報表正常工作。
第一個查詢:
select table_name
, to_char(load_date, 'MM/DD/ YYYY') as XDATE
, to_char(number_name) as NUMBER NAME
, round(sysdate-load_date) as DAYS
, 'E' AS TABLEIND
from error_table
where load_date is not null
and round(sysdate-load_date) > 15
and number_name not in
(select number_name
from table_comments)
order by table_name
第二個查詢:
select table_name
, to_char(load_date, 'MM/DD/ YYYY') as XDATE
, to_char(number_name) as NUMBER NAME
, round(sysdate-load_date) as DAYS
, 'O' AS TABLEIND
from other_table
where load_date is not null
and round(sysdate-load_date) > 15
and number_name not in
(select number_name
from table_comments)
order by table_name
這兩個查詢的結果應顯示這兩個查詢的結果,第一個查詢優先,第二個查詢第二個。 非常感謝您提供有關此問題的幫助。
( Query1
--leave out the "order by" line
)
UNION ALL
( Query2
--leave out the "order by" line, too
)
ORDER BY TABLEIND
, table_name
如果您要使它們出現在一個結果集中,請嘗試在它們之間使用UNION。 我相信,您可以通過TABLEIND,table_name對整個結果集進行排序,以對所需的方式進行排序。
如果必須采用這種格式,則將第一個查詢的結果轉儲到帶有標識列的臨時表中,然后將第二個查詢的結果轉儲到同一表中。
然后從該臨時表中選擇該標識列排序
您可以使用現有查詢作為內聯視圖來創建聯合查詢:
select 1 as whichQuery, q1.col, q1.col, ...
from
(select....) as q1
union all
select 2 as whichQuery, q2.col, q2.col, ...
from
(select ....) as q2
然后您可以通過whichQuery進行排序。 這樣可以保證在TABLEIND alpha排序值應變化(而不是按所需順序排序)的情況下所需的順序。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.