How to Use count and Group By with Self join in the same table in sql server 2008?

半腔热情 提交于 2019-12-25 18:20:16

问题


I have a single table with columns of st_name and id. I need to get the count of st_name and Group by st_name. How do I do this?


回答1:


select st_name, 
       count(*) as grp_cnt,
       (select count(distinct st_name) from your_table) as st_cnt
from your_table
group by st_name


来源:https://stackoverflow.com/questions/19633094/how-to-use-count-and-group-by-with-self-join-in-the-same-table-in-sql-server-200

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!