如何总结我在此SQL中所做的计算?我想为这两个数据做总结

时间:2018-01-03 04:16:33

标签: sql

如何总结我在此SQL中所做的计算?

select
    ac_invc."fd_st",
    (SUM(net_amount)-(ac_invc."total_amount"-ac_invc."balance_amount")) AS total_amt_due
from "public"."ac_invc" ac_invc 
INNER JOIN "public"."ac_invc_item" ac_invc_item ON ac_invc."id" = ac_invc_item."invoice_id"
where ac_invc."fd_st" = 16
group by ac_invc."fd_st",ac_invc."total_amount",ac_invc."balance_amount"

这个输出变成这样:

this output became like this:

1 个答案:

答案 0 :(得分:-1)

试试这个:

     SELECT TB.FD_ST,
           SUM (TB.TOTAL_AMT_DUE)
      FROM (SELECT   AC_INVC."fd_st" AS FD_ST,
                     (  SUM (NET_AMOUNT)
                      - (AC_INVC."total_amount" - AC_INVC."balance_amount") )
                                                                 AS TOTAL_AMT_DUE
                FROM "public"."ac_invc" AC_INVC INNER JOIN "public"."ac_invc_item" AC_INVC_ITEM ON AC_INVC."id" =
                                                                                                       AC_INVC_ITEM."invoice_id"
               WHERE AC_INVC."fd_st" = 16
            GROUP BY AC_INVC."fd_st") TB
      Group by TB.FD_ST