我现在在一个表中取10条记录。按times字段进行排序,取最近发表的10表记录。这里有一个name字段。我想取到的这10条记录中如果是同一个用户发表的,哪么就只取二个。也就是同一个用户只取二条,然后再从后面的提一条上来。不知道说明白没有。我举一个例子
比如说前十条记录。张三发表了3条,可是我只取它的前二条,把第十一条李四发表提上来。
比如说前十条记录。张三发表了3条,可是我只取它的前二条,把第十一条李四发表提上来。
解决方案 »
- 关于任务计划,请教!
- 一个较难的问题:求一个先进先出的的SQL
- 在线急等,在表有数据的情况下将text字段转换成ntext
- 关于存储过程中的查询另一个服务器上的数据的问题!
- 各位大侠,帮帮忙,这句sql2005的语句为什么不能在sql2000中运行!
- 实现2个触发器
- 关于SQL模糊查询的优化!
- MSSQL 问题
- 请问如何知道用户对大数据字段(如:binary)作了什么修改?
- DB2的控製中心启动時出錯"[IBM][JDBC DRIVER]CLI0616E開啟SOCKET時發生錯誤SQL STATE=08S01",請問如何解決???
- 求相同ID记录日期最大的一条记录
- 菜鸟提问,存储过程中变量和引号的问题。
类别编号 说明 排序
a aa 1
a aa2 2
a aa3 3
b bb 1
b bb2 2
b bb3 3
c cc 1
c cc2 2
c cc3 3
需要查询出来的结果是每个类别的头2条记录,按排序进行排序,结果如下:
类别编号 说明 排序
a aa 1
a aa2 2
b bb 1
b bb2 2
c cc 1
c cc2 2
谢谢各位了!只要测试通过马上给分!
if object_id('pubs..t1') is not null
drop table t1
gocreate table t1(
类别编号 varchar(10),
说明 varchar(10),
排序 int
)
insert into t1(类别编号,说明,排序) values('a','aa',1)
insert into t1(类别编号,说明,排序) values('a','aa2',2)
insert into t1(类别编号,说明,排序) values('a','aa3',3)
insert into t1(类别编号,说明,排序) values('b','bb',1)
insert into t1(类别编号,说明,排序) values('b','bb2',2)
insert into t1(类别编号,说明,排序) values('b','bb3',3)
insert into t1(类别编号,说明,排序) values('c','cc',1)
insert into t1(类别编号,说明,排序) values('c','cc2',2)
insert into t1(类别编号,说明,排序) values('c','cc3',3)select * from t1 as t
where (select count(*) from t1 where 类别编号 = t.类别编号 and 排序 < t.排序) < 2drop table t1
类别编号 说明 排序
---------- ---------- -----------
a aa 1
a aa2 2
b bb 1
b bb2 2
c cc 1
c cc2 2(所影响的行数为 6 行)
--方法一:
Select * From news A
Where (Select Count(*) From news Where classAddDate = A.classAddDate And AddDate > A.AddDate) < 3
Order By classAddDate, AddDate--方法二:
Select * From news A
Where Exists (Select Count(*) From news Where classAddDate = A.classAddDate And AddDate > A.AddDate Having Count(*) < 3)
Order By classAddDate, AddDate--方法三:
Select * From news A
Where AddDate In (Select TOP 3 AddDate From news Where classAddDate = A.classAddDate Order By AddDate Desc)
Order By classAddDate, AddDate
Where (Select Count(*) From tb Where name = A.name And times > A.times) < 2
Order By times desc
比如说前十条记录。张三发表了3条,可是我只取它的前二条,把第十一条李四发表提上来。 select TOP 10 * from tb
where (select count(1) from tb t where name=tb.name and times>tb.times)<2
order by times desc
Where (Select Count(*) From tb Where name = A.name And times > A.times) < 3
Order By times desc