union 和order by 连接使用报错的问题

今天写sql的时候,遇到一个奇怪的现象,分别执行的时候可以成功执行,但是union连接之后却会报错。

--前面的sql


select count(distinct u.trade_id) as count, u.trade_type as trade_type
  from (select distinct (l.trade_id) as trade_id, t.trade_type as trade_type
          from uop_crm3.tf_b_trade l
         right join (select *
                      from uop_crm3.td_s_tradetype y
                     where y.trade_type_code in (71, 340, 63)) t on l.trade_type_code =
                                                                    t.trade_type_code
                                                                and l.net_type_code =
                                                                    t.net_type_code
        union
       
        select distinct (l.trade_id) as trade_id, t.trade_type as trade_type
          from uop_crm3.tf_bh_trade l
         right join (select *
                       from uop_crm3.td_s_tradetype y
where y.trade_type_code in (71, 340, 63)) t on l.trade_type_code =
                                                                     t.trade_type_code
                                                                 and l.net_type_code =
                                                                     t.net_type_code
       
        ) u
group by trade_type
order by trade_type

--后面的sql

select count(distinct uu.trade_id) as count,
       '开户-' || uu.net_type_name as trade_type
  from (select distinct (l.trade_id) as trade_id,
                        g.net_type_name as net_type_name
          from uop_crm3.tf_b_trade l, uop_crm3.td_s_netcode g
         where l.net_type_code = g.net_type_code
           and l.trade_type_code = '10'
           and l.eparchy_code = '0319'
           and l.net_type_code in ('16', '17', '33', '10', '40', '30')
        union
        select distinct (l.trade_id) as trade_id,
                        g.net_type_name as net_type_name
          from uop_crm3.tf_bh_trade l, uop_crm3.td_s_netcode g
         where l.net_type_code = g.net_type_code
           and l.trade_type_code = '10'
           and l.net_type_code in ('16', '17', '33', '10', '40', '30')) uu
group by net_type_name
order by trade_type

以上两个sql有同样的表结构,都具有两列,且类型一致,分别执行的时候没有任何问题,但是用union关联之后,PL/SQL 就会报错,找原因找找了好好久,最后终于发现错误的地方,原来是union与order by 使用不当惹得祸。

错误的语句是这样的:

select count(distinct u.trade_id) as count, u.trade_type as trade_type
  from (select distinct (l.trade_id) as trade_id, t.trade_type as trade_type
          from uop_crm3.tf_b_trade l
         right join (select *
                      from uop_crm3.td_s_tradetype y
                     where y.trade_type_code in (71, 340, 63)) t on l.trade_type_code =
                                                                    t.trade_type_code
                                                                and l.net_type_code =
                                                                    t.net_type_code
        union
       
        select distinct (l.trade_id) as trade_id, t.trade_type as trade_type
          from uop_crm3.tf_bh_trade l
         right join (select *
                       from uop_crm3.td_s_tradetype y
                      where y.trade_type_code in (71, 340, 63)) t on l.trade_type_code =
                                                                     t.trade_type_code
                                                                 and l.net_type_code =
                                                                     t.net_type_code
       
        ) u
group by trade_type
order by trade_type
union

--后面的sql

select count(distinct uu.trade_id) as count,
       '开户-' || uu.net_type_name as trade_type
  from (select distinct (l.trade_id) as trade_id,
                        g.net_type_name as net_type_name
          from uop_crm3.tf_b_trade l, uop_crm3.td_s_netcode g
         where l.net_type_code = g.net_type_code
           and l.trade_type_code = '10'
           and l.eparchy_code = '0319'
           and l.net_type_code in ('16', '17', '33', '10', '40', '30')
        union
        select distinct (l.trade_id) as trade_id,
                        g.net_type_name as net_type_name
          from uop_crm3.tf_bh_trade l, uop_crm3.td_s_netcode g
         where l.net_type_code = g.net_type_code
           and l.trade_type_code = '10'
           and l.net_type_code in ('16', '17', '33', '10', '40', '30')) uu
group by net_type_name
order by trade_type

修改之后的语句如下:
SELECT *
  FROM (select count(distinct u.trade_id) as count,
               u.trade_type as trade_type
          from (select distinct (l.trade_id) as trade_id,
                                t.trade_type as trade_type
                  from uop_crm3.tf_b_trade l
                 right join (select *
                              from uop_crm3.td_s_tradetype y
                             where y.trade_type_code in (71, 340, 63)) t on l.trade_type_code =
                                                                            t.trade_type_code
                                                                        and l.net_type_code =
                                                                            t.net_type_code
                union
               
                select distinct (l.trade_id) as trade_id,
                                t.trade_type as trade_type
                  from uop_crm3.tf_bh_trade l
                 right join (select *
                               from uop_crm3.td_s_tradetype y
                              where y.trade_type_code in (71, 340, 63)) t on l.trade_type_code =
                                                                             t.trade_type_code
                                                                         and l.net_type_code =
                                                                             t.net_type_code
               
                ) u
         group by trade_type
       
        union
        select count(distinct uu.trade_id) as count,
               '开户-' || uu.net_type_name as trade_type
          from (select distinct (l.trade_id) as trade_id,
                                g.net_type_name as net_type_name
                  from uop_crm3.tf_b_trade l, uop_crm3.td_s_netcode g
                 where l.net_type_code = g.net_type_code
                   and l.trade_type_code = '10'
                   and l.eparchy_code = '0319'
                   and l.net_type_code in
                       ('16', '17', '33', '10', '40', '30')
                union
                select distinct (l.trade_id) as trade_id,
                                g.net_type_name as net_type_name
                  from uop_crm3.tf_bh_trade l, uop_crm3.td_s_netcode g
                 where l.net_type_code = g.net_type_code
                   and l.trade_type_code = '10'
                   and l.net_type_code in
                       ('16', '17', '33', '10', '40', '30')) uu
         group by net_type_name)
order by trade_type

原来错误之处就在于union与oerder by的使用位置,union之后才可以使用order by ,查询语句继续零基础学习中。相信每天一点一滴的积累,再菜鸟的我,慢慢也会有所收获。

↖(^ω^)↗加油ing!

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   
                                                                                                                                                                                                                                                                                        

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值