2011-07-07 85 views
1

嗨任何一個可以給我解釋一下什麼毛病我查詢SQL Server查詢語法問題

SELECT  
    cDispatcherName AS Name, 
    cCallNature AS EmergencyCall, 
    COUNT(cCallNature) AS Total, 
    (
     SELECT 
      COUNT(cCallNature) 
      FROM CallInfo 
      WHERE (cCallNature ='Regular call') 
     GROUP BY cCallNature 
    ) AS Expr1 
FROM   CallInfo 
WHERE  (cCallNature = 'Emergency call') 
GROUP BY cDispatcherName, cCallNature 

給出了這樣的錯誤消息:

Errormsg::There was an error parsing the query. [ Token line number = 1,Token line offset = 93,Token in error = SELECT ]

+1

您是否收到任何錯誤消息? –

+0

似乎有效的語法。問題是什麼? –

+0

Yes.My querySELECT cDispatcherName AS Name,cCallNature AS EmergencyCall,COUNT(cCallNature)AS Total,(SELECT COUNT(cCallNature)FROM CallInfo WHERE cCallNature ='Regular call'GROUP BY cCallNature from Callinfo)AS Expr1 FROM CallInfo WHERE(cCallNature ='緊急呼叫')GROUP BY cDispatcherName,cCallNature Errormsg ::解析查詢時出錯。 [令牌行號= 1,令牌行偏移= 93,令牌出錯= SELECT]我正在處理Sqlserverce Compactedtion – Ram

回答

0

我覺得這個查詢可以在一個更好的寫但我可以看到你沒有指定(DispatcherName)的子查詢的主要問題。

所以更改子查詢,如下

(
     SELECT 
      COUNT(cCallNature) 
      FROM CallInfo rcall 
      WHERE (cCallNature ='Regular call' and rcall.cDispatcherName =CallInfo.CallInfo) 

    ) AS Expr1 

你也可以改變所有查詢(但是這取決於你的需求)

SELECT  
    cDispatcherName AS Name, 
    COUNT(cCallNature) AS EmergencyCallTotal, 
    (
     SELECT 
      COUNT(cCallNature) 
      FROM CallInfo rcall 
      WHERE (cCallNature ='Regular call' and rcall.cDispatcherName =CallInfo.CallInfo) 
    ) AS RegularcallTotal 
FROM   CallInfo 
WHERE  (cCallNature = 'Emergency call') 
GROUP BY cDispatcherName 
+0

對不起仍然沒有工作 – Ram

1

它不是你的後完全清楚從原來​​的查詢,但是你不能在SQL-CE中使用嵌套選擇,除非它們返回一個集合(而不是標量值),所以應該嘗試類似;

SELECT  
     CallInfo.cDispatcherName AS Name, 
     cCallNature AS EmergencyCall, 
     COUNT(cCallNature) AS Total, 
     SUB.CNT AS RegularCalls 
    FROM CallInfo 
    LEFT JOIN (
     SELECT 
      cDispatcherName, 
      COUNT(*) AS CNT 
     FROM CallInfo 
     WHERE cCallNature ='Regular call' 
     GROUP BY cDispatcherName 
    ) AS SUB ON CallInfo.cDispatcherName = SUB.cDispatcherName 

    WHERE cCallNature = 'Emergency call' 
    GROUP BY CallInfo.cDispatcherName, CallInfo.cCallNature, SUB.CNT 
+0

Thanx非常爲我工作 – Ram