• 全部
  • 基础类
  • 应用实例
  • 新技术前沿

请高手帮忙,20分,只要能解决,不够再加

hww2004 2008-02-26 02:12:06
有如下表:A,
CUS_NO PRD_NO S_DD UP
====== ======= ========== =====
A BAD001 2007-12-01 2.0
A BAD001 2008-01-21 2.2
A BAD001 2008-02-15 2.1
B BAD001 2008-01-11 2.2
B BAD001 2008-02-22 2.3
C BAD001 2008-02-21 2.2
C BAD001 2008-02-23 2.1
A1 BAD002 2008-01-21 2.2
A2 BAD002 2008-01-21 2.3
A2 BAD002 2008-01-26 2.2
... ... ... ...
例如:希望将BAD001对应CUS_NO中取A,BAD001,2008-02-15(最近日期)的记录 ,
希望取如下结果后的补集合
CUS_NO PRD_NO S_DD UP
====== ======= ========== =====
A BAD001 2008-02-15 2.1
B BAD001 2008-02-22 2.3
C BAD001 2008-02-23 2.1
A1 BAD002 2008-01-21 2.2
A2 BAD002 2008-01-26 2.2
个人感觉比较难描述,就将需求结果列示出来让大家容易看懂。
请专家、高手帮忙!重谢!
...全文
59 点赞 收藏 10
写回复
10 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
pt1314917 2008-02-26
晕。8楼的不是行吗?
回复
-狙击手- 2008-02-26
create table A(CUS_NO varchar(8),PRD_NO varchar(10),S_DD datetime,UP decimal(9,2))
go
insert into A
select 'A','BAD001','2007-12-01',2.0
union select 'A','BAD001','2008-01-21',2.2
union select 'A','BAD001','2008-02-15',2.1
union select 'B','BAD001','2008-01-11',2.2
union select 'B','BAD001','2008-02-22',2.3
union select 'C','BAD001','2008-02-21',2.2
union select 'C','BAD001','2008-02-23',2.1
union select 'A1','BAD002','2008-01-21',2.2
union select 'A2','BAD002','2008-01-21',2.3
union select 'A2','BAD002','2008-01-26',2.3
union select 'A2','BAD002','2008-01-26',2.2
go
select * from A
select cus_no,prd_no,s_dd,up
from A
where not exists(select *
from A b
where datepart(yy,a.S_DD)= datepart(yy,b.S_DD) and a.cus_no=b.cus_no and a.prd_no=b.prd_no and a.s_dd > b.s_dd )
order by len(cus_no),cus_no
go
drop table A;
go
/*

cus_no prd_no s_dd up
-------- ---------- ------------------------------------------------------ -----------
A BAD001 2007-12-01 00:00:00.000 2.00
A BAD001 2008-01-21 00:00:00.000 2.20
B BAD001 2008-01-11 00:00:00.000 2.20
C BAD001 2008-02-21 00:00:00.000 2.20
A1 BAD002 2008-01-21 00:00:00.000 2.20
A2 BAD002 2008-01-21 00:00:00.000 2.30

(所影响的行数为 6 行)
*/
回复
pt1314917 2008-02-26

--哦。明白搂主的意思了。。


select * from ta a where exists
(select 1 from ta where CUS_NO=a.CUS_NO and PRD_NO=a.PRD_NO and S_DD>a.S_DD)

回复
-狙击手- 2008-02-26
CUS_NO PRD_NO S_DD UP
======= =========== ==== =====
A BAD001 2007-12-01 2.0
A BAD001 2008-01-21 2.2
B BAD001 2008-01-11 2.2
C BAD001 2008-02-21 2.2
A2 BAD002 2008-01-21 2.3


------------------------


A BAD001 2007-12-01 2.0
A BAD001 2008-01-21 2.2
一样的?
回复
pt1314917 2008-02-26

--怎么每天都有无数个这种类型的题目啊

select * from 表名 a where not exists
(select 1 from 表名 where CUS_NO=a.CUS_NO and PRD_NO=a.PRD_NO and S_DD>a.S_DD)
回复
hww2004 2008-02-26
wzh1215:
您好,您的语句仍然有问题。希望的结果是:
CUS_NO PRD_NO S_DD UP
======= =========== ==== =====
A BAD001 2007-12-01 2.0
A BAD001 2008-01-21 2.2
B BAD001 2008-01-11 2.2
C BAD001 2008-02-21 2.2
A2 BAD002 2008-01-21 2.3

回复
wzh1215 2008-02-26
select * from A
where not exists(select 1 from A AA where A.CUS_NO=AA.CUS_NO AND A.PRD_NO=AA.PRD_NO AND A.S_DD>AA.S_DD)
回复
zefuzhang2008 2008-02-26
with t as(select t.*,row_number() over(PARTITION BY CUS_NO order by  S_DD desc) as rowid
from t)
select CUS_NO,PRD_NO,S_DD,UP
from t
where rowid=1
回复
liangCK 2008-02-26
create table A(CUS_NO varchar(8),PRD_NO varchar(10),S_DD datetime,UP decimal(9,2))
go
insert into A
select 'A','BAD001','2007-12-01',2.0
union select 'A','BAD001','2008-01-21',2.2
union select 'A','BAD001','2008-02-15',2.1
union select 'B','BAD001','2008-01-11',2.2
union select 'B','BAD001','2008-02-22',2.3
union select 'C','BAD001','2008-02-21',2.2
union select 'C','BAD001','2008-02-23',2.1
union select 'A1','BAD002','2008-01-21',2.2
union select 'A2','BAD002','2008-01-21',2.3
union select 'A2','BAD002','2008-01-26',2.3
union select 'A2','BAD002','2008-01-26',2.2
go
select * from A;
select cus_no,prd_no,s_dd,up
from A
where not exists(select * from A b where a.cus_no=b.cus_no and a.prd_no=b.prd_no and checksum(a.s_dd,a.up)>checksum(b.s_dd,b.up))
order by len(cus_no),cus_no
go
drop table A;
go

(11 行受影响)
CUS_NO PRD_NO S_DD UP
-------- ---------- ----------------------- ---------------------------------------
A BAD001 2007-12-01 00:00:00.000 2.00
A BAD001 2008-01-21 00:00:00.000 2.20
A BAD001 2008-02-15 00:00:00.000 2.10
A1 BAD002 2008-01-21 00:00:00.000 2.20
A2 BAD002 2008-01-21 00:00:00.000 2.30
A2 BAD002 2008-01-26 00:00:00.000 2.20
A2 BAD002 2008-01-26 00:00:00.000 2.30
B BAD001 2008-01-11 00:00:00.000 2.20
B BAD001 2008-02-22 00:00:00.000 2.30
C BAD001 2008-02-21 00:00:00.000 2.20
C BAD001 2008-02-23 00:00:00.000 2.10

(11 行受影响)

cus_no prd_no s_dd up
-------- ---------- ----------------------- ---------------------------------------
A BAD001 2008-02-15 00:00:00.000 2.10
B BAD001 2008-02-22 00:00:00.000 2.30
C BAD001 2008-02-23 00:00:00.000 2.10
A1 BAD002 2008-01-21 00:00:00.000 2.20
A2 BAD002 2008-01-21 00:00:00.000 2.30

(5 行受影响)
回复
liangCK 2008-02-26
--按某一字段分组取最大(小)值所在行的数据
(爱新觉罗.毓华 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')
go

select * , px = identity(int,1,1) into tmp from tb

select 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')
go

select 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 行受影响)
*/
回复
相关推荐
发帖
MS-SQL Server
创建于2007-09-28

3.3w+

社区成员

MS-SQL Server相关内容讨论专区
申请成为版主
帖子事件
创建了帖子
2008-02-26 02:12
社区公告
暂无公告