如何在SQL Server 2008的同一张表中使用带有self的count和Group By? [英] How to Use count and Group By with Self join in the same table in sql server 2008?

查看:106
本文介绍了如何在SQL Server 2008的同一张表中使用带有self的count和Group By?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个表,其中的列为 st_name id 。我需要获取 st_name 的数量,并按 st_name 分组。我该怎么做?

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?

推荐答案

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

这篇关于如何在SQL Server 2008的同一张表中使用带有self的count和Group By?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆