2013-11-25 84 views
0

當我嘗試加入時,我收到了很多錯誤(可能是因爲我從來沒有和他們一起過)。TSQL加入表時出錯

我的存儲過程是非常簡單的:

SELECT * 
FROM 
    (SELECT DISTINCT 
     (A.whoAdded) AS whoAdded, 
     COUNT(A.tag) AS totalTags, 
     COUNT(DISTINCT A.data) AS totalSubmissions, 
     COUNT(DISTINCT B.data) AS totalSearches, 
     C.firstName AS firstName, 
     C.lastName AS lastName, 
     C.titleDesc AS titleDesc 
    FROM 
     Tags_Accounts AS A 
    INNER JOIN 
     Tags_Log AS B ON A.whoAdded = B.ntid 
    INNER JOIN 
     empTable AS C ON A.whoAdded = C.ntid 
    WHERE  
     whoAdded = @ntid 
    GROUP BY 
     whoAdded) AS a 
FOR XML PATH ('profile'), TYPE, ELEMENTS, ROOT ('root'); 

的錯誤我得到的總是相似的:

列「empTable.FirstName」無效在選擇列表中,因爲它是不包含在聚合函數或GROUP BY子句中。

我希望如果有人能指出爲什麼這個查詢不起作用或我可以做什麼來解決它,我可以修復所有其他嘗試,讓我在同一個地點。

+0

的錯誤是很明顯的:你包括第一個和最後一個名字,加上標題,而不會被分組他們。將它們添加到組中,將它們從select語句中刪除,或者對它們執行一些聚合。 – LittleBobbyTables

+0

我想我只是有點不清楚什麼時候你添加一些東西到條款 – SBB

+0

兩個方面的注意事項:**(1)**如果你的列'C.firstName'與你給它的別名相同( 'AS firstName'),那麼這個別名是毫無意義的,完全可以避免。 **(2)**我建議使用**更有意義的表別名而不僅僅是'A','B'和'C'--使用更明顯的東西:'Tags_Accounts AS ta','Tags_Logs AS tl','empTable AS e'或類似的東西!讓您的查詢更具可讀性,更易於理解! –

回答

1

你問查詢名稱和說明

    C.firstName AS firstName, 
        C.lastName AS lastName, 
        C.titleDesc AS titleDesc 

以及分化爲

whoAdded

因此他們需要通過條款添加到組,以便行可以正確地摺疊到組中。

SELECT * 
FROM (SELECT DISTINCT (A.whoAdded) AS whoAdded, 
         count(A.tag) AS totalTags, 
         count(DISTINCT A.data) AS totalSubmissions, 
         count(DISTINCT B.data) AS totalSearches, 
         C.firstName AS firstName, 
         C.lastName AS lastName, 
         C.titleDesc AS titleDesc 
    FROM  Tags_Accounts AS A 
      INNER JOIN 
      Tags_Log AS B 
      ON A.whoAdded = B.ntid 
      INNER JOIN 
      empTable AS C 
      ON A.whoAdded = C.ntid 
    WHERE whoAdded = @ntid 
    GROUP BY 
      whoAdded, 
      C.firstName , 
      C.lastName , 
      C.titleDesc 
) AS a 
FOR XML PATH ('profile'), TYPE, ELEMENTS, ROOT ('root'); 
0

當你通過,你的選擇應該只包含分組列或agregated功能,所以你需要組

C.firstName AS firstName, 
C.lastName AS lastName, 
C.titleDesc AS titleDesc 

使用這種用組:

SELECT * 
    FROM (SELECT DISTINCT (A.whoAdded) AS whoAdded, 
          count(A.tag) AS totalTags, 
          count(DISTINCT A.data) AS totalSubmissions, 
          count(DISTINCT B.data) AS totalSearches, 
          C.firstName AS firstName, 
          C.lastName AS lastName, 
          C.titleDesc AS titleDesc 
     FROM  Tags_Accounts AS A 
       INNER JOIN 
       Tags_Log AS B 
       ON A.whoAdded = B.ntid 
       INNER JOIN 
       empTable AS C 
       ON A.whoAdded = C.ntid 
     WHERE whoAdded = @ntid 
     GROUP BY whoAdded,C.firstName,C.lastName,C.titleDesc) AS a 
    FOR XML PATH ('profile'), TYPE, ELEMENTS, ROOT ('root'); 
+0

你能解釋一下這個短語的含義:「當你使用group by時,你的select應該只包含分組列或聚合函數,所以你需要組合」 - 我理解你對它做出的改變,但仍然是一個有點不確定它是如何工作的 – SBB

+1

如果一個列不是聚合函數(Sum,count等)的一部分,並且你想看到它,那麼你需要將它包含在group by中。 – HLGEM

1

您的「GROUP BY」語句必須包含所有不受聚合函數影響的列(如SUM,COUNT,M AX等)。以下列將需要添加到您的GROUP BY C.firstName,C.lastName,C.titleDesc。另外,您應該完全刪除DISTINCT(A.whoAdded),因爲更正GROUP語句會產生所需的效果。

解決方案,修復您的GROUP BY!

... 
GROUP BY A.whoAdded, C.firstName, C.lastName, C.titleDesc 
... 
0

如果您彙總等欄目,顯然你需要使用組,以組列一起將在其中進行聚合。 只是包含這個在您的查詢:

Group by A.wholeadded, C.firstname,c.lastname,c.titledesc 

彙總將在論壇中進行。 希望這有助於!

0

當使用GROUP BY時必須調用在選擇 像使用柱:

 select T.firstname,T.lastname,T.ID,Count(*) from personel 
     Where Count(*) >1 
     Group by T.firstname,T.lastname,T.ID