重开一贴,过滤与求和 sum()
DepotInTbl WareTbl
PLUNo,Name,Qty PLU,Price1
我想先把WareTbl中PLU(有重复)给先过滤掉,只取一个,然后进行查询
“select PLUNo,Name,sum(Qty) as dQuality,Price1 from DepotInTbl,WareTbl w where (select min(PLU) from WareTbl where PLU=w.PLU) and PLUNo=w.PLU and Qty>0”
也就是说先过滤重复的PLU,再去求那个SUM(),但是现在结果:确实是过滤了,但是是先求和、再过滤!返回的dQuality总是重复累加了。我要的是:先过滤,再求和!
求助~~~~~~~~~~~~~~~
[解决办法]
select a.PLUNo,a.[Name],sum(a.Qty) as dQuality, b.Price1
from DepotInTbl a inner join
(select PLU,min(Price1)as Price1 FROM WareTb1 group by PLU) b
on a.PLUNo = b.PLU
group by a.PLUNo,a.[Name],b.Price1
[解决办法]
select distinct PLU,Price1 from WareTbl
相於 select PLU,Price1 from WareTbl group by PLU,Price1
注意:distinct 多列,PLU可能有重。(好象不符合你的要求 )。。。
你可以改: select PLU , AVG(Price) as Price From WareTbl Group By PLU
" WareTbl 中的PLU要去掉重复项 " 是要如何去掉重呢,你要明。如果只保留一PLU的,多 Price 又取哪(或是平均值)。如果只要去掉相同格的的,存在多PLU,又如何表DepotInTbl 做接呢(左右接或是 交叉接全接,得到的果是不一的)
DepotInTbl的PLUNo要保留全部,
select PLUNo,Name,sum(Qty) as dQuality FROM DepotInTbl
GROUP BY PLUNo,Name
可以足要求,但需要再接一格 Price ...可是 price 有多。
我是不清楚主想要的果是什。。。。如果能用就好了。