读书人

求教一个SQL语句~该如何解决

发布时间: 2012-01-18 00:23:26 作者: rapoo

求教一个SQL语句~
情况是这样的,有个表:
table1
type int
point int

现在想在一句SQL里求得如下几个值:
type =0 时的纪录总数
type =0 时的point数之和(sum)
type =0 AND point= 10 时的纪录数
type =0 AND point= 20 时的纪录数

请问有可能吗?

[解决办法]
select count(type),sum(point),纪录数1=sum(case when point= 10 then 1 else 0 end),纪录数2=sum(case when point= 20 then 1 else 0 end)
from table1
where type=0
[解决办法]
select
count(*),
sum(point),
sum(case point when 10 then 1 else 0 end),
sum(case point when 20 then 1 else 0 end)
from

where
type=0
[解决办法]
select count(1),sum(point),sum(case when point=10 then 1 else 0 end),sum(case when point=20 then 1 else 0 end) from table1 where type=0
[解决办法]
declare @table1 table(type int,point int)

insert @table1 select 0 ,20
union all select 0 ,20
union all select 0 ,10
union all select 0 ,10

select count(1),sum(point),(select count(1) from @table1 where type =0 AND point= 10)
,(select count(1) from @table1 where type =0 AND point= 20)
from @table1
where type =0

读书人网 >SQL Server

热点推荐