使用SQL Server计算金额

时间:2017-04-24 18:42:35

标签: sql-server

我正在使用SQL服务器。我已将Excel文件表导入SQL服务器。在此表中,我有三个列字段,如下所示:

AMount    Name              Type
277.6    Arasu              Tax
119.23   Karthi             Tax
446.43   Selvam             Tax
381.63   Alex               Tax
119.23   Karthi             income
446.43   Selvam             income
381.63   Alex               income

现在我想计算基于金额的类型值。如何计算单独的税额类型和收入类型?如何单独获得税收和收入金额?

4 个答案:

答案 0 :(得分:2)

类似的东西:

SELECT SUM(Amount), Type
FROM yourTableNameHere
GROUP BY Type

这是你要问的吗?

答案 1 :(得分:0)

你的意思是这样的吗?:

select A.Name, A.AMount as TAX_Amount, B.AMount as Income_Amount
from yourTable A left join yourTable B
    on A.Name = B.Name
where A.Type = 'Tax'
    and B.Type = 'income'
order by A.Name;

答案 2 :(得分:0)

您可以将TAX和INCOME的总和分开:

select sum(AMount),Type from taxincome group by Type;

(taxincome就是我所说的表格)。

答案 3 :(得分:0)

;WITH cte(AMount,Name,Type)
AS
(
SELECT 277.6  ,'Arasu'  ,'Tax'      Union All
SELECT 119.23 ,'Karthi' ,'Tax'      Union All
SELECT 446.43 ,'Selvam' ,'Tax'      Union All
SELECT 381.63 ,'Alex'   ,'Tax'      Union All
SELECT 119.23 ,'Karthi' ,'income'   Union All
SELECT 446.43 ,'Selvam' ,'income'   Union All
SELECT 381.63 ,'Alex'   ,'income'   
)
SELECT * ,SUM(AMount)OVER(Partition by Type)TotalIncome_TaxAmount From
(
SELECT Name,Type,SUM(AMount)AMount FROM cte
GROUP BY Name,Type
)Dt