2

我有以下查询:

SELECT  
   h.helpcontent, s.MenuID, s.pkSitePageId, s.PageAliasName,
   s.SitePageName,s.pagepath
FROM 
   tblSiteRolePermissions p
INNER JOIN 
   tblSitePages s ON p.fkSitePageId = s.pkSitePageId
INNER JOIN 
   tblHelp h ON s.pkSitePageId=h.fkSitePageId
WHERE 
   (p.fkRoleId = 4 AND p.ViewOnly = 1) 
ORDER BY 
   s.pkSitePageId

我想从tblSiteRolePermissions. 我已经尝试过,Count但它抛出了一些错误,例如:

消息 8120,级别 16,状态 1,第 1
行列 'tblHelp.HelpContent' 在选择列表中无效,因为它既不包含在聚合函数或 GROUP BY 子句中。

请帮助我,让我知道我错在哪里。

更新

这是我正在尝试的代码:

SELECT 
    count(p.pkRolePermissionId), 
    h.helpcontent, s.MenuID, s.pkSitePageId, s.PageAliasName, 
    s.SitePageName, s.pagepath
FROM
    tblSiteRolePermissions p
INNER JOIN 
    tblSitePages s ON p.fkSitePageId = s.pkSitePageId
INNER JOIN
    tblHelp h ON s.pkSitePageId = h.fkSitePageId
WHERE 
    (p.fkRoleId = 4 AND p.ViewOnly = 1) 
ORDER BY 
    s.pkSitePageId
4

2 回答 2

5

如果您使用聚合函数,则需要对语句中未在聚合中GROUP BY的剩余字段使用子句。SELECT与此类似:

SELECT  count(p.pkRolePermissionId) CountRolePermission,
  h.helpcontent, 
  s.MenuID, 
  s.pkSitePageId, 
  s.PageAliasName,
  s.SitePageName,
  s.pagepath
from tblSiteRolePermissions p
INNER JOIN tblSitePages s 
  ON p.fkSitePageId = s.pkSitePageId
inner join tblHelp h 
  on s.pkSitePageId=h.fkSitePageId
WHERE (p.fkRoleId =4 and p.ViewOnly=1) 
GROUP BY h.helpcontent, 
  s.MenuID, 
  s.pkSitePageId, 
  s.PageAliasName,
  s.SitePageName,
  s.pagepath
ORDER BY s.pkSitePageId

如果你想要所有行的计数,那么你可以使用这样的东西:

SELECT  NumRows.TotalCount,
  h.helpcontent, 
  s.MenuID, 
  s.pkSitePageId, 
  s.PageAliasName,
  s.SitePageName,
  s.pagepath
from tblSiteRolePermissions p
INNER JOIN tblSitePages s 
  ON p.fkSitePageId = s.pkSitePageId
inner join tblHelp h 
  on s.pkSitePageId=h.fkSitePageId
cross join
(
  SELECT  count(*) TotalCount
  from tblSiteRolePermissions p
  INNER JOIN tblSitePages s 
    ON p.fkSitePageId = s.pkSitePageId
  inner join tblHelp h 
    on s.pkSitePageId=h.fkSitePageId
  WHERE (p.fkRoleId =4 and p.ViewOnly=1) 
) NumRows
WHERE (p.fkRoleId =4 and p.ViewOnly=1) 
ORDER BY s.pkSitePageId

或者您可以在查询中使用 CTE,类似于:

;with cte as 
(
  SELECT h.helpcontent, 
    s.MenuID, 
    s.pkSitePageId, 
    s.PageAliasName,
    s.SitePageName,
    s.pagepath
  from tblSiteRolePermissions p
  INNER JOIN tblSitePages s 
    ON p.fkSitePageId = s.pkSitePageId
  inner join tblHelp h 
    on s.pkSitePageId=h.fkSitePageId
  WHERE (p.fkRoleId =4 and p.ViewOnly=1) 
)
select h.helpcontent, 
    s.MenuID, 
    s.pkSitePageId, 
    s.PageAliasName,
    s.SitePageName,
    s.pagepath, 
    (select count(*) from cte) as Total
from cte
于 2012-11-15T10:25:37.170 回答
0
SELECT count(p.pkRolePermissionId),h.helpcontent, s.MenuID, s.pkSitePageId, s.PageAliasName,
s.SitePageName,s.pagepath
from tblSiteRolePermissions p
INNER JOIN tblSitePages s ON p.fkSitePageId = s.pkSitePageId
inner join tblHelp h on s.pkSitePageId=h.fkSitePageId
WHERE (p.fkRoleId =4 and p.ViewOnly=1) 
GROUP BY h.helpcontent, s.MenuID, s.pkSitePageId, s.PageAliasName,
s.SitePageName,s.pagepath
ORDER BY s.pkSitePageId
于 2012-11-15T10:25:59.523 回答