如何在sql中实现excel sumif类型函数?

时间:2016-08-03 22:01:36

标签: sql sql-server-2008 tsql

如何根据另一列中的某组条件在列中添加不同的值? 与使用Sumif

在excel上完成的相同

2 个答案:

答案 0 :(得分:1)

Select SalesAgent
      ,JanSales = sum(case when SalesMonth=1 then Sales else 0 end)
      ,FebSales = sum(case when SalesMonth=2 then Sales else 0 end)
      ... {march - november}
      ,DecSales = sum(case when SalesMonth=12 then Sales else 0 end)
      ,Total    = sum(Sales)
 From SalesData
 Group By SalesAgent
 Order By SalesAgent

答案 1 :(得分:0)

要为符合特定条件的行汇总列的所有值:

SELECT Sum(value1) FROM Table WHERE value2=something