0
我試過尋找這個答案,但找不到一個。更改微軟訪問的cte
我有一個CTE我使用與數據庫中的2代數據表的SQL查詢。一個表的主鍵是另一個表中的外鍵,可以在第二個表中出現多次。我想對每個外鍵出現在第二個表中的次數進行計數,並將其列爲我的搜索結果中的總字段以及第一個表中的詳細信息。由於CTE不能在Access中工作,我已經調整了這個以在連接中使用子選擇,但它仍然不喜歡它在訪問中。
下面是表
CREATE TABLE [dbo].[Clients](
[ClientRef] [int] NOT NULL,
[Surname] [varchar](40) NULL,
[Forenames] [varchar](50) NULL,
[Title] [varchar](40) NULL,
CONSTRAINT [CLIE_ClientRef_PK] PRIMARY KEY CLUSTERED
(
[ClientRef] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
CREATE TABLE [dbo].[Policies](
[PolicyRef] [int] NOT NULL,
[ClientRef] [int] NULL,
CONSTRAINT [POLI_PolicyRef_PK] PRIMARY KEY CLUSTERED
(
[PolicyRef] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
的基本組成部分這是我的CTE
WITH CliPol (ClientRef, Plans) AS (SELECT ClientRef, COUNT(ClientRef) AS Plans FROM Policies GROUP BY ClientRef)
SELECT Clients.Surname, Clients.Forenames, Clients.Title, CliPol.Plans AS [No. of plans]
FROM Clients LEFT JOIN CliPol ON Clients.ClientRef = CliPol.ClientRef
ORDER BY Surname, Forenames;
這是我的調整查詢。
SELECT Clients.ClientRef, Clients.Surname, Clients.Forenames, Clients.Title , Plans.NoPlans
FROM Clients
LEFT JOIN
(SELECT ClientRef, COUNT(ClientRef) AS NoPlans FROM Policies GROUP BY ClientRef)
AS Plans ON Plans.ClientRef = Clients.ClientRef
ORDER BY Clients.Surname, Clients.Forenames
不幸的是訪問拋出錯誤#3131,「語法錯誤在FROM子句」,當我試圖運行查詢。
有誰知道我是怎麼做的訪問這項工作?
你得到什麼錯誤?嘗試更改連接子句中的列順序(「Clients.ClientRef = Plans.ClientRef」) – 2015-02-06 13:58:01
當你說Access不喜歡你的子查詢方法時,是否意味着它通過拋出錯誤信息來投訴? – HansUp 2015-02-06 13:58:06
嗨,對不起,是的,它告訴我在FROM子句中有一個語法錯誤(錯誤3131)。 – welshsteve147 2015-02-06 14:14:50