我有这个 SQL 查询:
SELECT
sg.rolesID,
count# = ROW_NUMBER() OVER (ORDER BY sg.rolesContainerID),
sg.rolesContainerID,
eventTimeStamp AS timeStarted,
eventTimeStamp AS timeFinished
FROM
view_SecurityGroup sg
WHERE
sg.rolesID = '22400ab8-62a2-4a69-b002-5dd4d0459728'
ORDER BY
sg.rolesContainerID, timeStarted, timeFinished DESC
结果如下:
rolesID count# rolesContainerID timeStarted timeFinished
----------------------------------------------------------------------------------------------------------------------------------------------------
22400AB8-62A2-4A69-B002-5DD4D0459728 1 153F36C8-6FB5-4B50-A2D7-000209C96663 2023-02-27 14:26:50.583 2023-02-27 14:26:50.583
22400AB8-62A2-4A69-B002-5DD4D0459728 2 153F36C8-6FB5-4B50-A2D7-000209C96663 2023-02-27 14:26:50.587 2023-02-27 14:26:50.587
22400AB8-62A2-4A69-B002-5DD4D0459728 3 153F36C8-6FB5-4B50-A2D7-000209C96663 2023-02-27 14:26:58.420 2023-02-27 14:26:58.420
22400AB8-62A2-4A69-B002-5DD4D0459728 4 790E699D-1BDE-4CCA-B6A6-013696B81774 2023-02-28 15:41:02.540 2023-02-28 15:41:02.540
22400AB8-62A2-4A69-B002-5DD4D0459728 5 790E699D-1BDE-4CCA-B6A6-013696B81774 2023-02-28 15:42:51.540 2023-02-28 15:42:51.540
22400AB8-62A2-4A69-B002-5DD4D0459728 6 790E699D-1BDE-4CCA-B6A6-013696B81774 2023-02-28 15:44:27.560 2023-02-28 15:44:27.560
22400AB8-62A2-4A69-B002-5DD4D0459728 7 223C4704-2B04-404D-991A-04B408C6CA93 2022-11-04 13:59:03.343 2022-11-04 13:59:03.343
22400AB8-62A2-4A69-B002-5DD4D0459728 8 223C4704-2B04-404D-991A-04B408C6CA93 2022-11-04 13:59:03.347 2022-11-04 13:59:03.347
22400AB8-62A2-4A69-B002-5DD4D0459728 9 223C4704-2B04-404D-991A-04B408C6CA93 2022-11-04 14:05:49.133 2022-11-04 14:05:49.133
22400AB8-62A2-4A69-B002-5DD4D0459728 10 229105A8-2713-44FE-B0D0-0FF09F54E905 2023-02-27 00:28:58.840 2023-02-27 00:28:58.840
22400AB8-62A2-4A69-B002-5DD4D0459728 11 229105A8-2713-44FE-B0D0-0FF09F54E905 2023-02-27 00:29:48.513 2023-02-27 00:29:48.513
但我希望
count#
仅针对每个角色ContainerID 递增......如下所示:
rolesID count# rolesContainerID timeStarted timeFinished
----------------------------------------------------------------------------------------------------------------------------------------------------
22400AB8-62A2-4A69-B002-5DD4D0459728 1 153F36C8-6FB5-4B50-A2D7-000209C96663 2023-02-27 14:26:50.583 2023-02-27 14:26:50.583
22400AB8-62A2-4A69-B002-5DD4D0459728 1 153F36C8-6FB5-4B50-A2D7-000209C96663 2023-02-27 14:26:50.587 2023-02-27 14:26:50.587
22400AB8-62A2-4A69-B002-5DD4D0459728 1 153F36C8-6FB5-4B50-A2D7-000209C96663 2023-02-27 14:26:58.420 2023-02-27 14:26:58.420
22400AB8-62A2-4A69-B002-5DD4D0459728 2 790E699D-1BDE-4CCA-B6A6-013696B81774 2023-02-28 15:41:02.540 2023-02-28 15:41:02.540
22400AB8-62A2-4A69-B002-5DD4D0459728 2 790E699D-1BDE-4CCA-B6A6-013696B81774 2023-02-28 15:42:51.540 2023-02-28 15:42:51.540
22400AB8-62A2-4A69-B002-5DD4D0459728 2 790E699D-1BDE-4CCA-B6A6-013696B81774 2023-02-28 15:44:27.560 2023-02-28 15:44:27.560
22400AB8-62A2-4A69-B002-5DD4D0459728 3 223C4704-2B04-404D-991A-04B408C6CA93 2022-11-04 13:59:03.343 2022-11-04 13:59:03.343
22400AB8-62A2-4A69-B002-5DD4D0459728 3 223C4704-2B04-404D-991A-04B408C6CA93 2022-11-04 13:59:03.347 2022-11-04 13:59:03.347
22400AB8-62A2-4A69-B002-5DD4D0459728 3 223C4704-2B04-404D-991A-04B408C6CA93 2022-11-04 14:05:49.133 2022-11-04 14:05:49.133
22400AB8-62A2-4A69-B002-5DD4D0459728 4 229105A8-2713-44FE-B0D0-0FF09F54E905 2023-02-27 00:28:58.840 2023-02-27 00:28:58.840
22400AB8-62A2-4A69-B002-5DD4D0459728 4 229105A8-2713-44FE-B0D0-0FF09F54E905 2023-02-27 00:29:48.513 2023-02-27 00:29:48.513
有没有办法在 SQL Server 中做到这一点?
谢谢!
count# = DENSE_RANK() OVER (ORDER BY sg.rolesContainerID),
或
DENSE_RANK() OVER (ORDER BY sg.rolesContainerID) AS count#,
霍根的答案是正确的,那就完美了。
我只是认为您可能会感兴趣看到从此文档页面中提取的此查询。
SELECT p.FirstName, p.LastName
,ROW_NUMBER() OVER (ORDER BY a.PostalCode) AS "Row Number"
,RANK() OVER (ORDER BY a.PostalCode) AS Rank
,DENSE_RANK() OVER (ORDER BY a.PostalCode) AS "Dense Rank"
,NTILE(4) OVER (ORDER BY a.PostalCode) AS Quartile
,s.SalesYTD
,a.PostalCode
FROM Sales.SalesPerson AS s
INNER JOIN Person.Person AS p
ON s.BusinessEntityID = p.BusinessEntityID
INNER JOIN Person.Address AS a
ON a.AddressID = p.BusinessEntityID
WHERE TerritoryID IS NOT NULL AND SalesYTD <> 0;
名字 | 姓氏 | 行号 | 排名 | 密集排名 | 四分位 | 年初至今销售额 | 邮政编码 |
---|---|---|---|---|---|---|---|
迈克尔 | 布莱斯 | 1 | 1 | 1 | 1 | 3763178.1787 | 98027 |
琳达 | 米切尔 | 2 | 1 | 1 | 1 | 4251368.5497 | 98027 |
吉莉安 | 卡森 | 3 | 1 | 1 | 1 | 3189418.3662 | 98027 |
加勒特 | 巴尔加斯 | 4 | 1 | 1 | 1 | 1453719.4653 | 98027 |
茨维 | 瑞特 | 5 | 1 | 1 | 2 | 2315185.6110 | 98027 |
帕梅拉 | 安斯曼-沃尔夫 | 6 | 1 | 1 | 2 | 1352577.1325 | 98027 |
舒 | 伊藤 | 7 | 7 | 2 | 2 | 2458535.6169 | 98055 |
何塞 | 萨莱瓦 | 8 | 7 | 2 | 2 | 2604540.7172 | 98055 |
大卫 | 坎贝尔 | 9 | 7 | 2 | 3 | 1573012.9383 | 98055 |
太特 | 门萨-安南 | 10 | 7 | 2 | 3 | 1576562.1966 | 98055 |
林恩 | 索菲利亚斯 | 11 | 7 | 2 | 3 | 1421810.9242 | 98055 |
雷切尔 | 瓦尔迪兹 | 12 | 7 | 2 | 4 | 1827066.7118 | 98055 |
杰 | 朴 | 13 | 7 | 2 | 4 | 4116871.2277 | 98055 |
兰吉特 | 瓦尔基楚杜卡提尔 | 14 | 7 | 2 | 4 | 3121616.3202 | 98055 |
在示例中,它们展示了不同排名窗口函数如何在同一查询中使用所有这些函数来工作,因此您可以清楚地看到并理解它们之间的差异。