SQL group by max count

时间:2014-01-03 15:56:53

标签: sql sql-server

我一直在看堆栈上的几篇文章,但并不完全具体到我需要的东西

我有一个包含应用程序名称,团队,服务,董事会和用户名的表

我想根据用户(即用户数量)将应用程序名称,团队,服务,董事会退回到最高使用位置(团队,服务,董事会)

SELECT [ApplicationName]
      ,[Team]
      ,[Service]
      ,[Directorate]
      ,count(distinct username) Usercount
FROM 
       [Windows7data].[dbo].[devices_users_apps_detail] a
GROUP BY
       [ApplicationName]
      ,[Team]
      ,[Service]
      ,[Directorate]
ORDER BY
       [ApplicationName], 
       count(distinct username) desc;

我通过添加到上面嵌套的子查询,使用语句等来玩,但这没有用

Using sub-queries in SQL to find max(count())

5 个答案:

答案 0 :(得分:3)

您可以使用分析功能RANK按照应用程序名称按照号码用户的顺序放置您的团队/服务/首长级组合,然后只为每个用户选择一个。关键是ApplicationName出现在group by子句中,但不出现在Rank函数的Partition by子句中。

SELECT  [ApplicationName]
        ,[Team]
        ,[Service]
        ,[Directorate]
        ,UserCount
FROM    (   SELECT  [ApplicationName]
                    ,[Team]
                    ,[Service]
                    ,[Directorate]
                    ,COUNT(DISTINCT username) Usercount,
                    [Rank] = RANK() OVER(PARTITION BY [Team], [Service], [Directorate]   
                                        ORDER BY COUNT(DISTINCT UserName) DESC)
            FROM    [Windows7data].[dbo].[devices_users_apps_detail] a
            GROUP BY [ApplicationName], [Team], [Service], [Directorate]
        ) t
WHERE   t.[Rank] = 1
ORDER BY [ApplicationName], UserCount DESC;

<强> Example on SQL Fiddle


我实际上无法确定你想从问题中找到这个问题,所以我将发布两个:

SELECT  [ApplicationName]
        ,[Team]
        ,[Service]
        ,[Directorate]
        ,UserCount
FROM    (   SELECT  [ApplicationName]
                    ,[Team]
                    ,[Service]
                    ,[Directorate]
                    ,COUNT(DISTINCT username) Usercount,
                    [Rank] = RANK() OVER(PARTITION BY [ApplicationName] ORDER BY COUNT(DISTINCT UserName) DESC)
            FROM    [Windows7data].[dbo].[devices_users_apps_detail] a
            GROUP BY [ApplicationName], [Team], [Service], [Directorate]
        ) t
WHERE   t.[Rank] = 1
ORDER BY [ApplicationName], UserCount DESC;

<强> Example on SQL Fiddle

答案 1 :(得分:0)

SELECT TOP(1)
       [ApplicationName]
      ,[Team]
      ,[Service]
      ,[Directorate]
      ,count(distinct username) Usercount
  FROM [Windows7data].[dbo].[devices_users_apps_detail] a
  group by  
       [ApplicationName]
      ,[Team]
      ,[Service]
      ,[Directorate]
  order by 
       Usercount desc;

答案 2 :(得分:0)

;With HighestValues
AS
(
  SELECT *, rn = ROW_NUMBER() OVER (PARTITION BY [Team], [Service], [Directorate] 
                                    ORDER BY Usercount DESC)
 FROM
  (
    SELECT [ApplicationName]
          ,[Team]
          ,[Service]
          ,[Directorate]
          ,count(distinct username) Usercount
    FROM T a
    group by  [ApplicationName],[Team],[Service],[Directorate]
  )Q
)
SELECT APPLICATIONNAME,TEAM,SERVICE,DIRECTORATE,USERCOUNT 
FROM HighestValues
WHERE rn = 1

SQL Fiddle

答案 3 :(得分:0)

对我来说还不错。这会产生错误吗?如果您只想返回最大行,那么您可以尝试在SELECT之后添加TOP 1.

答案 4 :(得分:0)

    select *
    from
    (
    SELECT [ApplicationName]
          ,[Team]
          ,[Service]
          ,[Directorate]
          ,count(username) Usercount
    FROM [Windows7data].[dbo].[devices_users_apps_detail] a
    group by  [ApplicationName],[Team],[Service],[Directorate]
    ) b
    inner join (
    select ApplicationName, Max(UserCount) MaxUserCount
    from
    (
    SELECT [ApplicationName]
          ,[Team]
          ,[Service]
          ,[Directorate]
          ,count(distinct username) Usercount
    FROM [Windows7data].[dbo].[devices_users_apps_detail] a
    group by  [ApplicationName],[Team],[Service],[Directorate]
    ) x
)Q on b.ApplicationName = Q.ApplicationName and b.UserCount = q.MaxUserCount

内部查询x获取应用程序等的列表及其用户数。查询Q然后获取appname和与之关联的max usercount。这连接到查询b(与x相同),它使用每个应用程序的maxusercount选择b上的行。