-1

我有一个数据表:

                COLUMN:  City    Deposittype  DepositAmount                  
                ROWS :  city1     new          100
                        city1     new          200
                        city2     old          200
                        city2     old          100
                        city2     new          200
                         city3    new          100

想按城市分组,计算指定存款类型的存款金额总和。

例如,对于条件 depositType= new

我想要一行

               city1 city2 city3
                300   200   100

我想要具有特定存款类型的按城市分组的存款金额总和。即结果行应该将city1 city2 city3 作为列名,在该列名下,指定贷款类型的“Depositamounts”总和说Deposittype = new。

4

2 回答 2

0
SELECT City, SUM(DepositAmount) FROM myTable
WHERE Deposittype = 'new'
GROUP BY City
于 2012-10-20T11:01:15.993 回答
0
SELECT  
   (
    SELECT SUM(DepositAmount) FROM myTable 
    WHERE Deposittype = 'New' AND City = 'City1'
   ) AS City1,
   (
    SELECT SUM(DepositAmount) FROM myTable 
    WHERE Deposittype = 'New' AND City = 'City2'
   ) AS City2,
   (
    SELECT SUM(DepositAmount) FROM myTable 
    WHERE Deposittype = 'New' AND City = 'City3'
   ) AS City3

FROM    myTable
于 2012-10-20T11:01:52.783 回答