[英]Microsoft SQL Query Get count of DISTINCT values using Over Partition
I am trying to get a Count for all the Distinct Case Sensitive values using Over Partition. 我正在尝试使用Over Partition为所有不同的区分大小写值计数。
I have the following values: 我有以下值:
FirstCol SecCol
A E
A E
A a
A e
I want my results to be: 我希望我的结果是:
SecCol FirstCol SecColCount
a A 1
e A 1
E A 2
I am trying to achieve this using the OVER PARTITION method below, but I am getting the following results with my Query: 我正在尝试使用下面的OVER PARTITION方法实现这一目标,但是我的查询获得了以下结果:
SecCol FirstCol SecColCount
a A 1
e A 3
Here is the Query I am trying to use: 这是我要使用的查询:
SELECT Distinct SecCol, FirstCol, 'SecColCount' = count(SecCol) OVER (PARTITION BY SecCol)
FROM #LocalTempTable;
Your default collation for tempdb/db is case insensitive. tempdb / db的默认排序规则不区分大小写。 Use case sensitive instead:
改用区分大小写:
SELECT DISTINCT
SecCol COLLATE sql_latin1_general_cp1_cs_as,
FirstCol,
[SecColCount] = COUNT(SecCol) OVER (PARTITION BY SecCol
COLLATE sql_latin1_general_cp1_cs_as)
FROM #LocalTempTable;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.