Я не полностью понимаю результаты запроса, перечисленные ниже:
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()
, но, как видно, это не так.
Что мне не хватает?