我已经通过ODBC将Access数据库文件连接到MS SQL 05.我有一个包含5个数据字段的表:
示例:
Date Receipt No. Customer No. Item No. Net Amount
---- ----------- ------------ -------- ----------
09/01/2016 M00001 111111 123 100
09/01/2016 M00001 111111 XXX 0
09/01/2016 M00002 222222 123 100
09/01/2016 M00002 222222 XXX 0
如果商品编号(相同的收据编号)有一个名为“XXX”的文本,我想得到的数据总和总数,并计算10%的净金额总和。
对于上面的示例,它将获得10美元的收据结果。 M00001。
答案 0 :(得分:0)
如果您想要每个收据的净金额总和,其项目编号为' XXX',您需要获取那些其商品名为&no;' XXX' 因此,以下查询将给出此结果 SELECT ReceiptNo FROM TableName WHERE ItemNo =" XXX"
现在,要找到总金额的10%,您有2个选项
1)使用innerjoin
SELECT B.ReceiptNo, SUM(A.NetAmount)/10 FROM TableName A INNER JOIN
(SELECT ReceiptNo FROM TableName WHERE ItemNo = "XXX")B
ON A.ReceiptNo = B.ReceiptNo GROUP BY B.ReceiptNo
2)使用In Clause
SELECT ReceiptNo, SUM(A.NetAmount)/10 FROM TableName WHERE ReceiptNo
IN (SELECT ReceiptNo FROM TableName WHERE ItemNo = "XXX")
答案 1 :(得分:0)
非常感谢你“KJV111”!
您能告诉我们在哪里放置此查询吗?它是在我查询的SQL视图中吗? - 帕特里克,我手头有这个问题......
SELECT [dbo_xxx$Trans_ Sales Entry].Date, [dbo_xxx$Trans_ Sales Entry].[Receipt No_], [dbo_xxx$Trans_ Sales Entry].[Customer No_], [dbo_xxx$Trans_ Sales Entry].[Net Amount], [dbo_xxx$Trans_ Sales Entry].[Item No_]
FROM [dbo_xxx$Trans_ Sales Entry]
GROUP BY [dbo_xxx$Trans_ Sales Entry].Date, [dbo_xxx$Trans_ Sales Entry].[Receipt No_], [dbo_xxx$Trans_ Sales Entry].[Customer No_], [dbo_xxx$Trans_ Sales Entry].[Net Amount], [dbo_xxx$Trans_ Sales Entry].[Item No_]
HAVING ((([dbo_xxx$Trans_ Sales Entry].Date)>#7/31/2016#));