0

Here is my SQL :

SELECT F9_OA008_DT||'|',
DED2(PX_OA008_PAN,'a')||'|',
F9_OA008_MCC||'|',
FX_OA008_MERC_NAME||FX_OA008_MERC_FILLER1||FX_OA008_MERC_CTY||FX_OA008_MERC_FILLER2||FX_OA008_MERC_ST_CNTRY||'|',
F9_OA008_AMT_REQ
FROM OA008 
WHERE F9_OA008_MCC=5542 AND F9_OA008_DT >= 20120501 
UNION
SELECT F9_IB006_DT||'|',
DED2(PX_IB006_PAN,'a')||'|',
F9_IB006_MCC||'|',
FX_IB006_CRD_ACCPT_NAL||'|',
F9_IB006_AMT_REQ
FROM IB006 
WHERE F9_IB006_MCC=5542 AND F9_IB006_DT >= 20120501 
;

When I added in ORDER BY F9_OA008_DT for the 1st SQL and ORDER BY F9_IB006_DT for the 2nd sql , it will hit error. Why ? The error msg is: ORA-00923: FROM keyword not found where expected.

4

3 回答 3

1

You should only have one order by clause in a SQL statement.

select col1, col2 from tab1
union all
select col3, col4 from tab2
order by 1, 2

Notes: "union" does an implicit distinct (and therefore requires a sort under the covers). If you don't need distinct values then use "union all". You can use ordinal numbers as above to sort by columns in their positional order

于 2012-05-07T08:40:20.923 回答
0

Maybe you should put the same ALIAS for the field let say Field1, then use an ORDER BY in the whole SQL to like something ORDER BY Field1 rather than separate ORDER BY for each sub-query..

于 2012-05-07T08:16:40.763 回答
0

You can apply the order by clause on F9_OA008_DT and F9_IB006_DT fields in query only if these two fields are used in both sql statement.

But in your case your don't have these fields in both sql joined by union.

You can add a dummy field F9_IB006_DT in first sql statement and F9_OA008_DT in second sql statement as below sample and then try:

SELECT F9_OA008_DT||'|',
DED2(PX_OA008_PAN,'a')||'|',
F9_OA008_MCC||'|',
FX_OA008_MERC_NAME||FX_OA008_MERC_FILLER1||FX_OA008_MERC_CTY||FX_OA008_MERC_FILLER2||FX_OA008_MERC_ST_CNTRY||'|',
F9_OA008_AMT_REQ,'' as F9_IB006_DT 
FROM OA008 
WHERE F9_OA008_MCC=5542 AND F9_OA008_DT >= 20120501 
UNION
SELECT F9_IB006_DT||'|',
DED2(PX_IB006_PAN,'a')||'|',
F9_IB006_MCC||'|',
FX_IB006_CRD_ACCPT_NAL||'|',
F9_IB006_AMT_REQ, '' as F9_OA008_DT 
FROM IB006 
WHERE F9_IB006_MCC=5542 AND F9_IB006_DT >= 20120501 
order by F9_IB006_DT , F9_OA008_DT
;
于 2012-05-07T09:06:53.120 回答