我已经乘以TJM.intMaterialQuantity * TM.monMaterialCost它给出了每个材料项在不同行中的总和。我需要将这4个总计相加并放入一个名为monTotalMaterialCost的新列中。它现在的方式显示monTotalMaterialCost中的每个材质。如何将所有材料的总数合并为一行,以便我可以在一行中看到作业和客户以及总成本?
SELECT
TJ.intJobID
,TC.intCustomerID
,TC.strLastName + ', ' + TC.strFirstName AS strCustomerName
,(SUM (TJM.intMaterialQuantity) * SUM (TM.monMaterialCost)) AS monTotalMaterialCost
FROM
TJobs AS TJ
,TJobCustomers AS TJC
,TCustomers AS TC
,TJobMaterials AS TJM
,TMaterials AS TM
WHERE
TJ.intJobID = TJC.intJobID
AND TJC.intCustomerID = TC.intCustomerID
AND TJM.intMaterialID = TM.intMaterialID
AND TJM.intJobID = TJ.intJobID
AND TJ.intJobID = 1
GROUP BY
TJ.intJobID
,TC.intCustomerID
,TC.strLastName + ', ' + TC.strFirstName
,TJM.intMaterialQuantity
,TM.monMaterialCost
示例输入
--Insert into TMaterials
INSERT INTO TMaterials ( intMaterialID, strMaterials, monMaterialCost )
VALUES ( 1, 'Nails', '4.99' )
INSERT INTO TMaterials ( intMaterialID, strMaterials, monMaterialCost )
VALUES ( 2, 'Drywall per 32 sqft', '12.99' )
INSERT INTO TMaterials ( intMaterialID, strMaterials, monMaterialCost )
VALUES ( 3, '2 x 4', '1.89' )
INSERT INTO TMaterials ( intMaterialID, strMaterials, monMaterialCost )
VALUES ( 4, 'Paint per gallon', '32.00' )
INSERT INTO TMaterials ( intMaterialID, strMaterials, monMaterialCost )
VALUES ( 5, 'Tile per sqft', '4.99' )
INSERT INTO TMaterials ( intMaterialID, strMaterials, monMaterialCost )
VALUES ( 6, 'Copper Water 10ft line', '6.99' )
INSERT INTO TMaterials ( intMaterialID, strMaterials, monMaterialCost )
VALUES ( 7, 'Screws', '4.99' )
INSERT INTO TMaterials ( intMaterialID, strMaterials, monMaterialCost )
VALUES ( 8, 'Shingles', '40.99' )
INSERT INTO TMaterials ( intMaterialID, strMaterials, monMaterialCost )
VALUES ( 9, 'Tar Paper per sqft', '1.99' )
--Insert into TJobMaterials
--Materials for Job 1 Customer 1
INSERT INTO TJobMaterials ( intJobID, intMaterialID, intMaterialQuantity )
VALUES (1, 1, 50 )
INSERT INTO TJobMaterials ( intJobID, intMaterialID, intMaterialQuantity )
VALUES (1, 2, 20 )
INSERT INTO TJobMaterials ( intJobID, intMaterialID, intMaterialQuantity )
VALUES (1, 3, 20 )
INSERT INTO TJobMaterials ( intJobID, intMaterialID, intMaterialQuantity )
VALUES (1, 4, 5 )
INSERT INTO TJobMaterials ( intJobID, intMaterialID, intMaterialQuantity )
VALUES (1, 5, 35 )
/*
Output I'm Getting
intJobID strJobDescription intJobStatusID strJobStatus monTotalMaterialCost
1 Kitchen Remodel 3 Complete 160.00
1 Kitchen Remodel 3 Complete 37.80
1 Kitchen Remodel 3 Complete 259.80
1 Kitchen Remodel 3 Complete 174.65
1 Kitchen Remodel 3 Complete 249.50
Output Wanted
intJobID strJobDescription intJobStatusID strJobStatus monTotalMaterialCost
1 Kitchen Remodel 3 Complete 881.75
I need the total of all 5 outputs into one final total into monTotalMaterialCost
* /
所需的输出: intJobID = 1 strJobDescription = Kitchen Remodel strJobStatus = Complete monTotalMaterialCost = 881.75
我得到了什么: intJobID = 1 strJobDescription = Kitchen Remodel strJobStatus = Complete monTotalMaterialCost = 7391.80
发生了什么:我有5种不同的材料来自TJM.intMaterialQuantity。每行中的每个值为50,20,20,5,35然后我对TM.monMaterialCost中的每种材料有5种不同的成本,这些值以相同的顺序匹配4.99,12.99,1.89,32.00,4.99。我得到了50 + 20 + 20 + 5 + 35 = 130和4.99 + 12.99 + 1.89 + 32.00 + 4.99 = 56.86所以我得到了最终的等式130 * 56.86 = 7,391.80。
我需要的是50 * 4.99 = 249.50,20 * 12.99 = 259.80,20 * 1.89 = 37.80,5 * 32.00 = 160,35 * 4.99 = 174.65我需要将所有这些总数加在一起以获得总金额用于工作的材料共计881.75。
我希望这有助于更好地理解我的问题。
我已经弄清楚了。我已经为未来的问题发布了正确答案:
SELECT
TJ.intJobID
,TJ.strJobDescription
,TJ.intJobStatusID
,TJS.strJobStatus
, SUM (TJM.intMaterialQuantity * TM.monMaterialCost) AS monTotalMaterialCost
FROM TJobs AS TJ
join TJobMaterials AS TJM on TJM.intJobID = TJ.intJobID
join TMaterials AS TM on TJM.intMaterialID = TM.intMaterialID
join TJobStatus AS TJS on TJ.intJobStatusID = TJS.intJobStatusID
GROUP BY
TJ.intJobID
,TJ.strJobDescription
,TJ.intJobStatusID
,TJS.strJobStatus
答案 0 :(得分:1)
你真的应该停止使用过时的ANSI-89样式连接。 ANSI-92风格的连接已经存在了25年。它们更容易阅读并防止出现大量意外错误。
查询的后半部分看起来像这样。
FROM TJobs AS TJ
join TJobCustomers AS TJC on TJ.intJobID = TJC.intJobID
join TCustomers AS TC on TJC.intCustomerID = TC.intCustomerID
join TJobMaterials AS TJM on TJM.intJobID = TJ.intJobID
join TMaterials AS TM on TJM.intMaterialID = TM.intMaterialID
WHERE TJ.intJobID = 1