SQL分组函数

发布于 2024-11-06 22:10:42 字数 1167 浏览 2 评论 0原文

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

鯉魚旗 2024-11-13 22:10:42

据我所知,您不能按别名进行分组。尝试更改 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)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文