我正在尝试在SQL中为用户创建一个视图。 (如下图所示)在这里,我希望在dbo.WipPartBook.Job列的值相同时,将dbo.WipPartBook.QtySupplied的值加起来。
SELECT TOP (100) PERCENT dbo.vwSTKCF.StockCode, dbo.vwSTKCF.Cavity, dbo.vwSTKCF.Mass, dbo.vwSTKCF.Material, dbo.WipPartBook.Job,
dbo.WipPartBook.QtySupplied, dbo.WipPartBook.TrnDate
FROM dbo.WipPartBook INNER JOIN
dbo.InvMaster INNER JOIN
dbo.WipMaster ON dbo.InvMaster.StockCode = dbo.WipMaster.StockCode ON dbo.WipPartBook.Job = dbo.WipMaster.Job LEFT OUTER JOIN
dbo.vwSTKCF ON dbo.InvMaster.StockCode = dbo.vwSTKCF.StockCode
WHERE (dbo.vwSTKCF.StockCode = 'SC2020-01-00')
ORDER BY dbo.vwSTKCF.StockCode
答案 0 :(得分:0)
您使用的是哪个SQL Server版本?如果它是当前的,你应该查看窗口函数,其中包括由“分组”列(dbo.WipPartBook.Job)分区的总数。 在这里查看介绍:Microsoft BOL
编辑:添加了一个(未经测试的)示例:
SELECT TOP (100) PERCENT dbo.vwSTKCF.StockCode, dbo.vwSTKCF.Cavity, dbo.vwSTKCF.Mass, dbo.vwSTKCF.Material, dbo.WipPartBook.Job,
dbo.WipPartBook.QtySupplied, dbo.WipPartBook.TrnDate,
SUM(dbo.WipPartBook.QtySupplied) OVER (PARTITION BY dbo.WipPartBook.Job) AS Total
FROM dbo.WipPartBook INNER JOIN
dbo.InvMaster INNER JOIN
dbo.WipMaster ON dbo.InvMaster.StockCode = dbo.WipMaster.StockCode ON dbo.WipPartBook.Job = dbo.WipMaster.Job LEFT OUTER JOIN
dbo.vwSTKCF ON dbo.InvMaster.StockCode = dbo.vwSTKCF.StockCode
WHERE (dbo.vwSTKCF.StockCode = 'SC2020-01-00')
ORDER BY dbo.vwSTKCF.StockCode
答案 1 :(得分:0)
SELECT dbo.WipPartBook.Job, sum(dbo.WipPartBook.QtySupplied)
FROM dbo.WipMaster
JOIN dbo.WipPartBook
ON dbo.WipPartBook.Job = dbo.WipMaster.Job
WHERE dbo.WipMaster.StockCode = 'SC2020-01-00'
GROUP BY dbo.WipPartBook.Job
Order BY dbo.WipPartBook.Job