求一SQL语句

xiaohe185 2010-04-25 11:21:04
描述:
表中有若干字段,其中,主键为:
行号,编号,组别
列举数据:
标识 行号 编号 组别 ......
1 1 S001 1
2 2 S001 1
3 2 S001 2
4 2 S001 3
5 3 S001 1 .......
6 1 S002 1

先求一SQL语句实现 根据编号检索出其中对应的记录,
要求,当行号重复时,取组别最大者

如果检索编号为S001的记录,上述数据检索结果为:
标识 行号 编号 组别 ......
1 1 S001 1
4 2 S001 3
5 3 S001 1 .......
在线等,请高手指教
...全文
55 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
xiaohe185 2010-04-25
  • 打赏
  • 举报
回复
谢谢各位,结贴给分了~~
dawugui 2010-04-25
  • 打赏
  • 举报
回复
--sql 2005
create table tb(标识 int,行号 int,编号 varchar(10),组别 int)
insert into tb values(1 ,1 ,'S001', 1)
insert into tb values(2 ,2 ,'S001', 1)
insert into tb values(3 ,2 ,'S001', 2)
insert into tb values(4 ,2 ,'S001', 3)
insert into tb values(5 ,3 ,'S001', 1)
insert into tb values(6 ,1 ,'S002', 1)
go

select 标识,行号,编号,组别 from
(
select t.* , px = row_number() over(partition by 行号 order by 组别 desc , 标识) from tb t
) m
where px = 1

drop table tb

/*
标识 行号 编号 组别
----------- ----------- ---------- -----------
1 1 S001 1
4 2 S001 3
5 3 S001 1

(3 行受影响)


*/
xiaohe185 2010-04-25
  • 打赏
  • 举报
回复
申明:谢谢楼上各位,
之前回复说不行的,其实是可以的,因为我直接拷贝过来没有再加上 编号='s001',所以没有得到我想要的结果

我现在用的数据库是SQLserver2000,row_number()这个函数好像没有,呵呵呵
dawugui 2010-04-25
  • 打赏
  • 举报
回复
create table tb(标识 int,行号 int,编号 varchar(10),组别 int)
insert into tb values(1 ,1 ,'S001', 1)
insert into tb values(2 ,2 ,'S001', 1)
insert into tb values(3 ,2 ,'S001', 2)
insert into tb values(4 ,2 ,'S001', 3)
insert into tb values(5 ,3 ,'S001', 1)
insert into tb values(6 ,1 ,'S002', 1)
go

select t.* from tb t where 组别 = (select max(组别) from tb where 行号 = t.行号) order by t.行号
select t.* from tb t where not exists (select 1 from tb where 行号 = t.行号 and 组别 > t.组别) order by t.行号

/*
标识 行号 编号 组别
----------- ----------- ---------- -----------
1 1 S001 1
6 1 S002 1
4 2 S001 3
5 3 S001 1

(所影响的行数为 4 行)

标识 行号 编号 组别
----------- ----------- ---------- -----------
1 1 S001 1
6 1 S002 1
4 2 S001 3
5 3 S001 1

(所影响的行数为 4 行)
*/
select m.* from
(
select t.* from tb t where 组别 = (select max(组别) from tb where 行号 = t.行号)
) m where 标识 = (select min(标识) from
(
select t.* from tb t where 组别 = (select max(组别) from tb where 行号 = t.行号)
) n where 行号 = m.行号
)
order by m.行号

select m.* from
(
select t.* from tb t where not exists (select 1 from tb where 行号 = t.行号 and 组别 > t.组别)
) m where 标识 = (select min(标识) from
(
select t.* from tb t where not exists (select 1 from tb where 行号 = t.行号 and 组别 > t.组别)
) n where 行号 = m.行号
)
order by m.行号
/*
标识 行号 编号 组别
----------- ----------- ---------- -----------
1 1 S001 1
4 2 S001 3
5 3 S001 1

(所影响的行数为 3 行)

标识 行号 编号 组别
----------- ----------- ---------- -----------
1 1 S001 1
4 2 S001 3
5 3 S001 1

(所影响的行数为 3 行)
*/

select 标识,行号,编号,组别 from
(
select t.* , px = (select count(1) from tb where 行号 = t.行号 and (组别 > t.组别 or (组别 = t.组别 and 标识 < t.标识)) ) + 1 from tb t
) m
where px = 1
/*
标识 行号 编号 组别
----------- ----------- ---------- -----------
1 1 S001 1
4 2 S001 3
5 3 S001 1

(所影响的行数为 3 行)
*/

drop table tb
htl258_Tony 2010-04-25
  • 打赏
  • 举报
回复
--------------------------------------------------------------------------
-- Author : htl258(Tony)
-- Date : 2010-04-25 11:25:27
-- Version:Microsoft SQL Server 2008 (RTM) - 10.0.1600.22 (Intel X86)
-- Jul 9 2008 14:43:34
-- Copyright (c) 1988-2008 Microsoft Corporation
-- Developer Edition on Windows NT 5.1 <X86> (Build 2600: Service Pack 2)
-- Blog : http://blog.csdn.net/htl258
--------------------------------------------------------------------------
--> 生成测试数据表:a

IF NOT OBJECT_ID('[a]') IS NULL
DROP TABLE [a]
GO
CREATE TABLE [a]([标识] INT,[行号] INT,[编号] NVARCHAR(10),[组别] INT)
INSERT [a]
SELECT 1,1,'S001',1 UNION ALL
SELECT 2,2,'S001',1 UNION ALL
SELECT 3,2,'S001',2 UNION ALL
SELECT 4,2,'S001',3 UNION ALL
SELECT 5,3,'S001',1 UNION ALL
SELECT 6,1,'S002',1
GO
--SELECT * FROM [a]

-->SQL查询如下:
select * from a t where not exists(select 1 from a where 行号=t.行号 and (组别>t.组别 or 组别=t.组别 and 标识<t.标识))
/*
标识 行号 编号 组别
----------- ----------- ---------- -----------
1 1 S001 1
4 2 S001 3
5 3 S001 1

(3 行受影响)
*/
dawugui 2010-04-25
  • 打赏
  • 举报
回复
create table tb(标识 int,行号 int,编号 varchar(10),组别 int)
insert into tb values(1 ,1 ,'S001', 1)
insert into tb values(2 ,2 ,'S001', 1)
insert into tb values(3 ,2 ,'S001', 2)
insert into tb values(4 ,2 ,'S001', 3)
insert into tb values(5 ,3 ,'S001', 1)
insert into tb values(6 ,1 ,'S002', 1)
go

select t.* from tb t where 组别 = (select max(组别) from tb where 行号 = t.行号) order by t.行号
select t.* from tb t where not exists (select 1 from tb where 行号 = t.行号 and 组别 > t.组别) order by t.行号

/*
标识 行号 编号 组别
----------- ----------- ---------- -----------
1 1 S001 1
6 1 S002 1
4 2 S001 3
5 3 S001 1

(所影响的行数为 4 行)

标识 行号 编号 组别
----------- ----------- ---------- -----------
1 1 S001 1
6 1 S002 1
4 2 S001 3
5 3 S001 1

(所影响的行数为 4 行)
*/
select m.* from
(
select t.* from tb t where 组别 = (select max(组别) from tb where 行号 = t.行号)
) m where 标识 = (select min(标识) from
(
select t.* from tb t where 组别 = (select max(组别) from tb where 行号 = t.行号)
) n where 行号 = m.行号
)
order by m.行号

select m.* from
(
select t.* from tb t where not exists (select 1 from tb where 行号 = t.行号 and 组别 > t.组别)
) m where 标识 = (select min(标识) from
(
select t.* from tb t where not exists (select 1 from tb where 行号 = t.行号 and 组别 > t.组别)
) n where 行号 = m.行号
)
order by m.行号
/*
标识 行号 编号 组别
----------- ----------- ---------- -----------
1 1 S001 1
4 2 S001 3
5 3 S001 1

(所影响的行数为 3 行)

标识 行号 编号 组别
----------- ----------- ---------- -----------
1 1 S001 1
4 2 S001 3
5 3 S001 1

(所影响的行数为 3 行)
*/

drop table tb
喜-喜 2010-04-25
  • 打赏
  • 举报
回复
--上面的怎么变成黑白的了,郁闷...

select 标识,行号,编号,组别 from(select idd=row_number()over(partition by 行号 order by 组别 desc,行号),* from tb where 编号='S001')t where idd=1
xiaohe185 2010-04-25
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 htl258 的回复:]
SQL code
select * from tb t where not exists(select 1 from tb where 行号=t.行号 and 组别>t.组别)
[/Quote]
一样的呀~~
dawugui 2010-04-25
  • 打赏
  • 举报
回复
create table tb(标识 int,行号 int,编号 varchar(10),组别 int)
insert into tb values(1 ,1 ,'S001', 1)
insert into tb values(2 ,2 ,'S001', 1)
insert into tb values(3 ,2 ,'S001', 2)
insert into tb values(4 ,2 ,'S001', 3)
insert into tb values(5 ,3 ,'S001', 1)
insert into tb values(6 ,1 ,'S002', 1)
go

select t.* from tb t where 组别 = (select max(组别) from tb where 行号 = t.行号) order by t.行号
select t.* from tb t where not exists (select 1 from tb where 行号 = t.行号 and 组别 > t.组别) order by t.行号
/*
标识 行号 编号 组别
----------- ----------- ---------- -----------
1 1 S001 1
6 1 S002 1
4 2 S001 3
5 3 S001 1

(所影响的行数为 4 行)

标识 行号 编号 组别
----------- ----------- ---------- -----------
1 1 S001 1
6 1 S002 1
4 2 S001 3
5 3 S001 1

(所影响的行数为 4 行)
*/


drop table tb
喜-喜 2010-04-25
  • 打赏
  • 举报
回复
--------------------SQL Server数据格式化工具-------------------
---------------------------------------------------------------
-- DESIGNER :happycell188(喜喜)
-- QQ :584738179
-- Development Tool :Microsoft Visual C++ 6.0 C Language
-- FUNCTION :CONVERT DATA TO T-SQL
---------------------------------------------------------------
-- Microsoft SQL Server 2005
-- Developer Edition on Microsoft Windows XP [版本 5.1.2600]
---------------------------------------------------------------
---------------------------------------------------------------

use test
go
if object_id('test.dbo.tb') is not null drop table tb
-- 创建数据表
create table tb
(
标识 int,
行号 int,
编号 char(5),
组别 int
)
go
--插入测试数据
insert into tb select 1,1,'S001',1
union all select 2,2,'S001',1
union all select 3,2,'S001',2
union all select 4,2,'S001',3
union all select 5,3,'S001',1
union all select 6,1,'S002',1
go
--代码实现

select 标识,行号,编号,组别 from(
select idd=row_number()over(partition by 行号 order by 组别 desc,行号),* from tb where 编号='S001')t
where idd=1

/*测试结果

标识 行号 编号 组别
---------------------
1 1 S001 1
4 2 S001 3
5 3 S001 1

(3 行受影响)
*/
htl258_Tony 2010-04-25
  • 打赏
  • 举报
回复
select * from tb t where not exists(select 1 from tb where 行号=t.行号 and 标识>t.标识)
xiaohe185 2010-04-25
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 fredrickhu 的回复:]
SQL code
select
*
from
tb t
where
组别=(select max(组别) from tb where 行号=t.行号)
[/Quote]
首先谢谢你的解答,但我试了一下,结果是将上述五条记录全部检索出来了,没有实现我的要求~~
dawugui 2010-04-25
  • 打赏
  • 举报
回复

select t.* from tb t where 组别 = (select max(组别) from tb where 行号 = t.行号)
select t.* from tb t where not exists (select 1 from tb where 行号 = t.行号 and 组别 > t.组别)
htl258_Tony 2010-04-25
  • 打赏
  • 举报
回复
select * from tb t where not exists(select 1 from tb where 行号=t.行号 and 组别>t.组别)
--小F-- 2010-04-25
  • 打赏
  • 举报
回复
select
*
from
tb t
where
组别=(select max(组别) from tb where 行号=t.行号)

34,590

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧