SQL DENSE_RANK 和分区依据
SQL DENSE_RANK and PARTITION BY
我不完全理解下面列出的查询结果:
CREATE TABLE #tmpAccountsTable (ID INT PRIMARY KEY, AccountID INT,
AccountName varchar(20), CompanyID int, CompanyName varchar(50))
INSERT INTO #tmpAccountsTable (ID, AccountID, AccountName, CompanyID, CompanyName)
SELECT 1 AS ID, 1 AS AccountID, 'ABC Account' AS AccountName, 1 AS CompanyID, 'Stark Enterprise' AS CompanyName UNION
SELECT 2 AS ID, 1 AS AccountID, 'ABC Account' AS AccountName, 1 AS CompanyID, 'Stark Enterprise' AS CompanyName UNION
SELECT 3 AS ID, 2 AS AccountID, 'DEF Account' AS AccountName, 1 AS CompanyID, 'Stark Enterprise' AS CompanyName UNION
SELECT 4 AS ID, 3 AS AccountID, 'GHI Account' AS AccountName, 1 AS CompanyID, 'Stark Enterprise' AS CompanyName UNION
SELECT 5 AS ID, 4 AS AccountID, 'JKL Account' AS AccountName, 1 AS CompanyID, 'Stark Enterprise' AS CompanyName UNION
SELECT 6 AS ID, 1 AS AccountID, 'ABC Account' AS AccountName, 1 AS CompanyID, 'Stark Enterprise' AS CompanyName UNION
SELECT 7 AS ID, 1 AS AccountID, 'ABC Account' AS AccountName, 1 AS CompanyID, 'Stark Enterprise' AS CompanyName UNION
SELECT 8 AS ID, 1 AS AccountID, 'ABC Account' AS AccountName, 1 AS CompanyID, 'Stark Enterprise' AS CompanyName UNION
SELECT 9 AS ID, 2 AS AccountID, 'DEF Account' AS AccountName, 1 AS CompanyID, 'Stark Enterprise' AS CompanyName
SELECT DENSE_RANK() OVER (PARTITION BY AccountID ORDER BY CompanyName) as AccountRANK, * FROM #tmpAccountsTable
DROP TABLE #tmpAccountsTable
执行上面的操作,得到如下结果集:
根据我对 SELECT DENSE_RANK() OVER (PARTITION BY AccountID ORDER BY CompanyName) as AccountRANK, * FROM #tmpAccountsTable
的理解,应该为所有相同的 AccountId 创建一个分区,并且它们应该收到相同的 DENSE_RANK()
值,但是可以看出,不是这种情况。
我错过了什么?
如果您希望每个 AccountId
有一个单独的值,那么它应该是 ORDER BY
的一部分,而不是 PARTITION BY
:
SELECT DENSE_RANK() OVER (ORDER BY AccountID, CompanyName) as AccountRANK, *
FROM #tmpAccountsTable;
PARTITION BY
用于重置计数器,所以每个AccountId
从1重新开始。然后CompanyNames
被排序。因为它们都有相同的值,所以结果是 1
.
我不完全理解下面列出的查询结果:
CREATE TABLE #tmpAccountsTable (ID INT PRIMARY KEY, AccountID INT,
AccountName varchar(20), CompanyID int, CompanyName varchar(50))
INSERT INTO #tmpAccountsTable (ID, AccountID, AccountName, CompanyID, CompanyName)
SELECT 1 AS ID, 1 AS AccountID, 'ABC Account' AS AccountName, 1 AS CompanyID, 'Stark Enterprise' AS CompanyName UNION
SELECT 2 AS ID, 1 AS AccountID, 'ABC Account' AS AccountName, 1 AS CompanyID, 'Stark Enterprise' AS CompanyName UNION
SELECT 3 AS ID, 2 AS AccountID, 'DEF Account' AS AccountName, 1 AS CompanyID, 'Stark Enterprise' AS CompanyName UNION
SELECT 4 AS ID, 3 AS AccountID, 'GHI Account' AS AccountName, 1 AS CompanyID, 'Stark Enterprise' AS CompanyName UNION
SELECT 5 AS ID, 4 AS AccountID, 'JKL Account' AS AccountName, 1 AS CompanyID, 'Stark Enterprise' AS CompanyName UNION
SELECT 6 AS ID, 1 AS AccountID, 'ABC Account' AS AccountName, 1 AS CompanyID, 'Stark Enterprise' AS CompanyName UNION
SELECT 7 AS ID, 1 AS AccountID, 'ABC Account' AS AccountName, 1 AS CompanyID, 'Stark Enterprise' AS CompanyName UNION
SELECT 8 AS ID, 1 AS AccountID, 'ABC Account' AS AccountName, 1 AS CompanyID, 'Stark Enterprise' AS CompanyName UNION
SELECT 9 AS ID, 2 AS AccountID, 'DEF Account' AS AccountName, 1 AS CompanyID, 'Stark Enterprise' AS CompanyName
SELECT DENSE_RANK() OVER (PARTITION BY AccountID ORDER BY CompanyName) as AccountRANK, * FROM #tmpAccountsTable
DROP TABLE #tmpAccountsTable
执行上面的操作,得到如下结果集:
根据我对 SELECT DENSE_RANK() OVER (PARTITION BY AccountID ORDER BY CompanyName) as AccountRANK, * FROM #tmpAccountsTable
的理解,应该为所有相同的 AccountId 创建一个分区,并且它们应该收到相同的 DENSE_RANK()
值,但是可以看出,不是这种情况。
我错过了什么?
如果您希望每个 AccountId
有一个单独的值,那么它应该是 ORDER BY
的一部分,而不是 PARTITION BY
:
SELECT DENSE_RANK() OVER (ORDER BY AccountID, CompanyName) as AccountRANK, *
FROM #tmpAccountsTable;
PARTITION BY
用于重置计数器,所以每个AccountId
从1重新开始。然后CompanyNames
被排序。因为它们都有相同的值,所以结果是 1
.