读书人

急sql存储过程,该如何解决

发布时间: 2013-10-25 14:36:53 作者: rapoo

急!急!sql存储过程
一张表里面有CYSJ(出院时间)这个字段,select语句怎样算出这张表在CYSJ之前的数据有多少条啊
[解决办法]


select count(*) from 表 where CYSJ<'时间'

[解决办法]
select count(*) from tb where cysj<=''
?
[解决办法]

create table #t1(a varchar(5), b varchar(5),CYSJ datetime)
insert into #t1 select '11', '21','2012/01/01 00:00:00'
union all select '12', '22','2012/10/10 00:00:00'
union all select '13','23','2013/01/01 00:00:00'
union all select '13','23','2010/01/01 00:00:00'
union all select '13','23','2009/01/01 00:00:00'
select * from #t1

select a.CYSJ ,ab.
from #t1 as a
cross apply (select COUNT(*) as ,max(b.CYSJ) as CYSJ from #t1 b
where b.CYSJ <=a.CYSJ ) ab

where a.CYSJ =ab.CYSJ

drop table #t1

[解决办法]

create table t1(a varchar(5), b varchar(5),CYSJ datetime)

insert into t1 select '11', '21','2012/01/01 00:00:00'
union all select '12', '22','2012/10/10 00:00:00'
union all select '13','23','2013/01/01 00:00:00'
union all select '13','23','2010/01/01 00:00:00'
union all select '13','23','2009/01/01 00:00:00'
go

select a.CYSJ ,
count(ab.CYSJ) '小于cysj的数据条数'
from t1 a
inner join t1 ab
on a.CYSJ < ab.CYSJ
group by a.CYSJ
/*
CYSJ小于cysj的数据条数
2009-01-01 00:00:00.0004
2010-01-01 00:00:00.0003
2012-01-01 00:00:00.0002
2012-10-10 00:00:00.0001
*/

drop table t1

读书人网 >SQL Server

热点推荐