0

我正在使用 Access 2007 和 SQL 新手。我有一个表,我希望在其中识别客户名称和部件号匹配但位置不匹配的任何行。如果同一客户订购了相同的零件,但将其发送到另一个位置,则该应用程序可以轻松识别价格差异。如果我不包含最后一行代码,我的查询将返回以下信息:

   CustomerNumber CompanyName PartNumber CustomerName Location SumOfQuantity AvgOfPrice
   5            Prestige    1000-A-26631-0  FINLAND OY  FINLAND 1            $5.00
   5            Prestige    1000-A-26631-0  FINLAND OY  KARHULA 2            $10.00
   15           Prestige    1000-A-29230-0  Portland    (US)    2            $15.00
   3            Prestige    1000-A-29230-0  BARBOU      BARBOU  2            $5.00
   9            Prestige    1000-A-29230-0  MEXICO      MEXICO  6            $20.00

前两行正是我正在寻找的。CustomerName 和 PartNumber 匹配,但 Location 不同。但是,我的查询也返回 PartNumbers 匹配但 CustomerNames 不匹配的行。我相信我也必须为 CustomerName 添加一个类似的 Group By, HAVING COUNT 语句,但是我遇到了语法错误。我想解决方案非常简单,但无法弄清楚。任何人都可以帮助确定我收到错误的原因吗?

SQL:

SELECT [2-B: Avg Prices by Customer].[Customer Number], [2-B: Avg Prices by Customer].[Company Name], *
FROM [2-B: Avg Prices by Customer]
WHERE ((([2-B: Avg Prices by Customer].[Company Name])=[Forms]![Start Form]![Combo73]))
OR ((([2-B: Avg Prices by Customer].[Company Name])=[Forms]![Start Form]![List67])
AND (([2-B: Avg Prices by Customer].[Part Number]) In 
(SELECT [Part Number] FROM [2-B: Avg Prices by Customer] 
GROUP BY [Part Number] HAVING COUNT([Part Number]) >1)), 
[Customer Name] HAVING COUNT([Customer Name])>1))); ------ > **Syntax error here, missing operator**
4

1 回答 1

0
SELECT
  CustomerName,
  PartNumber
FROM [2-B: Avg Prices by Customer] p
GROUP BY CustomerName,PartNumber
HAVING COUNT(DISTINCT Location) > 1
于 2013-07-12T18:22:45.380 回答