T_student
-------------------------------
student_id atime names
100 2007.1.1 吴米
101 2007.2.3 看你
--------------------------------
feedback
---------------------------------
student_id dqcs yxjg
100 1 1
100 2 1
100 3 1
101 1 2
101 2 3
101 3 4
101 4 1
---------------------------------结果:选择所有dqcs最大的yxjg=1的记录
----------------------------------
100 3 1
101 4 1
-----------------------------------
-------------------------------
student_id atime names
100 2007.1.1 吴米
101 2007.2.3 看你
--------------------------------
feedback
---------------------------------
student_id dqcs yxjg
100 1 1
100 2 1
100 3 1
101 1 2
101 2 3
101 3 4
101 4 1
---------------------------------结果:选择所有dqcs最大的yxjg=1的记录
----------------------------------
100 3 1
101 4 1
-----------------------------------
解决方案 »
- 谁有sql 练习题
- SQL Server2000中,备份设备和文件名有什么区别和联系?
- 我是ADSL接入,安装SQL Server后,程序无法(就是SQL 查询器也是)连接到该服务器,为什么?
- 怎么取出一个用户表中允许空和不允许空的字段?
- 同一个表中计算时间差(计算出花了多少个小时)
- 管理复制的问题
- 求sql server语句
- 生成存贮过程的问题,这个存贮过程含有一个临时表(非本存贮过程DEFINED)
- 我的sql server一修改数据表的数据,就提示”键列信息不足或不正确,更改影响过多的行“
- 请问谁知道怎样用oledb调用sqlserver的存储过程?我总是出现参数设置不正确的错误
- 我有一个数据表,结构为
- 各位大俠幫忙看下使用SQL Server DTS 執行時間為什么不能修改啊?
where yxjg=1 and not exists(Select 1 from feedback
where yxjg=1 and student_id=t.student_id
and dqcs>t.dqcs)
(select t.* from feedback t where yxjg = 1 and dqcs = (select max(dqcs) from feedback where yxjg = 1 and student_id = t.student_id)) n
where m.student_id = n.student_id
(爱新觉罗.毓华 2007-10-23于浙江杭州)
/*
数据如下:
name val memo
a 2 a2(a的第二个值)
a 1 a1--a的第一个值
a 3 a3:a的第三个值
b 1 b1--b的第一个值
b 3 b3:b的第三个值
b 2 b2b2b2b2
b 4 b4b4
b 5 b5b5b5b5b5
*/
--创建表并插入数据:
create table tb(name varchar(10),val int,memo varchar(20))
insert into tb values('a', 2, 'a2(a的第二个值)')
insert into tb values('a', 1, 'a1--a的第一个值')
insert into tb values('a', 3, 'a3:a的第三个值')
insert into tb values('b', 1, 'b1--b的第一个值')
insert into tb values('b', 3, 'b3:b的第三个值')
insert into tb values('b', 2, 'b2b2b2b2')
insert into tb values('b', 4, 'b4b4')
insert into tb values('b', 5, 'b5b5b5b5b5')
go--一、按name分组取val最大的值所在行的数据。
--方法1:
select a.* from tb a where val = (select max(val) from tb where name = a.name) order by a.name
--方法2:
select a.* from tb a where not exists(select 1 from tb where name = a.name and val > a.val)
--方法3:
select a.* from tb a,(select name,max(val) val from tb group by name) b where a.name = b.name and a.val = b.val order by a.name
--方法4:
select a.* from tb a inner join (select name , max(val) val from tb group by name) b on a.name = b.name and a.val = b.val order by a.name
--方法5
select a.* from tb a where 1 > (select count(*) from tb where name = a.name and val > a.val ) order by a.name
/*
name val memo
---------- ----------- --------------------
a 3 a3:a的第三个值
b 5 b5b5b5b5b5
*/--二、按name分组取val最小的值所在行的数据。
--方法1:
select a.* from tb a where val = (select min(val) from tb where name = a.name) order by a.name
--方法2:
select a.* from tb a where not exists(select 1 from tb where name = a.name and val < a.val)
--方法3:
select a.* from tb a,(select name,min(val) val from tb group by name) b where a.name = b.name and a.val = b.val order by a.name
--方法4:
select a.* from tb a inner join (select name , min(val) val from tb group by name) b on a.name = b.name and a.val = b.val order by a.name
--方法5
select a.* from tb a where 1 > (select count(*) from tb where name = a.name and val < a.val) order by a.name
/*
name val memo
---------- ----------- --------------------
a 1 a1--a的第一个值
b 1 b1--b的第一个值
*/--三、按name分组取第一次出现的行所在的数据。
select a.* from tb a where val = (select top 1 val from tb where name = a.name) order by a.name
/*
name val memo
---------- ----------- --------------------
a 2 a2(a的第二个值)
b 1 b1--b的第一个值
*/--四、按name分组随机取一条数据。
select a.* from tb a where val = (select top 1 val from tb where name = a.name order by newid()) order by a.name
/*
name val memo
---------- ----------- --------------------
a 1 a1--a的第一个值
b 5 b5b5b5b5b5
*/--五、按name分组取最小的两个(N个)val
select a.* from tb a where 2 > (select count(*) from tb where name = a.name and val < a.val ) order by a.name,a.val
select a.* from tb a where val in (select top 2 val from tb where name=a.name order by val) order by a.name,a.val
select a.* from tb a where exists (select count(*) from tb where name = a.name and val < a.val having Count(*) < 2) order by a.name
/*
name val memo
---------- ----------- --------------------
a 1 a1--a的第一个值
a 2 a2(a的第二个值)
b 1 b1--b的第一个值
b 2 b2b2b2b2
*/--六、按name分组取最大的两个(N个)val
select a.* from tb a where 2 > (select count(*) from tb where name = a.name and val > a.val ) order by a.name,a.val
select a.* from tb a where val in (select top 2 val from tb where name=a.name order by val desc) order by a.name,a.val
select a.* from tb a where exists (select count(*) from tb where name = a.name and val > a.val having Count(*) < 2) order by a.name
/*
name val memo
---------- ----------- --------------------
a 2 a2(a的第二个值)
a 3 a3:a的第三个值
b 4 b4b4
b 5 b5b5b5b5b5
*/
--七,如果整行数据有重复,所有的列都相同。
/*
数据如下:
name val memo
a 2 a2(a的第二个值)
a 1 a1--a的第一个值
a 1 a1--a的第一个值
a 3 a3:a的第三个值
a 3 a3:a的第三个值
b 1 b1--b的第一个值
b 3 b3:b的第三个值
b 2 b2b2b2b2
b 4 b4b4
b 5 b5b5b5b5b5
*/
--在sql server 2000中只能用一个临时表来解决,生成一个自增列,先对val取最大或最小,然后再通过自增列来取数据。
--创建表并插入数据:
create table tb(name varchar(10),val int,memo varchar(20))
insert into tb values('a', 2, 'a2(a的第二个值)')
insert into tb values('a', 1, 'a1--a的第一个值')
insert into tb values('a', 1, 'a1--a的第一个值')
insert into tb values('a', 3, 'a3:a的第三个值')
insert into tb values('a', 3, 'a3:a的第三个值')
insert into tb values('b', 1, 'b1--b的第一个值')
insert into tb values('b', 3, 'b3:b的第三个值')
insert into tb values('b', 2, 'b2b2b2b2')
insert into tb values('b', 4, 'b4b4')
insert into tb values('b', 5, 'b5b5b5b5b5')
goselect * , px = identity(int,1,1) into tmp from tbselect m.name,m.val,m.memo from
(
select t.* from tmp t where val = (select min(val) from tmp where name = t.name)
) m where px = (select min(px) from
(
select t.* from tmp t where val = (select min(val) from tmp where name = t.name)
) n where n.name = m.name)drop table tb,tmp/*
name val memo
---------- ----------- --------------------
a 1 a1--a的第一个值
b 1 b1--b的第一个值(2 行受影响)
*/
--在sql server 2005中可以使用row_number函数,不需要使用临时表。
--创建表并插入数据:
create table tb(name varchar(10),val int,memo varchar(20))
insert into tb values('a', 2, 'a2(a的第二个值)')
insert into tb values('a', 1, 'a1--a的第一个值')
insert into tb values('a', 1, 'a1--a的第一个值')
insert into tb values('a', 3, 'a3:a的第三个值')
insert into tb values('a', 3, 'a3:a的第三个值')
insert into tb values('b', 1, 'b1--b的第一个值')
insert into tb values('b', 3, 'b3:b的第三个值')
insert into tb values('b', 2, 'b2b2b2b2')
insert into tb values('b', 4, 'b4b4')
insert into tb values('b', 5, 'b5b5b5b5b5')
goselect m.name,m.val,m.memo from
(
select * , px = row_number() over(order by name , val) from tb
) m where px = (select min(px) from
(
select * , px = row_number() over(order by name , val) from tb
) n where n.name = m.name)drop table tb/*
name val memo
---------- ----------- --------------------
a 1 a1--a的第一个值
b 1 b1--b的第一个值(2 行受影响)
*/
where yxjg=1 and not exists (
select 1 from feedback
where yxjg=1
and student_id=a.student_id
and dqcs>a.dqcs
)
insert into t_student values(100, '2007.1.1', '吴米')
insert into t_student values(101, '2007.2.3', '看你')
create table feedback (student_id int, dqcs int, yxjg int)
insert into feedback values(100, 1, 1 )
insert into feedback values(100, 2, 1 )
insert into feedback values(100, 3, 1 )
insert into feedback values(101, 1, 2 )
insert into feedback values(101, 2, 3 )
insert into feedback values(101, 3, 4 )
insert into feedback values(101, 4, 1 )
goselect t.* from feedback t where yxjg = 1 and dqcs = (select max(dqcs) from feedback where yxjg = 1 and student_id = t.student_id) order by t.student_id
/*
student_id dqcs yxjg
----------- ----------- -----------
100 3 1
101 4 1
(2 行受影响)
*/
select m.student_id , n.dqcs , n.yxjg from T_student m,
(select t.* from feedback t where yxjg = 1 and dqcs = (select max(dqcs) from feedback where yxjg = 1 and student_id = t.student_id)) n
where m.student_id = n.student_id
order by m.student_id
/*
student_id dqcs yxjg
----------- ----------- -----------
100 3 1
101 4 1
(2 行受影响)
*/--drop table t_student , feedback
create table T_student (student_id int , atime varchar(10), names varchar(10))
insert into t_student values(100, '2007.1.1', '吴米')
insert into t_student values(101, '2007.2.3', '看你')
go
create table feedback (student_id int, dqcs int, yxjg int)
insert into feedback values(100, 1, 1 )
insert into feedback values(100, 2, 1 )
insert into feedback values(100, 3, 1 )
insert into feedback values(101, 1, 2 )
insert into feedback values(101, 2, 3 )
insert into feedback values(101, 3, 4 )
insert into feedback values(101, 4, 1 )
go
select * from Feedback a
where not exists(select 1 from Feedback b where a.student_id=b.student_id and a.dqcs<b.dqcs)
and yxjg=1
and exists(select 1 from T_student c where a.student_id=c.student_Id)
/*
student_id dqcs yxjg
----------- ----------- -----------
100 3 1
101 4 1(2 行受影响)
*/
select * from feedback a where yxjg =1 and
not exists(select 1 from feedback where yxjg =1 and student_id=a.student_id and dqcs>a.dqcs)或者:
select * from feedback a where yxjg =1 and dqcs
in(select max(dqcs) from feedback where student_id=a.student_id and yxjg =1)