[SQL]按部件号查找位置,托盘,箱子,部件号和总单位数量

时间:2015-02-19 06:23:57

标签: sql

执行查询时遇到错误:

按部件号

查找位置,托盘,箱子,部件号和总单位数量
[select l.stoloc,
    il.lodnum,
    ins.subnum,
    p.prtnum,
    sum(i.untqty)
  from invdtl as i
 inner
 join invsub as ins
 on i.subnum = ins.subnum
inner
 join invlod as il
 on il.lodnum = ins.lodnum
inner
 join locmst as l
 on l.stoloc = il.stoloc
 and l.wh_id = il.wh_id
inner
join prtmst as p
 on p.prtnum = i.prtnum
where l.stoloc = '10A10B'
group by p.prtnum] catch(-1403)    

这是用MOCA语言编写的,如果你不了解MOCA,请忽略catch和方括号,请帮我找出正确的查询。

目前我在执行上述查询时遇到以下错误:

error

2 个答案:

答案 0 :(得分:3)

在您的查询中,您聚合SUM() i.untqty个其他列将具有多个值,您必须将其包含在group by子句中或对其应用聚合函数。根据您的要求。

select l.stoloc,
    il.lodnum,
    ins.subnum,
    p.prtnum,
    sum(i.untqty)
  from invdtl as i
 inner
 join invsub as ins
 on i.subnum = ins.subnum
inner
 join invlod as il
 on il.lodnum = ins.lodnum
inner
 join locmst as l
 on l.stoloc = il.stoloc
 and l.wh_id = il.wh_id
inner
join prtmst as p
 on p.prtnum = i.prtnum
where l.stoloc = '10A10B'
group by p.prtnum,l.stoloc,il.lodnum,ins.subnum

我目前正在应用Group By,但您需要应用相关的汇总函数或Group by子句。

答案 1 :(得分:1)

select l.stoloc,il.lodnum,ins.subnum,p.prtnum,sum(i.untqty) from invdtl as i inner join invsub as ins on i.subnum = ins.subnum inner join invlod as il on il.lodnum = ins.lodnum inner join locmst as l on l.stoloc = il.stoloc and l.wh_id = il.wh_id inner join prtmst as p on p.prtnum = i.prtnum where l.stoloc = '10A10B' group by l.stoloc,il.lodnum,ins.subnum,p.prtnum;

这应该有效我认为你在group by子句中缺少l.stoloc。因为sql查询的拇指规则要在select子句中包含所有group by个东西。