22,207
社区成员
发帖
与我相关
我的任务
分享
create table a(id int,value int,dt datetime)
insert into a values(1 ,100 ,'2010/3/20')
insert into a values(2 ,10 ,'2010/3/20')
insert into a values(3 ,20 ,'2010/3/20')
insert into a values(4 ,0 ,'2010/3/20')
insert into a values(5 ,-1 ,'2010/3/20')
insert into a values(6 ,-2 ,'2010/3/20')
insert into a values(7 ,-3 ,'2010/3/20')
insert into a values(8 ,5 ,'2010/3/20')
insert into a values(9 ,6 ,'2010/3/20')
insert into a values(10,-6 ,'2010/3/20')
go
select convert(varchar(10),dt,120) , sum(case when value >= 0 then 1 else -1 end) from a where dt = '2010-03-20' group by convert(varchar(10),dt,120)
drop table a
/*
---------- -----------
2010-03-20 2
(所影响的行数为 1 行)
*/
create table a(id int,value int,dt datetime)
insert into a values(1 ,100 ,'2010/3/20')
insert into a values(2 ,10 ,'2010/3/20')
insert into a values(3 ,20 ,'2010/3/20')
insert into a values(4 ,0 ,'2010/3/20')
insert into a values(5 ,-1 ,'2010/3/20')
insert into a values(6 ,-2 ,'2010/3/20')
insert into a values(7 ,-3 ,'2010/3/20')
insert into a values(8 ,5 ,'2010/3/20')
insert into a values(9 ,6 ,'2010/3/20')
insert into a values(10,-6 ,'2010/3/20')
go
select sum(case when value >= 0 then 1 else -1 end) from a where dt = '2010-03-20'
drop table a
/*
-----------
2
(所影响的行数为 1 行)
*/
select sum(case when value >= 0 then 1 else -1 end) from a where dt = '2010-03-20'
select
dt,
sum(case when value>=0 then 1 else -1 end)
from
a
group by
dt
select dt,
sum(case when value>=0 then 1 else -1 end)
from a
group by dt