0

我当前的 SQL 查询:

SELECT WorkOrderParts.PartQTY,
       '$' + CONVERT(varchar(22), WorkOrderParts.PartCost, 1) AS PartCost,
       '$' + CONVERT(varchar(22), WorkOrderParts.PartQTY * WorkOrderParts.PartCost, 1) AS PartsTotal,
       tblParts.fldMfgPartNumber
FROM  tblWorkorders
INNER JOIN WorkOrderParts ON tblWorkorders.ID = WorkOrderParts.WorkOrderId
INNER JOIN tblParts ON WorkOrderParts.PartId = tblParts.ID
WHERE (tblWorkorders.ProcessedDate BETWEEN '5/1/2012 12:00:00 AM'
                                       AND '5/2/2012 12:00:00 AM')
GROUP BY tblWorkorders.ProcessedDate, WorkOrderParts.PartCost,
         WorkOrderParts.PartQTY, tblParts.fldMfgPartNumber, tblWorkorders.ID
ORDER BY tblParts.fldMfgPartNumber

返回的数据:

PartQTY  PartCost  PartsTotal  fldMfgPartNumber 
-------  --------  ----------  ----------------
1        $48.71    $48.71      1878042C91
1        $48.71    $48.71      1878042C91
1        $48.71    $48.71      1878042C91
1        $11.82    $11.82      1R1804
1        $11.82    $11.82      1R1804
1        $11.82    $11.82      1R1804
4        $255.39   $1,021.56   2697041
1        $8.94     $8.94       3719

如何修改我的 SQL 查询以返回以下结果:

PartQTY  PartCost  PartsTotal  fldMfgPartNumber 
-------  --------  ----------  ----------------
3        $48.71    $146.13     1878042C91
3        $11.82    $35.46      1R1804
4        $255.39   $1,021.56   2697041
1        $8.94     $8.94       3719
4

3 回答 3

0
SELECT SUM(WorkOrderParts.PartQTY) as PartQTY, '$' + CONVERT(varchar(22),
       WorkOrderParts.PartCost, 1) AS PartCost, '$' + CONVERT(varchar(22), 
       WorkOrderParts.PartQTY * WorkOrderParts.PartCost, 1) AS PartsTotal,
       tblParts.fldMfgPartNumber
FROM  tblWorkorders
INNER JOIN WorkOrderParts ON tblWorkorders.ID = WorkOrderParts.WorkOrderId
INNER JOIN tblParts ON WorkOrderParts.PartId = tblParts.ID
WHERE (tblWorkorders.ProcessedDate BETWEEN '5/1/2012 12:00:00 AM'
                                       AND '5/2/2012 12:00:00 AM')
GROUP BY tblWorkorders.ProcessedDate, WorkOrderParts.PartCost,
         tblParts.fldMfgPartNumber, tblWorkorders.ID
ORDER BY tblParts.fldMfgPartNumber
于 2012-05-07T22:15:17.743 回答
0

你的意思是类似的GROUP BY声明吗?

select sum(PartQTY), avg(PartCost), sum(PartsTotal), fldMfgPartNumber from Table group by fldMfgPartNumber

于 2012-05-07T22:11:33.063 回答
0

假设 tblParts.fldMfgPartNumber 是您要分组的列(我猜是 - 基于您的预期结果集),那么您可以尝试以下操作:

    SELECT SUM(WorkOrderParts.PartQTY), 
   '$' + CONVERT(VARCHAR(22), MIN(WorkOrderParts.PartCost), 1) AS PartCost, 
   '$' + CONVERT(VARCHAR(22), SUM(WorkOrderParts.PartQTY * WorkOrderParts.PartCost), 1) AS PartsTotal, 
    tblParts.fldMfgPartNumber 
 FROM tblWorkorders 
 INNER JOIN WorkOrderParts ON tblWorkorders.ID = WorkOrderParts.WorkOrderId 
 INNER JOIN tblParts ON WorkOrderParts.PartId = tblParts.ID 
 WHERE (tblWorkorders.ProcessedDate BETWEEN '5/1/2012 12:00:00 AM' AND '5/2/2012 12:00:00 AM') 
 GROUP BY tblParts.fldMfgPartNumber;
于 2012-05-07T22:11:57.700 回答