Use Union and Order by problem (ORA-00933 error) to solve

Posted by jcarver on Mon, 02 Dec 2019 13:46:13 +0100

Previously, colleagues encountered this error while writing views. I'll sort out the simplified sentences as follows:

   1: select
   2: '2016' as nf,
   3: qxdm,
   4: round(sum(tbdlmj)/10000,2) as csydmj--Unit conversion, 2 decimal places
   5: from dltb_2016@dblink_td_tdxz m where dlmc='City'
   6: group by m.qxdm order by m.qxdm
   7:  
   8: union all 
   9:  
  10: select
  11: '2017' as nf,
  12: qxdm,
  13: round(sum(tbdlmj)/10000,2) as csydmj--Unit conversion, 2 decimal places
  14: from dltb_2017@dblink_td_tdxz n where dlmc='City'
  15: group by n.qxdm order by n.qxdm

It is mainly to query the area of urban land in the 2016 and 2017 land type map data of each jurisdiction. The statement can be executed successfully in single block, but the ora-00933 error appears after using the UNION.

Check that the number of columns and data format are consistent, and there is no mismatch.

After investigating the causes, we found that it was the words union and order by.

The final treatment is as follows:

1. If the sorting is unnecessary, it can be removed directly or unified after union

   1: select
   2: '2016' as nf,
   3: qxdm,
   4: round(sum(tbdlmj)/10000,2) as csydmj--Unit conversion, 2 decimal places
   5: from dltb_2016@dblink_td_tdxz m where dlmc='City'
   6: group by m.qxdm 
   7:  
   8: union all 
   9:  
  10: select
  11: '2017' as nf,
  12: qxdm,
  13: round(sum(tbdlmj)/10000,2) as csydmj--Unit conversion, 2 decimal places
  14: from dltb_2017@dblink_td_tdxz n where dlmc='City'
  15: group by n.qxdm

perhaps

   1: select
   2: '2016' as nf,
   3: qxdm,
   4: round(sum(tbdlmj)/10000,2) as csydmj--Unit conversion, 2 decimal places
   5: from dltb_2016@dblink_td_tdxz m where dlmc='City'
   6: group by m.qxdm 
   7:  
   8: union all 
   9:  
  10: select
  11: '2017' as nf,
  12: qxdm,
  13: round(sum(tbdlmj)/10000,2) as csydmj--Unit conversion, 2 decimal places
  14: from dltb_2017@dblink_td_tdxz n where dlmc='City'
  15: group by n.qxdm order by qxdm

2. Another layer of query can be nested

   1: select * from (
   2: select
   3: '2016' as nf,
   4: qxdm,
   5: round(sum(tbdlmj)/10000,2) as csydmj--Unit conversion, 2 decimal places
   6: from dltb_2016@dblink_td_tdxz m where dlmc='City'
   7: group by m.qxdm order by m.qxdm
   8: )
   9:  
  10: union all 
  11:  
  12: select * from (
  13: select
  14: '2017' as nf,
  15: qxdm,
  16: round(sum(tbdlmj)/10000,2) as csydmj--Unit conversion, 2 decimal places
  17: from dltb_2017@dblink_td_tdxz n where dlmc='City'
  18: group by n.qxdm order by n.qxdm
  19: )

perhaps

   1: with
   2:  s1 as (
   3:  select
   4:        '2016' as nf,
   5:        qxdm,
   6:        round(sum(tbdlmj)/10000,2) as csydmj--Unit conversion, 2 decimal places
   7:        from dltb_2016@dblink_td_tdxz m where dlmc='City'
   8:        group by m.qxdm order by m.qxdm
   9:   ),
  10:   s2 as (
  11:   select
  12:      '2017' as nf,
  13:      qxdm,
  14:      round(sum(tbdlmj)/10000,2) as csydmj--Unit conversion, 2 decimal places
  15:      from dltb_2017@dblink_td_tdxz n where dlmc='City'
  16:      group by n.qxdm order by n.qxdm
  17:   )
  18:   select * from s1
  19:   union all
  20:   select * from s2;

Topics: Oracle