SQL查询每月每天的前两条数据....

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SQL查询每月每天的前两条数据....相关的知识,希望对你有一定的参考价值。

如图,我需要一条SQL查询相同年月日的前两条,并且按时间排序之后的.像这里的12条就只需要4条,每天条数不定,ID唯一

参考技术A select *
from 表名 A
where 日期列 in(select top 2 日期列 from 表名 B where cast(A.日期列 as date)=cast(B.日期列 as date) order by 日期列)追问

mysql数据库好像不能用top 函数,报错1064

追答

select *
from 表名 A
where 日期列 in(select 日期列 from 表名 B where cast(A.日期列 as date)=cast(B.日期列 as date) order by 日期列 limit 1,2)

在SQL SERVER中,怎样查询每门功课成绩最好的前两名,帮我写一下,谢谢,请补充:

create table SC(
S# varchar(10), /*学号
C# varchar(10), /*课号
score decimal(18,1) /*成绩
)
insert into sc values('01' , '01' , 80)
insert into sc values('01' , '02' , 90)
insert into sc values('01' , '03' , 99)
insert into sc values('02' , '01' , 70)
insert into sc values('02' , '02' , 60)
insert into sc values('02' , '03' , 80)
insert into sc values('03' , '01' , 80)
insert into sc values('03' , '02' , 80)
insert into sc values('03' , '03' , 80)
insert into sc values('04' , '01' , 50)
insert into sc values('04' , '02' , 30)
insert into sc values('04' , '03' , 20)
insert into sc values('05' , '01' , 76)
insert into sc values('05' , '02' , 87)
insert into sc values('06' , '01' , 31)
insert into sc values('06' , '03' , 34)
insert into sc values('07' , '02' , 89)
insert into sc values('07' , '03' , 98)
go

查询每门功课成绩最好的前两名,求高手指教。
最后的执行结果是:
01 01 80.0
03 01 80.0
01 02 90.0
07 02 89.0
01 03 99.0
07 03 98.0
select sc1.s#,sc1.c#,sc1.score from sc sc1 where score in
(select top 2 sc2.score from sc sc2
where sc2.C# = sc1.C# order by sc2.score desc)
order by sc1.C# , sc1.score desc
这样的语句也能实现同样的功能,但我有点看不太懂,能各位高手能帮我分析一下吗,呵呵。谢谢。where sc2.C# = sc1.C#这里又用到了SC1,感觉有点头晕。

create table #SC(
S# varchar(10), --学号
C# varchar(10), --课号
score decimal(18,1) --成绩
)
insert into #sc values('01' , '01' , 80)
insert into #sc values('01' , '02' , 90)
insert into #sc values('01' , '03' , 99)
insert into #sc values('02' , '01' , 70)
insert into #sc values('02' , '02' , 60)
insert into #sc values('02' , '03' , 80)
insert into #sc values('03' , '01' , 80)
insert into #sc values('03' , '02' , 80)
insert into #sc values('03' , '03' , 80)
insert into #sc values('04' , '01' , 50)
insert into #sc values('04' , '02' , 30)
insert into #sc values('04' , '03' , 20)
insert into #sc values('05' , '01' , 76)
insert into #sc values('05' , '02' , 87)
insert into #sc values('06' , '01' , 31)
insert into #sc values('06' , '03' , 34)
insert into #sc values('07' , '02' , 89)
insert into #sc values('07' , '03' , 98)
create table #temp
(
S# varchar(10), --学号
C# varchar(10), --课号
score decimal(18,1) --成绩
)
insert into #temp
select top 2 *from #SC where C#='01' order by score desc
insert into #temp
select top 2 *from #SC where C#='02' order by score desc
insert into #temp
select top 2 *from #SC where C#='03' order by score desc
select *from #temp
drop table #temp
drop table #SC

运行结果
S# C# score
01 01 80.0
03 01 80.0
01 02 90.0
07 02 89.0
01 03 99.0
07 03 98.0
参考技术A select * from
(
select top 2 * from SC where C#='01' order by score desc
) a
union
select * from
(
select top 2 * from SC where C#='02' order by score desc
) b
union
select * from
(
select top 2 * from SC where C#='03' order by score desc
) c
order by C#,S#,score本回答被提问者采纳
参考技术B SELECT
*
FROM
SC
WHERE
( SELECT
COUNT(1)
FROM
SC SC1
WHERE
SC.C# = SC1.C#
AND SC.score < SC1.score
) < 2
ORDER BY
C#,S#追问

1, SC.score < SC1.score
这条语句是什么意思啦?
2,()<2为什么,括号内为什么要小于2呢?

参考技术C 正确答案,给分吧!
select * from sc a
where s# in (
select s# from (select top 2 b.score,s# from sc b
where b.c#=a.c#
group by score,s#
order by score desc) c

)
order by c#,score desc
参考技术D 单独科目去查吧,一起查我也不会,单独查某门科目前两名用top就可以追问

这样查我也会,还是多学学吧。

以上是关于SQL查询每月每天的前两条数据....的主要内容,如果未能解决你的问题,请参考以下文章

如何查询一个表中,各个分类的前面2条数据,用一条sql语句

oracle sql语句获取前两条数据

mysql按条件分页查询的语句怎么写啊?

在SQL SERVER中,怎样查询每门功课成绩最好的前两名,帮我写一下,谢谢,请补充:

您如何检索每个分组中的前两条记录

mysql 用一条sql语句取出各类数据的前两N条数据