Sql Sum OF相关表

时间:2011-03-28 14:55:53

标签: sql sum

这是SQL语句:

SELECT
  dbo.tblWorkflow.WorkflowName,
  ISNULL((SELECT SUM(dbo.tblSchemaOperation_Execution.ExecutedQuantity * dbo.tblOperationItemPrice.UnitPrice) AS Amount
          FROM dbo.tblSchemaOperation_Execution 
          INNER JOIN dbo.tblOperationItemPrice ON dbo.tblSchemaOperation_Execution.OperationID = dbo.tblOperationItemPrice.OperationID
          GROUP BY dbo.tblSchemaOperation_Execution.SchemaYear, 
                   dbo.tblSchemaOperation_Execution.SchemaNO
          HAVING (dbo.tblSchemaOperation_Execution.SchemaNO = dbo.tblWorkflowActivtyInsatnce.SchemaNO) AND 
                (dbo.tblSchemaOperation_Execution.SchemaYear = dbo.tblWorkflowActivtyInsatnce.SchemaYear)), 0) AS ActualAmount

FROM dbo.tblWorkflowActivtyInsatnce 
INNER JOIN dbo.tblWorkflow ON dbo.tblWorkflowActivtyInsatnce.WorkflowID = dbo.tblWorkflow.WorkflowID 
INNER JOIN dbo.tblWorkflowActivity ON dbo.tblWorkflowActivtyInsatnce.WorkflowActivityID = dbo.tblWorkflowActivity.WorkflowActivityID

WHERE (dbo.tblWorkflowActivtyInsatnce.StartDate IS NOT NULL) 
  AND (dbo.tblWorkflowActivtyInsatnce.FinishDate IS NULL) 
  AND (dbo.tblWorkflowActivity.ActivityName <> 'Execution')

,输出为:

WorkFlowName  :   ActualAmount
------------  :    ------------
Sceco         :      28020.00
Sevareg       :       5005.00
Sevareg       :       1100.00

如何使用总和来获得Sevareg的总数?像这样

WorkFlowName  :   ActualAmount
------------  :    ------------
Sceco         :      28020.00
Sevareg       :       6105.00

1 个答案:

答案 0 :(得分:0)

这是您想要的,使用GROUP BYSUM吗?

SELECT WorkflowName, SUM(ActualAmount)
FROM (SELECT dbo.tblWorkflow.WorkflowName,
             ISNULL ((SELECT SUM(dbo.tblSchemaOperation_Execution.ExecutedQuantity * dbo.tblOperationItemPrice.UnitPrice) AS Amount 
                      FROM dbo.tblSchemaOperation_Execution 
                      INNER JOIN dbo.tblOperationItemPrice
                              ON dbo.tblOperationItemPrice.OperationID = dbo.tblSchemaOperation_Execution.OperationID
                      GROUP BY dbo.tblSchemaOperation_Execution.SchemaYear,
                               dbo.tblSchemaOperation_Execution.SchemaNO 
                      HAVING (dbo.tblSchemaOperation_Execution.SchemaNO = dbo.tblWorkflowActivtyInsatnce.SchemaNO) 
                         AND (dbo.tblSchemaOperation_Execution.SchemaYear = dbo.tblWorkflowActivtyInsatnce.SchemaYear)), 
                     0) AS ActualAmount

      FROM dbo.tblWorkflowActivtyInsatnce 
      INNER JOIN dbo.tblWorkflow 
              ON dbo.tblWorkflow.WorkflowID = dbo.tblWorkflowActivtyInsatnce.WorkflowID
      INNER JOIN dbo.tblWorkflowActivity
              ON dbo.tblWorkflowActivity.WorkflowActivityID = dbo.tblWorkflowActivtyInsatnce.WorkflowActivityID
      WHERE (dbo.tblWorkflowActivtyInsatnce.StartDate IS NOT NULL) 
        AND (dbo.tblWorkflowActivtyInsatnce.FinishDate IS NULL) 
        AND (dbo.tblWorkflowActivity.ActivityName <> 'Execution')
     ) AS x
GROUP BY dbo.tblWorkflow.WorkflowName