日期:2014-05-18 浏览次数:20652 次
with t as( SELECT distinct c.cInvAddCode as 货号, d.dDate as 日期, a.cWhCode as 仓库, a.iQuantity as 数量, a.iNum as 件数, a.cFree1 as 颜色 , a.iInvExchRate as 换算率, c.cInvDefine1 as 标准条码, c.cInvDefine5 as 标准名称 FROM b, a, c, d WHERE ( a.DLID = b.DLID ) and ( b.cInvCode = c.cInvCode ) and ( a.cDLCode = d.cDLCode ) ) select [货号],[颜色],SUM([件数]) as [件数] from t group by [货号],[颜色] /* 货号 颜色 件数 b-0239 150 5 g-0226 CS彩色柜93 10 g-0227 CS彩色柜93 15 g-0226 黑白柜 40 g-0227 黑白柜 60 g-0226 蓝白柜96 70 g-0227 蓝白柜96 15 */ --结果是要这样的吗
------解决方案--------------------
with cte as ( SELECT distinct c.cInvAddCode , --as 货号 d.dDate , --as 日期 a.cWhCode , --as 仓库 a.iQuantity , --as 数量 a.iNum , --as 件数 a.cFree1 , --as 颜色 a.iInvExchRate , --as 换算率 c.cInvDefine1 , --as 标准条码 c.cInvDefine5 --as 标准名称 FROM b, a, c, d WHERE ( a.DLID = b.DLID ) and ( b.cInvCode = c.cInvCode ) and ( a.cDLCode = d.cDLCode ) ) select cInvAddCode, cFree1, SUM(iQuantity) as [总数量], SUM(iNum) as [总件数] from cte group by cInvAddCode,cFree1