22,209
社区成员
发帖
与我相关
我的任务
分享
--对表进行分组查询,在组内就进行排序,并且取得每组前5条记录. 还要获得每组使用其它条件查询时候的总记录数
--如:
--字段Key为int型.
select * from TableName where Word='FQ' and UserName='张三' group by Key order by ID desc
UNION ALL
select * from TableName where Word='FQ' and UserName='李四' group by Key order by ID desc
UNION ALL
select * from TableName where Word='FQ' and UserName='王五' group by Key order by ID desc
--这里只是分组了,我还要获得每组(条件Word='FQ')的记录总数
select count(ID) from TableName where Word='FQ'
---------------------------------------------------------
--上面的SQL是我的个人试想,实际运行是报错的.
-- 最后返回记录集.组程序中DataSet
----------------------------------------------------------
select m.*,n.cnt
from
(
select * from tb t where id in (select top 5 id from tb where Word='FQ' and [key] = t.[key] order by id)
) m join
(
select [key] , cnt = count(ID) from tb where Word='FQ' group by [key]
) n on m.[key] = n.[key]
-------分组前5条记录
select * from TableName a where Word='FQ' and id in(select top 5 id from TableName where Word='FQ' and UserName=a.UserName order by id desc)
---每组记录总数
select UserName,count(1)[总数] from TableName where Word='FQ' group by UserName
select m.*,n.cnt from
(
select * from tb t where id in (select top 5 id from tb where Word='FQ' and [key] = t.[key] order by id)
) m,
(
select [key] , cnt = count(ID) from tb where Word='FQ' group by [key]
) n
where m.[key] = n.[key]
select * from tb t
where id in
(
select top 5 id from tb where key = t.key order by id
)
group by Key?你要干什么?
select a.UserName
from TableName a
join TableName b
on a.UserName = b.UserName
where a.Word='FQ'
group by a.UserName
having count(case when a.id<= b.idthen 1 else null end) < = 5 --可动态修改
order by a.UserName
--测试环境
create table student --学生列表
(
s_id int,
s_name char(10)
)
create table class --课程列表
(
c_id int,
c_name char(10)
)
create table grade --学生成绩表
(
s_id int,
c_id int,
cj int
)
--测试数据
insert student values(1,'A')
insert student values(2,'B')
insert student values(3,'C')
insert student values(4,'D')
insert student values(5,'E')
insert class values(1,'历史')
insert class values(2,'数学')
insert grade values(1,1,60)
insert grade values(2,1,70)
insert grade values(3,1,80)
insert grade values(4,1,90)
insert grade values(5,1,100)
insert grade values(1,2,85)
insert grade values(2,2,67)
insert grade values(3,2,94)
insert grade values(4,2,63)
insert grade values(5,2,87)
select a.c_id,a.s_id,a.cj from grade a
join grade b
on a.c_id = b.c_id
group by a.c_id,a.s_id,a.cj
having count(case when a.cj <= b.cj then 1 else null end) < = 5 --可动态修改
order by a.c_id,a.cj desc
--处理重复分数
select a.c_id,a.s_id,a.cj
from grade a join
(
select c_id,cj
from grade
group by c_id,cj
) b
on a.c_id = b.c_id
group by a.c_id,a.s_id,a.cj
having count(case when a.cj <= b.cj then 1 else null end) < = 5 --可动态修改
order by a.c_id,a.cj desc
--你可以以动态的修改: n <= n 来获得
--每门课程的前n个最高分的
declare @ta table(n1 float,n2 float)
insert @ta values(2,3)
insert @ta values(8,5)
insert @ta values(8,5)
insert @ta values(9,3)
insert @ta values(3,1)
insert @ta values(10,4)
select n1,n2,cast((n2-n1)*100/n1 as decimal(10,2)) [(n2-n1)*100/n1] ,identity(int,1,1) id
into #temp
from @ta
select a.n1,a.n2,a.[(n2-n1)*100/n1],
count(case when a.[(n2-n1)*100/n1] <= b.[(n2-n1)*100/n1]
then 1 else null end) 名次
from #temp a cross join
(
select [(n2-n1)*100/n1]
from #temp
group by [(n2-n1)*100/n1]
) b
group by a.n1,a.n2,a.[(n2-n1)*100/n1],a.id
order by 名次
drop table #temp
n1 n2 (n2-n1)*100/n1 名次
------ ----- -------------- --------
2.0 3.0 50.00 1
8.0 5.0 -37.50 2
8.0 5.0 -37.50 2
10.0 4.0 -60.00 3
9.0 3.0 -66.67 4
3.0 1.0 -66.67 4
(所影响的行数为 6 行)