1

I am a beginner in SQL and been trying to obtain all of the maximum values as per the access strength and folder names, with allowing groups to the folders being displayed accordingly. I tried using MAX function but it was returning only 1 maximum result. Thank you in advance for any help or guidance of how t achieve the specified results.

Please see the jpg link attached for the top table and the desired outcome in the bottom table.

John

top table and the desired outcome in the bottom table

4

3 回答 3

1

您可以使用RANK功能将您的记录按顺序排列,然后只选择前 1 个:

WITH CTE AS
(   SELECT  Folder_name,
            [User],
            Access,
            Group_Allowing,
            [Rank] = RANK() OVER(PARTITION BY Folder_Name, [User] ORDER BY Access DESC)
    FROM    T
)
SELECT  Folder_name,
        [User],
        Access,
        Group_Allowing
FROM    CTE
WHERE   [Rank] = 1;

PARTITION BY子句类似于 GROUP BY,每个组的排名将从 0 重新开始。然后,顺序简单地指示了按什么顺序进行排名。

于 2013-09-26T19:55:42.820 回答
0

您可以使用“ORDER BY Access DESC”和“LIMIT [您想要的条目数]”,或者如果您只想获得最高数字,您可以使用“GROUP BY Access”,然后使用 max() 显示您的所有条目想

于 2013-09-26T19:46:57.627 回答
0

您需要将您的查询MAX()放入子查询JOIN中:

SELECT a.*
FROM YourTable a
JOIN (SELECT Folder_Name, [User], MAX(Access) Max_Access
      FROM YourTable
      GROUP BY Folder_Name, [User]
     )b
 ON a.Folder_Name = b.Folder_Name
  AND a.[User] = b.[User]
  AND a.Access = b.Max_Access
于 2013-09-26T19:44:03.330 回答