union 和order by 连接使用报错的问题
来源:互联网 发布:英国公学。知乎 编辑:程序博客网 时间:2024/06/05 11:53
今天写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!
- union 和order by 连接使用报错的问题
- MySQL的union和order by一起使用的问题
- UNION/UNION ALL和order by 连接使用
- oracle order by 和union all(union)的使用
- ORACLE数据库UNION集合ORDER BY 和 ROWNUM的使用
- MySQL中union和order by一起使用的方法
- UNION ALL ORDER BY的问题
- UNION中ORDER By的使用
- UNION中ORDER By的使用
- union中order by的使用
- UNION中ORDER By的使用
- union 或者 union all 与 order by 的联合使用
- Union 中使用Order by
- 关于union的order by
- union all和order by一起使用出问题
- oracle union all和order by一起使用
- oracle union all和order by一起使用
- sql语句order by和union联合使用
- bmp图像格式 读二进制方式
- c++ 数据类型转换(RTTI)
- android.os.NetworkOnMainThreadException
- nfs
- 停下来与自己的心对话
- union 和order by 连接使用报错的问题
- 五险一金,你知道多少
- Yum被锁定以及其解决方法
- sql去重查询的另一种方式
- 对于开发环境无法升级checkin等各种乱七八糟的问题的最终解释
- SQL2008 附加数据库提示 5120错误
- linux syslog日志服务器的搭建
- C#运算符大全
- javascript XMLHttpRequest对象全面剖析