2016-04-03 69 views
-5

我遇到了我的邏輯問題。請幫忙。使用連接進行計數

我得到的錯誤是: Msg 8120, Level 16, State 1, Line 603 Column 'Client.LastName' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause. Msg 147, Level 15, State 1, Line 605 An aggregate may not appear in the WHERE clause unless it is in a subquery contained in a HAVING clause or a select list, and the column being aggregated is an outer reference.

我不知道如何做一個內部子查詢與計數功能。

  • 編寫存儲過程調用NoJobs返回客戶名字,姓氏和PhoneNumber所有還沒有工作的客戶端。不要使用連接。 (2分)
  • Go 
    Create Procedure NoJobs (@ClientID int = null) 
    as 
    
    If @ClientID is null 
    
    Begin 
    RaisError ('Must provide Client ID',16,1) 
    End 
    Else 
    Begin 
    Select FirstName,LastName,Phone from Client 
    group by FirstName,LastName,Phone 
    having ClientID = (Select Count(*) from Job where Count(*) = 0 group by ClientID) 
    End 
    Return 
    

    回答

    0

    在查詢的邏輯是方式關閉。您正在使用having子句將ClientID與計數進行比較......您如何期望這種方式起作用?

    什麼你想要做的是和having子句跳過組,並使用in運營商,而不是像一個條件:

    where ClientID not in (select ClientID from job) 
    
    +0

    太感謝你了,我做了otherquestions這種邏輯...我不知道我錯過了多少感覺:) – Lost