use [test]
go/****** object: table [dbo].[scores] script date: 2018/3/11 17:59:15 ******/
set ansi_nulls on
goset quoted_identifier on
goset ansi_padding on
gocreate
table [dbo].[scores](
[id] [int] identity(1,1) not
null,
[name] [varchar](10) null,
[course] [varchar](10) null,
[score] [decimal](4, 2) null,
constraint [pk__score__3214ec276d139b95] primary
key clustered
( [id] asc
)with (pad_index = off, statistics_norecompute = off, ignore_dup_key = off, allow_row_locks = on, allow_page_locks = on) on [primary]
) on [primary]
goset ansi_padding off
goset identity_insert [dbo].[scores] on
insert [dbo].[scores] ([id], [name], [course], [score]) values (1, n'小明', n'語文', cast(90.00
asdecimal(4, 2)))
insert [dbo].[scores] ([id], [name], [course], [score]) values (2, n'小明', n'數學', cast(80.00
asdecimal(4, 2)))
insert [dbo].[scores] ([id], [name], [course], [score]) values (3, n'小明', n'英語', cast(60.00
asdecimal(4, 2)))
insert [dbo].[scores] ([id], [name], [course], [score]) values (4, n'小紅', n'語文', cast(93.00
asdecimal(4, 2)))
insert [dbo].[scores] ([id], [name], [course], [score]) values (5, n'小紅', n'數學', cast(92.00
asdecimal(4, 2)))
insert [dbo].[scores] ([id], [name], [course], [score]) values (6, n'小紅', n'英語', cast(91.00
asdecimal(4, 2)))
insert [dbo].[scores] ([id], [name], [course], [score]) values (7, n'小花', n'語文', cast(50.00
asdecimal(4, 2)))
insert [dbo].[scores] ([id], [name], [course], [score]) values (8, n'小花', n'數學', cast(30.00
asdecimal(4, 2)))
insert [dbo].[scores] ([id], [name], [course], [score]) values (9, n'小花', n'英語', cast(70.00
asdecimal(4, 2)))
insert [dbo].[scores] ([id], [name], [course], [score]) values (10, n'小草', n'語文', cast(95.00
asdecimal(4, 2)))
insert [dbo].[scores] ([id], [name], [course], [score]) values (11, n'小草', n'數學', cast(86.00
asdecimal(4, 2)))
insert [dbo].[scores] ([id], [name], [course], [score]) values (12, n'小草', n'英語', cast(62.00
asdecimal(4, 2)))
insert [dbo].[scores] ([id], [name], [course], [score]) values (13, n'小剛', n'語文', cast(78.00
asdecimal(4, 2)))
insert [dbo].[scores] ([id], [name], [course], [score]) values (14, n'小剛', n'數學', cast(68.00
asdecimal(4, 2)))
insert [dbo].[scores] ([id], [name], [course], [score]) values (15, n'小剛', n'英語', cast(76.00
asdecimal(4, 2)))
insert [dbo].[scores] ([id], [name], [course], [score]) values (16, n'小柔', n'語文', cast(56.00
asdecimal(4, 2)))
insert [dbo].[scores] ([id], [name], [course], [score]) values (17, n'小柔', n'數學', cast(86.00
asdecimal(4, 2)))
insert [dbo].[scores] ([id], [name], [course], [score]) values (18, n'小柔', n'英語', cast(88.00
asdecimal(4, 2)))
insert [dbo].[scores] ([id], [name], [course], [score]) values (19, n'小陳', n'語文', cast(77.00
asdecimal(4, 2)))
insert [dbo].[scores] ([id], [name], [course], [score]) values (20, n'小陳', n'數學', cast(88.00
asdecimal(4, 2)))
insert [dbo].[scores] ([id], [name], [course], [score]) values (21, n'小陳', n'英語', cast(99.00
asdecimal(4, 2)))
set identity_insert [dbo].[scores] off
方法一:
select course,name, score from scores sc where name in(
select top 3 name from scores where sc.course=course
)group
by course,name, score order
by course,score desc
方法二:
select * from (
select *, row_number() over(partition by course order
by score desc) rownum
from scores
) as s where s.rownum<=3
mysql分組取每組前幾條記錄
drop table if exists ho archives create table ho archives id mediumint 11 unsigned not null auto increment comment 自增id type smallint 5 unsigned not n...
SQL分組取每組前幾條記錄,剩下的列為其他
sqlserver 寫法 select from lx dbo 全國銷售額 where province 北京 group by province name 按銷售額排序 取每個省的前4名,剩下的為其他 select from select a.province,a.name a.value fro...
sql取分組的前幾條 指定條數
注 sqlserver下 create table test areaid int,score int insert into test select 0,10 union all select 0,20 union all select 0,30 union all select 0,40 uni...