我有一个SALES RECEIPT表和一个RETURNS表。两个表中的信息彼此独立,除了每个表都具有SALES_REP表中的Rep_ID属性。
在第一个子查询中,我每月从每个销售人员的SALES RECEIPT表中计算“总销售额”和“总佣金”的总和。
在第二个子查询中,我每月从每个销售人员的RETURNS表中计算“退货销售”和“失去佣金”的总和。
在主查询中,我想每月为每位销售人员显示“总销售额”, “总佣金”,“退货销售”,“失去佣金”和“净佣金”。 “网络委员会”是“总委员会”减去“失去委员会”。
使用我的代码,我收到错误消息:
" Microsoft Access数据库引擎找不到输入表或查询'totSales'。确保它存在并且其名称拼写正确。“
Select
totSales.Year-Month as [Month/Year],
SALES_REP.rep_name as [Sales Person],
SUM(totSales.[Total Sales]) as [Total Sales],
SUM(totSales.[Gross Commission]) as [Gross Commission],
SUM(totReturns.[Return Sales],0) as [Sales Returns],
SUM(totReturns.[Lost Commission],0) as [Lost Commission],
Round([Gross Commission] - [Lost Commission],2) AS [Net Commission],
(SELECT
Format(SALES_RECEIPT.sale_date,'yyyy-mm') AS [Year-Month],
SALES_RECEIPT.rep_id,
( SALES_RECEIPT.selling_price * SALES_RECEIPT.quantity) AS [Total Sales],
((Nz(SALES_RECEIPT.selling_price, 0) * Nz( SALES_RECEIPT.quantity, 0)) * (Nz(SALES_RECEIPT.commission_percent, 100) * 0.001)) AS [Gross Commission]
FROM
SALES_RECEIPT
WHERE
SALES_RECEIPT.sale_date Between #1/1/2000# And #12/31/2050#) AS totSales,
(SELECT
RETURNS.rep_id,
(Nz(RETURNS.selling_price * RETURNS.quantity)) AS [Sales Returns],
((Nz(RETURNS.selling_price, 0) * Nz(RETURNS.quantity, 0)) * (Nz(RETURNS.commission_percent, 100) * 0.001)) AS [Lost Commission]
FROM
RETURNS
WHERE
RETURNS.return_date Between #1/1/2000# And #12/31/2050#) As totReturns
From (totSales
LEFT JOIN totReturns on totReturns.rep_id = totSales.rep_id)
INNER JOIN SALES_REP ON totSales.REP_ID = SALES_REP.rep_id
Group By totSales.Year-Month, totSales.rep_name;
以下查询返回每月销售佣金数据,但仅限一位销售人员。我希望上面的查询返回与下面的查询完全相同的结果,但对于所有销售人员而言,不仅仅是一个销售人员。
SELECT Format(DatePart("m",months.month_start),"00") & "/" & Year(months.month_start) AS [Month/Year],
(SELECT SALES_REP.rep_name FROM SALES_REP WHERE SALES_REP.rep_id = 1) AS [Sales Person],
(select Round(Nz(Sum(sales_receipt.SELLING_PRICE * sales_receipt.quantity),0) ,2)
FROM SALES_RECEIPT INNER JOIN SALES_REP ON SALES_REP.REP_ID = SALES_RECEIPT.REP_ID
WHERE SALES_RECEIPT.[SALE_DATE] between months.month_start and months.month_end and SALES_REP.rep_id = 1) AS [Total Sales],
(SELECT Round((Sum(((Nz(SALES_RECEIPT.SELLING_PRICE,0)*Nz(sales_receipt.quantity,0))*(Nz(sales_receipt.commission_percent,100)*0.001)))),2)
FROM SALES_RECEIPT INNER JOIN SALES_REP ON SALES_REP.REP_ID = SALES_RECEIPT.REP_ID
WHERE SALES_RECEIPT.[SALE_DATE] between months.month_start and months.month_end and SALES_REP.rep_id = 1) AS [Gross Commission],
(SELECT Round(Nz(Sum(returns.selling_price * returns.quantity), 0),2)
FROM (returns inner JOIN inventory ON INVENTORY.INVENTORY_ID = returns.INVENTORY_ID)
LEFT JOIN SALES_REP ON SALES_REP.REP_ID = returns.REP_ID
WHERE returns.return_date between months.month_start and months.month_end AND SALES_REP.rep_id = 1) AS [Sales Returns],
(SELECT Round(Nz((Sum(((Nz(returns.SELLING_PRICE,0)*Nz(returns.quantity,0))*(Nz(returns.commission_percent,100)*0.001)))),0),2)
FROM (returns inner JOIN inventory ON INVENTORY.INVENTORY_ID = returns.INVENTORY_ID)
LEFT JOIN SALES_REP ON SALES_REP.REP_ID = returns.REP_ID
WHERE returns.return_date between months.month_start and months.month_end AND SALES_REP.rep_id = 1) AS [Lost Commission],
(SELECT Round((Sum(((Nz(SALES_RECEIPT.SELLING_PRICE,0)*Nz(sales_receipt.quantity,0))*(Nz(sales_receipt.commission_percent,100)*0.001)))),2)
FROM SALES_RECEIPT INNER JOIN SALES_REP ON SALES_REP.REP_ID = SALES_RECEIPT.REP_ID WHERE SALES_RECEIPT.[SALE_DATE] between months.month_start and months.month_end and SALES_REP.rep_id = 1) - (SELECT Round(Nz((Sum(((Nz(returns.SELLING_PRICE,0)*Nz(returns.quantity,0))*(Nz(returns.commission_percent,100)*0.001)))),0),2)
FROM(returns inner JOIN inventory ON INVENTORY.INVENTORY_ID = returns.INVENTORY_ID)
LEFT JOIN SALES_REP ON SALES_REP.REP_ID = returns.REP_ID
WHERE returns.return_date between months.month_start and months.month_end AND SALES_REP.rep_id = 1) AS [Net Commission]
FROM
(SELECT DateSerial(Year(sale_date), Month(sale_date), 1) AS month_start,
DateAdd("d", -1, DateSerial(Year(sale_date), Month(sale_date) + 1, 1)) AS month_end
FROM SALES_RECEIPT
WHERE sale_date between #1/1/2000# And #12/31/2100#
GROUP BY Year(sale_date), Month(sale_date)) AS months;
答案 0 :(得分:1)
移动两个子查询,使它们位于查询的NSThread.sleepForTimeInterval(0.4) // works
NSThread.sleepForTimeInterval(0.3) // Does not work
... FROM
部分。正如@Leviathan所指出的那样,查询目前正在将它们视为字段(当然,它们并非如此)。
另外:请确保在JOIN
子句中包含您要分组的所有字段(并且不要将它们别名 - 您将收到错误)。
这是对第一个查询的抨击。当你进行多次连接时,访问是如何使用圆括号一样,如果第一次没有工作就会道歉:
GROUP BY
另外:作为一种风格的东西:通常首选将逗号分隔线上的多个字段与其后面的字段分开,例如:
Select
totSales.Year-Month as [Month/Year],
SALES_REP.rep_name as [Sales Person],
SUM(totSales.[Total Sales]) as [Total Sales],
SUM(totSales.[Gross Commission]) as [Gross Commission],
SUM(totReturns.[Return Sales],0) as [Sales Returns],
SUM(totReturns.[Lost Commission],0) as [Lost Commission],
Round(TotCommissions - TotLostCommissions,2) AS [Net Commission]
FROM
SALES_REP
INNER JOIN
(
(SELECT
Format(SALES_RECEIPT.sale_date,'yyyy-mm') AS [Year-Month],
SALES_RECEIPT.rep_id,
( SALES_RECEIPT.selling_price * SALES_RECEIPT.quantity) AS [Total Sales],
((Nz(SALES_RECEIPT.selling_price, 0) * Nz( SALES_RECEIPT.quantity, 0)) * (Nz(SALES_RECEIPT.commission_percent, 100) * 0.001)) AS [Gross Commission]
FROM
SALES_RECEIPT
WHERE
SALES_RECEIPT.sale_date Between #1/1/2000# And #12/31/2050#) AS totSales
LEFT JOIN
(SELECT
RETURNS.rep_id,
(Nz(RETURNS.selling_price * RETURNS.quantity)) AS [Returns Sales],
((Nz(RETURNS.selling_price, 0) * Nz(RETURNS.quantity, 0)) * (Nz(RETURNS.commission_percent, 100) * 0.001)) AS [Lost Commission]
FROM
RETURNS
WHERE
RETURNS.return_date Between #1/1/2000# And #12/31/2050#) As totReturns
on totReturns.rep_id = totSales.rep_id
)
ON totSales.REP_ID = SALES_REP.rep_id
Group By
totSales.Year-Month
,SALES_REP.rep_name
,Round(TotCommissions - TotLostCommissions,2)
;
不
SELECT
SomeField
,SomeOtherField
有些人对此非常不满意,所以尝试养成用逗号引导自己不必要的批评的习惯可能是一个好主意。