开发者

Count number of rows with distinct value in column in T-SQL

开发者 https://www.devze.com 2023-04-03 13:12 出处:网络
In T-SQL, how can I query this table to show me record counts based on how many times a distinct value appears in a column?

In T-SQL, how can I query this table to show me record counts based on how many times a distinct value appears in a column?

For example, I have a table defined as:

ControlSystemHierarchy
----------------------
ParentDeviceID int
ChildDeviceID int
Instrument bit

I want to display the number of records that match each distinct ParentDeviceID in the table so that this table

ParentDeviceID  | ChildDeviceID  | Instrument
1               | 1              | 0
1               | 2              | 0
1               | 2              | 1
2               | 3              | 0

would retur开发者_StackOverflown

ParentDeviceID | Count
1              | 3
2              | 1


select ParentDeviceID, count(*) as [Count]
from ControlSystemHierarchy
group by ParentDeviceID
0

精彩评论

暂无评论...
验证码 换一张
取 消