2012-01-06 64 views
2

執行我的代碼時出現此錯誤。這是什麼意思?我正在使用SQL Server Management Server Express。SQL Server和嵌套的SELECT:...')'附近的語法不正確?

消息102,級別15,狀態1,行7

附近有語法錯誤 ')'。


declare @start datetime 
declare @end datetime 
set @start = '2012/01/02' 
set @end = '2012/01/06' 


SELECT 
SUM(VCount) as [Total], 
vdate, 
(select COUNT(VIP) From (SELECT DISTINCT(VIP) FROM dbo.Visiter)) as [IP3] 
FROM dbo.Visiter 
where VDate between @start and @end 
GROUP BY VDate 

回答

5

在SQL Server中,當你在FROM子句中使用派生表(子查詢),你必須給派生表的別名:

SELECT 
SUM(VCount) as [Total], 
vdate, 
-- add an alias in the next line 
(select COUNT(VIP) From (SELECT DISTINCT(VIP) FROM dbo.Visiter) AS a) as [IP3] 
FROM dbo.Visiter 
where VDate between @start and @end 
GROUP BY VDate 

參考:FROM clause

2

我覺得這

(select COUNT(VIP) From (SELECT DISTINCT(VIP) FROM dbo.Visiter)) as [IP3] 

是更好地轉化爲

(select COUNT(DISTINCT VIP) FROM dbo.Visiter) as [IP3] 

據我所知,MySQL的,至少說Every derived table must have its own alias這樣,你的子查詢早該樣子

(select COUNT(v) From (SELECT DISTINCT(VIP) AS v FROM dbo.Visiter) AS tmp1) as [IP3] 

但短 - 好=)

+0

ID做的和得到這個: 消息156,15級,狀態1,11號線 語法錯誤關鍵字接近 'SELECT'。 Msg 102,Level 15,State 1,Line 11 ')'附近語法不正確。 – user1133937 2012-01-06 08:55:07

+0

是的,抱歉,修正。已經測試=) – shybovycha 2012-01-06 08:56:32

+0

我想獲得唯一訪客的數量,但這讓我總結所有獨特的訪客。請幫助我(對不起我弱的英語; D) – user1133937 2012-01-06 09:21:37

相關問題