我正在一个项目中,我必须显示库存的付款明细。问题在于,有多个使用相同编号生成的凭证。 对于每个凭单,我必须计算费率和数量的乘积,然后必须计算具有相同编号的所有凭单的总和,并将其显示在结果中。 到目前为止,我能够找到两列的乘积。
SELECT MIN(dbo.tblvoucherDetail.fldvoucherno) AS voucherNO,
dbo.tblvoucherDetail.fldpono, dbo.tblvoucherDetail.fldcategoryno, (
SELECT SUM((dbo.tblvoucherDetail.fldRate * dbo.tblvoucherDetail.fldRqty)))
AS Total
FROM dbo.tblvoucher INNER JOIN dbo.tblvoucherDetail ON
dbo.tblvoucher.fldvoucherno = dbo.tblvoucherDetail.fldvoucherno AND
dbo.tblvoucher.fldvoucherdate = dbo.tblvoucherDetail.fldvoucherdate AND
dbo.tblvoucher.fldpono = dbo.tblvoucherDetail.fldpono AND
dbo.tblvoucher.fldpodate = dbo.tblvoucherDetail.fldpodate AND
dbo.tblvoucher.fldcategoryno = dbo.tblvoucherDetail.fldcategoryno INNER JOIN
dbo.tblitem ON dbo.tblvoucherDetail.flditemid = dbo.tblitem.flditemno AND
dbo.tblvoucherDetail.fldcategoryno = dbo.tblitem.fldcategory AND
dbo.tblvoucherDetail.flditemsecno = dbo.tblitem.fldsectionno INNER JOIN
dbo.tblSection ON dbo.tblitem.fldsectionno = dbo.tblSection.fldSectionNo
AND dbo.tblitem.fldcategory = dbo.tblSection.fldCategoryNo
GROUP BY dbo.tblvoucherDetail.fldpono,
dbo.tblvoucherDetail.fldcategoryno,
dbo.tblvoucherDetail.fldRate, dbo.tblvoucherDetail.fldRqty,
dbo.tblitem.flditemname, dbo.tblitem.fldmunit, dbo.tblSection.fldSectionNo,
dbo.tblSection.fldSectionName
以上查询的输出如下:
我希望结果是这样的:
voucherNO | fldpono | fldcategoryno | Total
1 | 01 | 03 | 572280
3 | 01 | 04 | 56496
2 | 04 | 04 | 1500
我想计算总列的总和并按凭证编号分组
答案 0 :(得分:0)
尝试一下:
SELECT Min(dbo.tblvoucherdetail.fldvoucherno) AS voucherNO,
dbo.tblvoucherdetail.fldpono,
dbo.tblvoucherdetail.fldcategoryno,
Sum(dbo.tblvoucherdetail.fldrate * dbo.tblvoucherdetail.fldrqty) AS Total
FROM dbo.tblvoucher
INNER JOIN dbo.tblvoucherdetail
ON dbo.tblvoucher.fldvoucherno = dbo.tblvoucherdetail.fldvoucherno
AND dbo.tblvoucher.fldvoucherdate = dbo.tblvoucherdetail.fldvoucherdate
AND dbo.tblvoucher.fldpono = dbo.tblvoucherdetail.fldpono
AND dbo.tblvoucher.fldpodate = dbo.tblvoucherdetail.fldpodate
AND dbo.tblvoucher.fldcategoryno = dbo.tblvoucherdetail.fldcategoryno
INNER JOIN dbo.tblitem
ON dbo.tblvoucherdetail.flditemid = dbo.tblitem.flditemno
AND dbo.tblvoucherdetail.fldcategoryno = dbo.tblitem.fldcategory
AND dbo.tblvoucherdetail.flditemsecno = dbo.tblitem.fldsectionno
INNER JOIN dbo.tblsection
ON dbo.tblitem.fldsectionno = dbo.tblsection.fldsectionno
AND dbo.tblitem.fldcategory = dbo.tblsection.fldcategoryno
GROUP BY dbo.tblvoucherdetail.fldpono,
dbo.tblvoucherdetail.fldcategoryno;
答案 1 :(得分:0)
学习使用表别名!
您的查询确实很难解密,但我将从简单的汇总开始:
SELECT v.fldvoucherno, v.fldpono, v.fldcategoryno,
SUM(vd.fldRate * vd.fldRqty) AS Total
FROM dbo.tblvoucher v INNER JOIN
dbo.tblvoucherDetail vd
ON v.fldvoucherno = vd.fldvoucherno AND
v.fldvoucherdate = vd.fldvoucherdate AND
v.fldpono = vd.fldpono AND
v.fldpodate = vd.fldpodate AND
v.fldcategoryno = vd.fldcategoryno INNER JOIN
dbo.tblitem i
ON vd.flditemid = i.flditemno AND
vd.fldcategoryno = i.fldcategory AND
vd.flditemsecno = i.fldsectionno INNER JOIN
dbo.tblSection s
ON i.fldsectionno = s.fldSectionNo AND
i.fldcategory = s.fldCategoryNo
GROUP BY v.fldvoucherno, v.fldpono, v.fldcategoryno;
您的JOIN
条件似乎也不必要地复杂。例如,我希望fldvoucherno
足以应付凭证和凭证明细之间的JOIN
。