当我尝试加入时,我得到了很多错误(可能是因为我从来没有对它们太过分了)。
我的存储过程非常简单:
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 :(得分:1)
您要求查询按名称和说明进行区分
C.firstName AS firstName,
C.lastName AS lastName,
C.titleDesc AS titleDesc
以及
whoAdded
因此需要将它们添加到group by子句中,以便行可以正确地折叠到组中。
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');
答案 1 :(得分:1)
您的“GROUP BY”语句必须包含所有不受聚合函数影响的列(如SUM,COUNT,MAX等)。以下列将需要添加到GROUP BY C.firstName,C.lastName,C.titleDesc。此外,您应该完全删除DISTINCT(A.whoAdded),因为更正GROUP语句将具有此预期效果。
解决方案,修复您的GROUP BY!
...
GROUP BY A.whoAdded, C.firstName, C.lastName, C.titleDesc
...
答案 2 :(得分:0)
当您使用分组依据时,您的选择应仅包含分组列或聚合函数,因此您需要分组
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');
答案 3 :(得分:0)
如果要聚合其他列,显然您需要使用group by,将列组合在一起以执行哪些聚合。 只需在查询中包含此内容:
Group by A.wholeadded, C.firstname,c.lastname,c.titledesc
将对群组执行聚合。 希望这有帮助!
答案 4 :(得分:0)
使用分组时必须调用select中使用的列 喜欢:
select T.firstname,T.lastname,T.ID,Count(*) from personel
Where Count(*) >1
Group by T.firstname,T.lastname,T.ID