使为人君者,深居高处:分组的sql问题???

来源:百度文库 编辑:杭州交通信息网 时间:2024/05/06 10:32:11
sql问题表tb,字段:日期,关键字,钱等。我要分日分关键字统计,为:select date,key,sum(money) from tb where .....group by date,key 这是ok的,但是我想问1:要统计这个记录个数的sql(count(*))怎么写?2: 由于分页时要从第三行查,用到select top 2.....where ....and (如果不是分组的,这里可以是id not in select top 2。。。。),但是这种剔出两列不属于某记录集,怎么做啊?谢谢

select count(*) from tb where .....group by date
//count()计数里只能为单关键名或者"*".

select top 2 date,key,sum(money) from tb where id not in(select top 2 id from tb where...group by date) and ... group by date
//不知道你这里的id是否有重复,如果记录的id有重复的话可能会影响搜索效果。如果你的date是精确到分秒的话,应该不会重复,可以换date来作为分组的区别的关键名。

select top 2 date,key,sum(money) from tb where id not in(select top 2 id from tb where...group by date) and ... group by date

就可以了