需要T-SQL代码用于以下伪代码

时间:2011-09-21 12:36:34

标签: tsql

Table A: | BillToName | Current| Total |
          -----------------------------
         | Company29  | N      | 100   |
          -----------------------------
         | Company02  | Y      | 80    |
          -----------------------------

我需要T-SQL用于以下伪代码:

SELECT BillToName,SUM(总计*,无论状态是“Y”还是“N”)WHERE Current ='Y'

随意提问。谢谢!

3 个答案:

答案 0 :(得分:3)

我认为你的意思是:

SELECT BillToName, SUM(Total) 
FROM Table
WHERE BillToName IN (SELECT BillToName FROM Table WHERE Current = 'Y')
GROUP BY BillToName

答案 1 :(得分:1)

SELECT a.BillToName, b.Total
FROM TableA a
CROSS APPLY (SELECT SUM(Total) Total FROM TableA) b
WHERE Current = 'Y'

答案 2 :(得分:0)

select billtoname,(select sum(total) from table)
where current='Y'