如何在sql中获取一列子查询结果列的总和

时间:2017-05-29 13:25:24

标签: php mysql

这是我在这个子查询中的查询给出了一些输出列,我想要总金额的总和

select x.*,sum(x.AmountCommited)
from
(select
a.TrustMemName,
a.DonorFirstName,
a.AmountCommited,
a.YourCommitmentId,
b.AmountFulfilled,
SUM( CASE WHEN a.DateFulfilled1 between "2017-01-01" and "2017-03-31"
      THEN a.AmountFulfilled          
 END) as quarter1,
SUM( CASE WHEN a.DateFulfilled1 between "2017-04-01" and "2017-06-30"
      THEN a.AmountFulfilled          
 END) as quarter2,
SUM( CASE WHEN a.DateFulfilled1 between "2017-07-01" and "2017-09-30" 
      THEN a.AmountFulfilled          
 END) as quarter3,
SUM( CASE WHEN a.DateFulfilled1 between "2017-10-01" and "2017-12-31" 
      THEN a.AmountFulfilled          
 END) as quarter4
 from
 commitmentcomplete as a left join commitmentsummary as b on 
  a.YourCommitmentId=b.YourCommitmentId
  where a.TrustId=2521
 group by a.YourCommitmentId
 ) as x 

------------------------- out put ------------------

  "TrustMemName"        "DonorFirstName"    "AmountCommited"    "YourCommitmentId"  "AmountFulfilled"   "quarter1"          "quarter2"  "quarter3"  "quarter4"
  "nithin"              "xdfcdg"             "4567"                 "171"       "4500"                   "3000"                 "1500"            \N          \N
  "virendra"            "ghgf"              "20000"                 "172" "10000"                               \N       \N     \N       \N
  "virendra"            "ghfgh"            "30000"                  "173"   "15000"                             \N                       "15000"    \N  \N

这是子查询结果,我想要金额的总和

1 个答案:

答案 0 :(得分:0)

创建上面的子查询视图,然后使用上面的子查询加入该视图,我们将得到结果