在加入的 table 中计数第一个 table
Counting first table in joined tables
我的 sql 查询有问题。我有 2 tables (a. Sales, b. SalesDetail)。行数:
Sales SalesDetail
------ -----------
SaleNo : MakeDate IDSalesDetail: SaleNo : ItemCode : ItemPrice : ItemQty
--------:--------- -------------:--------:----------:-----------:--------:
1 :12/18/2015 1 : 1 : 001 : 100 : 5
2 :12/28/2015 2 : 2 : 001 : 100 : 10
3 :01/08/2016 3 : 3 : 001 : 100 : 20
4 : 2 : 002 : 50 : 10
5 : 1 : 002 : 50 : 5
我需要提供类似
结果的查询
TotalReceipt : Year : Amount :
-------------:-------:--------:
2 : 2015 : 2250 :
1 : 2016 : 2000 :
我使用了这个查询
SELECT COUNT(Sales.SaleNo) AS TotalReceipt,
FORMAT(Sales.MakeDate, 'yyyy') AS [Year],
SUM(SalesDetail.ItemQty * SalesDetail.ItemPrice) AS Amount
FROM (Sales INNER JOIN SalesDetail ON Sales.SaleNo = SalesDetail.SaleNo)
GROUP BY FORMAT(Sales.MakeDate, 'yyyy')
但它给了我这个
TotalReceipt : Year : Amount :
-------------:-------:--------:
4 : 2015 : 2250 :
1 : 2016 : 2000 :
由于 Count(*)
影响 SalesDetail table。
如果有人能解决我的问题,我将不胜感激
谢谢,祝你有个愉快的一天!
尝试在计数时使用 distinct,例如:
SELECT COUNT(DISTINCT SalesDetail.NoSale) AS TotalReceipt,
FORMAT(Sales.MakeDate, 'yyyy') AS [Year],
SUM(SalesDetail.ItemQty * SalesDetail.ItemPrice) AS Amount
FROM (Sales INNER JOIN SalesDetail ON Sales.SaleNo = SalesDetail.SaleNo)
GROUP BY FORMAT(Sales.MakeDate, 'yyyy')
刚刚自己找到了自己的答案公式,如果你想知道如何检查这个
SELECT COUNT(*) AS TotalReceipt, Format(MakeDate, 'yyyy') AS [Year], SUM(Total) AS Amount
FROM (
SELECT SaleNo, SUM(ItemQty * ItemPrice) AS Total
FROM SalesDetail
GROUP BY SaleNo) DetailSum
INNER JOIN Sales ON Sales.SaleNo = DetailSum.SaleNo
GROUP BY Format(MakeDate, 'yyyy')
我的 sql 查询有问题。我有 2 tables (a. Sales, b. SalesDetail)。行数:
Sales SalesDetail
------ -----------
SaleNo : MakeDate IDSalesDetail: SaleNo : ItemCode : ItemPrice : ItemQty
--------:--------- -------------:--------:----------:-----------:--------:
1 :12/18/2015 1 : 1 : 001 : 100 : 5
2 :12/28/2015 2 : 2 : 001 : 100 : 10
3 :01/08/2016 3 : 3 : 001 : 100 : 20
4 : 2 : 002 : 50 : 10
5 : 1 : 002 : 50 : 5
我需要提供类似
结果的查询TotalReceipt : Year : Amount :
-------------:-------:--------:
2 : 2015 : 2250 :
1 : 2016 : 2000 :
我使用了这个查询
SELECT COUNT(Sales.SaleNo) AS TotalReceipt,
FORMAT(Sales.MakeDate, 'yyyy') AS [Year],
SUM(SalesDetail.ItemQty * SalesDetail.ItemPrice) AS Amount
FROM (Sales INNER JOIN SalesDetail ON Sales.SaleNo = SalesDetail.SaleNo)
GROUP BY FORMAT(Sales.MakeDate, 'yyyy')
但它给了我这个
TotalReceipt : Year : Amount :
-------------:-------:--------:
4 : 2015 : 2250 :
1 : 2016 : 2000 :
由于 Count(*)
影响 SalesDetail table。
如果有人能解决我的问题,我将不胜感激
谢谢,祝你有个愉快的一天!
尝试在计数时使用 distinct,例如:
SELECT COUNT(DISTINCT SalesDetail.NoSale) AS TotalReceipt,
FORMAT(Sales.MakeDate, 'yyyy') AS [Year],
SUM(SalesDetail.ItemQty * SalesDetail.ItemPrice) AS Amount
FROM (Sales INNER JOIN SalesDetail ON Sales.SaleNo = SalesDetail.SaleNo)
GROUP BY FORMAT(Sales.MakeDate, 'yyyy')
刚刚自己找到了自己的答案公式,如果你想知道如何检查这个
SELECT COUNT(*) AS TotalReceipt, Format(MakeDate, 'yyyy') AS [Year], SUM(Total) AS Amount
FROM (
SELECT SaleNo, SUM(ItemQty * ItemPrice) AS Total
FROM SalesDetail
GROUP BY SaleNo) DetailSum
INNER JOIN Sales ON Sales.SaleNo = DetailSum.SaleNo
GROUP BY Format(MakeDate, 'yyyy')