日期:2014-05-19  浏览次数:20361 次

问个SQL问题
怎么统计一个分组查询后的记录总数?

比如:
select   a   from   t   group   by   a
我要知道结果返回多少条记录


我这样写:
select   count(a)   from   t   group   by   a   为啥不行?

请指点,不甚感激!


------解决方案--------------------
select a,count(a) from t group by a 为啥不行?
你前面没有a元素后面怎么group by
------解决方案--------------------
ls的正解
------解决方案--------------------
look
-----------------------------

declare @t table
(a int)

select a from @t group by a
select @@rowcount
------解决方案--------------------
or
----------------------

select count(a) from (select a from @t group by a) b
------解决方案--------------------
support Red_angelX
------解决方案--------------------
select a, count(a) from t group by a
------解决方案--------------------
select a, count(*) from t group by a
------解决方案--------------------
jf
------解决方案--------------------
select count(min_lvl)
from (
select min_lvl from jobs
group by min_lvl) tb
------解决方案--------------------
select count(a) from (select a from t group by a)
------解决方案--------------------
select count(1) from t group by a
------解决方案--------------------
前面没有a元素
------解决方案--------------------
比如 select a, b from t group by a 的结果如下:
a b
-------
a1 1
a2 3
a3 2

这时我要求返回记录总数,也就是3

---------

可以直接這麼寫

Select Count(Distinct a) From t