sql选择两个where子句的总和

时间:2012-01-17 16:39:37

标签: sql select sum where

SELECT Projects.Projectid, Projects.ProjectNumber, Projects.ProjectName,
       Projects.ProjectBudgetedIS, Projects.ProjectSpentIS, 
       Projects.ProjectBudgetedBusiness, Projects.PorjectSpentBusiness, Project.Status,
       ProjectStatus.Status AS Expr1

FROM Projects 
     INNER JOIN ProjectStatus ON  Projects.Status = ProjectStatus.StatusID 
WHERE Projects.Status = @Status

所以我想要做的是获取一个名为发票的表的总和,该表有一个名为ISorBusiness的字段和一个名为totalspent的字段,并将该数据存储在相应字段的项目表格中。因此,当我收到向IS收取的发票时,需要支付该金额并将其汇入Projects.ProjectSpentIS,如果我收到了与业务相关的发票,则会将其汇总到Projects.ProjectBudgetedBusiness。< / p>

我知道这应该是一件容易的事,对于这个虚假的问题感到抱歉。提前谢谢!

1 个答案:

答案 0 :(得分:2)

我会做类似的事情:

SELECT  SUM(CASE WHEN (IsOrBusiness = 'IS') THEN totalSpent ELSE 0 END) AS IsSpent, 
        SUM(CASE WHEN (IsOrBusiness = 'Business') THEN totalSpent ELSE 0 END) AS BusinessSpent
FROM Invoices

显然,用法取决于您是尝试编写插入查询还是选择此数据作为您发布的选择查询的一部分。