标题:请教SQL语句错在哪里???
取消只看楼主
foshan
Rank: 1
等 级:新手上路
威 望:2
帖 子:605
专家分:0
注 册:2006-3-1
结帖率:100%
 问题点数:0 回复次数:2 
请教SQL语句错在哪里???
1、执行以下查询语句时出现“在关键字 'Union' 附近有语法错误。”,不知如何更正错误???
2、以下查询语句为了实现在最后一行统计  FuKuanJinE 字段之和,请问能否精简一下代码??
谢谢大家!

select
HeTongXinXi.HeTongID,HeTongBianHao,HeTongMingCheng,YiFangDanWei,convert(varchar(10),FuKuanShiJian,120) as FuKuanShiJian,FuKuanJinE
from HeTongXinXi
right join ShiGongHeTongFuKuanDengJi on HeTongXinXi.HeTongID=ShiGongHeTongFuKuanDengJi.HeTong_ID
where  XiangMuMingChenID  like '%1/2/3/11%' order by HeTongBianHao
Union all
select
null as HeTongID,总计 as HeTongBianHao,null as HeTongMingCheng,null as YiFangDanWei,null as FuKuanShiJian,sum(FuKuanJinE) as FuKuanJinE
from HeTongXinXi
right join ShiGongHeTongFuKuanDengJi on HeTongXinXi.HeTongID=ShiGongHeTongFuKuanDengJi.HeTong_ID
where  XiangMuMingChenID  like '%1/2/3/11%'
搜索更多相关主题的帖子: SQL 语句 
2008-02-20 14:34
foshan
Rank: 1
等 级:新手上路
威 望:2
帖 子:605
专家分:0
注 册:2006-3-1
得分:0 
谢谢!将 ORDER BY 放在最后就可以解决出错的问题了。
select
HeTongXinXi.HeTongID,HeTongBianHao,HeTongMingCheng,YiFangDanWei,convert(varchar(10),FuKuanShiJian,120) as FuKuanShiJian,FuKuanJinE
from HeTongXinXi
right join ShiGongHeTongFuKuanDengJi on HeTongXinXi.HeTongID=ShiGongHeTongFuKuanDengJi.HeTong_ID
where  XiangMuMingChenID  like '%1/2/3/11%'
Union all
select
null as HeTongID,总计 as HeTongBianHao,null as HeTongMingCheng,null as YiFangDanWei,null as FuKuanShiJian,sum(FuKuanJinE) as FuKuanJinE
from HeTongXinXi
right join ShiGongHeTongFuKuanDengJi on HeTongXinXi.HeTongID=ShiGongHeTongFuKuanDengJi.HeTong_ID
where  XiangMuMingChenID  like '%1/2/3/11%'
order by HeTongBianHao

我是2.0超级菜鸟,请多多教导!
2008-02-28 08:57
foshan
Rank: 1
等 级:新手上路
威 望:2
帖 子:605
专家分:0
注 册:2006-3-1
得分:0 
还有一个问题:上述语句有很多重复部分,如何将上述的语句精简一下啊?

我是2.0超级菜鸟,请多多教导!
2008-02-28 08:59



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-200434-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.116782 second(s), 8 queries.
Copyright©2004-2025, BCCN.NET, All Rights Reserved