我使用的是SQL Server 2005,我希望得到1张发票的总价值
当前输出:
Invoice price
-------------------
1234 2000
1234 2500
9012 3000
3456 1000
但是我的选择字段(这是为了价格)已经很长了,而且我不知道如何在其上添加总和,
RIGHT('000000000000'+ convert(varchar,cast(A.IVC_ForeignGrossAmt as decimal(9,2))),13) as IVC_ForeignGross,
预期输出应为:
Invoice price
-------------------
1234 4500
1234 4500
9012 3000
3456 1000
答案 0 :(得分:2)
使用相关子查询,如下所示:
SELECT
Invoice,
(SELECT SUM(t2.price)
FROM table1 t2
WHERE t1.invoice = t2.invoice) price
FROM Table1 t1;
这会给你:
| INVOICE | PRICE |
-------------------
| 1234 | 4500 |
| 1234 | 4500 |
| 9012 | 3000 |
| 3456 | 1000 |
答案 1 :(得分:2)
您可以将SUM-OVER
与PARTITION BY Invoice
一起使用(类似于Group By
):
SELECT
Invoice, SUM(price) OVER (PARTITION BY Invoice) AS price
FROM
dbo.Table
(但是,不确定问题的RIGHT('000000000000...
部分)
答案 2 :(得分:0)
你的意思是出局意图,
Invoice price
-------------------
1234 4500
9012 3000
3456 1000
您可以尝试
SELECT INVOICE.ID, SUM(PRICE) FROM INVOICE
GROUP BY INVOICE.ID
答案 3 :(得分:0)
SELECT Invoice, SUM(price) FROM table1 GROUP BY Invoice;
答案 4 :(得分:0)
根据发票上的SUM
,您只需要GROUP BY
的价格,并根据需要格式化价格。
SELECT A.Invoice,
RIGHT('000000000000'+ convert(varchar,cast(SUM(A.IVC_ForeignGrossAmt)
as decimal(9,2))),13) as IVC_ForeignGross
FROM yourTable A
GROUP BY Invoice;