Sql Сумма связанных таблиц - PullRequest
0 голосов
/ 28 марта 2011

Это 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

Как я могу использовать сумму, чтобы получить сумму Севарег? как это

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

1 Ответ

0 голосов
/ 28 марта 2011

Это то, что вы хотите, используя GROUP BY и SUM?

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
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...