[英]Count same values in column SQL Server
I am trying to count Same values in a column and want it to return a count there off. 我试图计算一列中的Same值,并希望它返回一个计数。
| ITEM | COUNT |
+-------+-------+
| GREEN | 1 |
| GREEN | 2 |
| GREEN | 3 |
| RED | 1 |
| RED | 2 |
I tried 我试过了
ROW_NUMBER() OVER (ORDER BY ITEM) AS Row
But that only counts each line 1 - 1000 但这仅计算每行1-1000
How would I accomplish this? 我将如何完成?
Try: 尝试:
ROW_NUMBER() OVER (PARTITION BY ITEM ORDER BY ITEM) AS Row
or 要么
ROW_NUMBER() OVER (PARTITION BY ITEM ORDER BY COUNT) AS Row
You can have count of each ITEM
like this: 您可以像这样计算每个
ITEM
数量:
COUNT(*) OVER (PARTITION BY ITEM) As CountByItem
You'll want to include a partition by
clause with the row_number function. 您将希望在row_number函数中包含一个
partition by
子句。 This makes the row_number restart from 1 for each new type of item. 对于每种新类型的项目,这使row_number从1重新开始。
ROW_NUMBER() OVER (PARTITION BY ITEM ORDER BY ITEM) AS Row
this would give you a result like: 这将为您提供如下结果:
item Row
GREEN 1
GREEN 2
GREEN 3
RED 1
RED 2
This code will count same values in table 此代码将在表中计算相同的值
SELECT item, COUNT(*)
FROM Table
Group by item
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.