2011年3月16日 星期三

select count+sum 範例

參考
---
declare @table table
(
id int,
value int
)

insert into @table
select 1, 10 union all
select 1, 11 union all
select 2, 20 union all
select 2, 21 union all
select 3, 30 union all
select 4, 40

select id, count(*)
from @table
group by id
-- result :
-- 1, 2
-- 2, 2
-- 3, 1
-- 4, 1

select sum(cnt)
from
(
select id, count(*) as cnt
from @table
group by id
) as c
-- result = 6

select count(*)
from @table
-- result = 6

沒有留言:

張貼留言