我有这样的查询:
SELECT
TC.F_Exhibition_Name,
T.F_Exhibitor_Name,
TC.F_CreditNoteNo,
TC.F_Description,
TC.F_Price,
TC.F_Qty,
TC.F_CNqty,
TC.F_Totalamt
FROM T_CreditNote TC
LEFT JOIN T_Exhibitor T ON
T.F_Exhibitor_Name = TC.F_Exhibitor_Name
WHERE TC.F_CreditNoteNo='CNINV100002'
输出看起来像这样
----------------------------------------------------------------------------------------------------------------------------------------------------
F_Exhibition_Name F_Exhibitor_Name F_CreditNoteNo F_Description F_Price F_Qty F_CNqty F_Totalamt
----------------------------------------------------------------------------------------------------------------------------------------------------
WORKSPACE 2015 AEP - Associacao Empresarial De Portugal CNINV100002 item1 12 5 8 96
WORKSPACE 2015 AEP - Associacao Empresarial De Portugal CNINV100002 item2 25 12 10 250
我希望最后添加一列[sum]
,其中F_total
金额之和。就我而言,我想展示sum(250+96)
= 346
。
我怎么能这样做?
答案 0 :(得分:5)
使用sorted_x1 = sorted(dt.items(), key=lambda k_v: (k_v[1]['score'], k_v[0]))
:
SUM OVER()
select TC.F_Exhibition_Name, T.F_Exhibitor_Name, TC.F_CreditNoteNo,
TC.F_Description, TC.F_Price,TC.F_Qty, TC.F_CNqty, TC.F_Totalamt,
sum(TC.F_Totalamt) OVER () AS SumOfTotalamt
from T_CreditNote TC
LEFT JOIN T_Exhibitor T on T.F_Exhibitor_Name=TC.F_Exhibitor_Name
where TC.F_CreditNoteNo='CNINV100002'
的窗口版本将两行都返回SUM
的总和。
答案 1 :(得分:3)
您可以使用SUM()
聚合函数,例如
SELECT
TC.F_Exhibition_Name,
T.F_Exhibitor_Name,
TC.F_CreditNoteNo,
TC.F_Description,
TC.F_Price,
TC.F_Qty,
TC.F_CNqty,
TC.F_Totalamt,
SUM(TC.F_Totalamt) as sum_amt
FROM T_CreditNote TC
LEFT JOIN T_Exhibitor T ON
T.F_Exhibitor_Name = TC.F_Exhibitor_Name
WHERE TC.F_CreditNoteNo='CNINV100002'
GROUP BY TC.F_Exhibition_Name,
T.F_Exhibitor_Name,
TC.F_CreditNoteNo,
TC.F_Description,
TC.F_Price,
TC.F_Qty,
TC.F_CNqty,
TC.F_Totalamt