读书人

问一个很长的查询串?该如何解决

发布时间: 2012-02-03 22:02:47 作者: rapoo

问一个很长的查询串?
有个表有100个记录

写一个查询

得到记录数从20-60数据段中 age> 20 sex=1 的实际记录数和age字段,sex字段

不知道说明白没有.
步骤是这样的
1.取得 100记录中的 20-60这段记录
2.在这40条记录中取出age> 20 sex=1的记录,一般少与40条
3.得到实际的符合条件的记录总数字段,age字段,sex字段...组成结果集

怎么写成一条语句

前2个实现方法是
selsect top xxx,xxx from ...where (id NOT IN(.........) and age> 20 and sex=1 .....
但是再添加一个条件,获得记录数,该怎么写/

[解决办法]
select count(*) from
(
selsect top xxx,xxx from ...where (id NOT IN(.........) and age> 20 and sex=1 .....
) t
[解决办法]
???

1.取得 100记录中的 20-60这段记录

select * from TAB where age> =20 and age <=60

2.在这40条记录中取出age> 20 sex=1的记录,一般少与40条

select * from TAB where age> 20 and age <=60 and sex=1

3.得到实际的符合条件的记录总数字段,age字段,sex字段...组成结果集

???
[解决办法]
select * from TAB where age> =20 and age <=60
理解错了她意思是取出100条数据中的第20-60的数据
top xxx,xxx 这样不可以的吧
[解决办法]
select * from (select top 40 * from t where id not in(select top 20 id from t) and sex=1 and age> 20) b,

(
select count(1) num,a.sex


from
(select top 40 * from t where id not in(select top 20 id from t) and sex=1 and age> 20
) a
group by a.sex

) c
where c.sex=b.sex

但是总数会出现很多行,只是说统计出来了而已!

读书人网 >SQL Server

热点推荐