SQL分组函数
我正在使用 odbc 连接到 as400,我知道连接字符串是正确的并且可以正常工作,因为我的所有其他查询都可以正常工作。
为什么这个sql语句有效:
select
rdqty,
alib.rcvshpdt.mbsnodesc,
rdauthor,
rdtitle,
rdediton,
ifnull(iecactno,actvty) as actlvl,
class,
ifnull(ieclp,lp) as cst
from
alib.rcvshpdt
left outer join
alib.bkmaster on alib.rcvshpdt.mbsnodesc=alib.bkmaster.mbsno
left outer join
alib.iecostda on alib.rcvshpdt.rcvno=alib.iecostda.rcvno
and alib.rcvshpdt.mbsnodesc = alib.iecostda.mbsnodesc
where
alib.rcvshpdt.rcvno='3930697'
order by
actlvl asc
但是一旦我尝试对数量求和并将结果分组如下,它就会失败。
select
sum(rdqty) as quanity,
alib.rcvshpdt.mbsnodesc,
rdauthor,
rdtitle,
rdediton,
ifnull(iecactno,actvty) as actlvl,
class,
ifnull(ieclp,lp) as cst
from
alib.rcvshpdt
left outer join
alib.bkmaster on alib.rcvshpdt.mbsnodesc=alib.bkmaster.mbsno
left outer join
alib.iecostda on alib.rcvshpdt.rcvno=alib.iecostda.rcvno
and alib.rcvshpdt.mbsnodesc = alib.iecostda.mbsnodesc
where
alib.rcvshpdt.rcvno='3930697'
group by
alib.rcvshpdt.mbsnodesc, rdauthor, rdtitle, rdediton, actlvl, class, cst
order by
actlvl asc
I am using an odbc connection to an as400, I know the connection string is proper and working as all my other queries work without any problem.
why is it that this sql statement works:
select
rdqty,
alib.rcvshpdt.mbsnodesc,
rdauthor,
rdtitle,
rdediton,
ifnull(iecactno,actvty) as actlvl,
class,
ifnull(ieclp,lp) as cst
from
alib.rcvshpdt
left outer join
alib.bkmaster on alib.rcvshpdt.mbsnodesc=alib.bkmaster.mbsno
left outer join
alib.iecostda on alib.rcvshpdt.rcvno=alib.iecostda.rcvno
and alib.rcvshpdt.mbsnodesc = alib.iecostda.mbsnodesc
where
alib.rcvshpdt.rcvno='3930697'
order by
actlvl asc
but as soon as I try to sum a quanity and group the results as the following it fails.
select
sum(rdqty) as quanity,
alib.rcvshpdt.mbsnodesc,
rdauthor,
rdtitle,
rdediton,
ifnull(iecactno,actvty) as actlvl,
class,
ifnull(ieclp,lp) as cst
from
alib.rcvshpdt
left outer join
alib.bkmaster on alib.rcvshpdt.mbsnodesc=alib.bkmaster.mbsno
left outer join
alib.iecostda on alib.rcvshpdt.rcvno=alib.iecostda.rcvno
and alib.rcvshpdt.mbsnodesc = alib.iecostda.mbsnodesc
where
alib.rcvshpdt.rcvno='3930697'
group by
alib.rcvshpdt.mbsnodesc, rdauthor, rdtitle, rdediton, actlvl, class, cst
order by
actlvl asc
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
据我所知,您不能按别名进行分组。尝试更改 Group 子句以直接使用计算,如下所示:
alib.rcvshpdt.mbsnodesc, rdauthor, rdtitle, rdediton, ifnull(iecactno,actvty), class, ifnull(ieclp,lp)
As far as I know, you can't group by an alias name. Try changing the Group clause to use the calculations directly like the following:
alib.rcvshpdt.mbsnodesc, rdauthor, rdtitle, rdediton, ifnull(iecactno,actvty), class, ifnull(ieclp,lp)