某列值相同,取另一列值的最大值

geminiiv 2008-03-25 10:58:09
假设表T1数据:
col1 col2 col3 col4 col5 col6
Y 10 5 123 A 你好
C 15 6 231 A 你好啊
C 15 5 321 A 你好吗
F 20 8 111 A 你真的好

想得到以下结果,如果col1列值相同,取col3列值的最大值:
col1 col2 col3 col4 col5 col6
Y 10 5 123 A 你好
C 15 6 231 A 你好啊
F 20 8 111 A 你真的好
...全文
1164 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
dobear_0922 2008-03-25
  • 打赏
  • 举报
回复
create table tb(col1 varchar(10),col2 int,col3 int,col4 int,col5 varchar(10),col6 varchar(10))
insert into tb select 'Y',10,5,123,'A','你好'
insert into tb select 'C',15,6,231,'A','你好啊'
insert into tb select 'C',15,5,321,'A','你好吗'
insert into tb select 'F',20,8,111,'A','你真的好'

select * from tb t where not exists(
select 1 from tb where col1=t.col1 and col3>t.col3
)
/*
col1 col2 col3 col4 col5 col6
---------- ----------- ----------- ----------- ---------- ----------
Y 10 5 123 A 你好
C 15 6 231 A 你好啊
F 20 8 111 A 你真的好

(3 row(s) affected)
*/

drop table tb
dawugui 2008-03-25
  • 打赏
  • 举报
回复
create table tb(col1 varchar(10) , col2 int , col3 int , col4 int , col5 varchar(10) , col6 varchar(10))
insert into tb values('Y', 10 , 5 , 123 , 'A', '你好')
insert into tb values('C', 15 , 6 , 231 , 'A', '你好啊')
insert into tb values('C', 15 , 5 , 321 , 'A', '你好吗')
insert into tb values('F', 20 , 8 , 111 , 'A', '你真的好')
go

--方法1:
select a.* from tb a where col3 = (select max(col3) from tb where col1 = a.col1) order by a.col1
--方法2:
select a.* from tb a where not exists(select 1 from tb where col1 = a.col1 and col3 > a.col3)
--方法3:
select a.* from tb a,(select col1,max(col3) col3 from tb group by col1) b where a.col1 = b.col1 and a.col3 = b.col3 order by a.col1
--方法4:
select a.* from tb a inner join (select col1 , max(col3) col3 from tb group by col1) b on a.col1 = b.col1 and a.col3 = b.col3 order by a.col1
--方法5
select a.* from tb a where 1 > (select count(*) from tb where col1 = a.col1 and col3 > a.col3 ) order by a.col1

drop table tb
/*
col1 col2 col3 col4 col5 col6
---------- ----------- ----------- ----------- ---------- ----------
C 15 6 231 A 你好啊
F 20 8 111 A 你真的好
Y 10 5 123 A 你好

(所影响的行数为 3 行)

col1 col2 col3 col4 col5 col6
---------- ----------- ----------- ----------- ---------- ----------
Y 10 5 123 A 你好
C 15 6 231 A 你好啊
F 20 8 111 A 你真的好

(所影响的行数为 3 行)

col1 col2 col3 col4 col5 col6
---------- ----------- ----------- ----------- ---------- ----------
C 15 6 231 A 你好啊
F 20 8 111 A 你真的好
Y 10 5 123 A 你好

(所影响的行数为 3 行)

col1 col2 col3 col4 col5 col6
---------- ----------- ----------- ----------- ---------- ----------
C 15 6 231 A 你好啊
F 20 8 111 A 你真的好
Y 10 5 123 A 你好

(所影响的行数为 3 行)

col1 col2 col3 col4 col5 col6
---------- ----------- ----------- ----------- ---------- ----------
C 15 6 231 A 你好啊
F 20 8 111 A 你真的好
Y 10 5 123 A 你好

(所影响的行数为 3 行)
*/
wzy_love_sly 2008-03-25
  • 打赏
  • 举报
回复
create table tb(col1 varchar(10),col2 int,col3 int,col4 int,col5 varchar(10),col6 varchar(10))
insert into tb select 'Y',10,5,123,'A','你好'
insert into tb select 'C',15,6,231,'A','你好啊'
insert into tb select 'C',15,5,321,'A','你好吗'
insert into tb select 'F',20,8,111,'A','你真的好'

select * from tb t where not exists(
select 1 from tb where col1=t.col1 and col3>t.col3
)


col1 col2 col3 col4 col5 col6
Y 10 5 123 A 你好
C 15 6 231 A 你好啊
F 20 8 111 A 你真的好
dawugui 2008-03-25
  • 打赏
  • 举报
回复
假设表T1数据:
col1 col2 col3 col4 col5 col6
Y 10 5 123 A 你好
C 15 6 231 A 你好啊
C 15 5 321 A 你好吗
F 20 8 111 A 你真的好

想得到以下结果,如果col1列值相同,取col3列值的最大值:
col1 col2 col3 col4 col5 col6
Y 10 5 123 A 你好
C 15 6 231 A 你好啊
F 20 8 111 A 你真的好
------------------------

select t.* from tb t where col3 = (select max(col3) from tb where col1 = t.col1) order by t.col1
xiaomeixiang 2008-03-25
  • 打赏
  • 举报
回复
select * from t1 a
where col3=(select max(col3) from t1 where col1=a.col1)
wzy_love_sly 2008-03-25
  • 打赏
  • 举报
回复
select * from t1 t where not exists(
select 1 from t1 where col1=t.col1 and col3>t.col3
)
pt1314917 2008-03-25
  • 打赏
  • 举报
回复

select * from t1 a where col3 = (select max(col3) from tb where col1 = a.col1)
--or:

select * from t1 a where not exists(select 1 from t1 where col1 = a.col1 and col3>a.col3)
--or:

select a.* from t1 a,(select col1,max(col3) col3 from t1 group by col1) b
where a.col1 = b.col1 and a.col3 = b.col3
--or:

select * from t1 a where
(select count(distinct con3) from t1 where col1 = a.col1 and col3 >= a.col3 )=1
wjpath23 2008-03-25
  • 打赏
  • 举报
回复
唉!!方法太多了.....[color=#FF00FF][/color]
jevin8011 2008-03-25
  • 打赏
  • 举报
回复

select a.* from Tb a inner join
(select Col1,Col2,Max(Col3) as c3 from Tb Group by Col1,Col2) b
on a.Col1 = b.Col1 and a.Col2 = b.Col2 and a.Col3 = b.C3
order by a.Col1,a.Col2,a.Col3
-狙击手- 2008-03-25
  • 打赏
  • 举报
回复
select * 
from tb t
where not exists(select 1 from tb where col1=t.col1 and col3>t.col3)
MySQL 教程MySQL 是流行的关系型数据库管理系统,在 WEB 应用方面 MySQL 是最好的 RDBMS(Relational Database Management System:关系数据库管理系统)应用软件之一。在本教程中,会让大家快速掌握 MySQL 的基本知识,并轻松使用 MySQL 数据库。什么是数据库?数据库(Database)是按照数据结构来组织、存储和管理数据的仓库。每个数据库都有一个或多个不同的 API 用于创建,访问,管理,搜索和复制所保存的数据。我们也可以将数据存储在文件中,但是在文件中读写数据速度相对较慢。所以,现在我们使用关系型数据库管理系统(RDBMS)来存储和管理大数据量。所谓的关系型数据库,是建立在关系模型基础上的数据库,借助于集合代数等数学概念和方法来处理数据库中的数据。RDBMS 即关系数据库管理系统(Relational Database Management System)的特点:1.数据以表格的形式出现2.每行为各种记录名称3.每列为记录名称所对应的数据域4.许多的行和列组成一张表单5.若干的表单组成databaseRDBMS 术语 在我们开始学习MySQL 数据库前,让我们先了解下RDBMS的一些术语:数据库: 数据库是一些关联表的集合。数据表: 表是数据的矩阵。在一个数据库中的表看起来像一个简单的电子表格。列: 一列(数据元素) 包含了相同类型的数据, 例如邮政编码的数据。行:一行(=元组,或记录)是一组相关的数据,例如一条用户订阅的数据。冗余:存储两倍数据,冗余降低了性能,但提高了数据的安全性。主键:主键是唯一的。一个数据表中只能包含一个主键。你可以使用主键来查询数据。外键:外键用于关联两个表。复合键:复合键(组合键)将多个列作为一个索引键,一般用于复合索引。索引:使用索引可快速访问数据库表中的特定信息。索引是对数据库表中一列或多列的进行排序的一种结构。类似于书籍的目录。参照完整性: 参照的完整性要求关系中不允许引用不存在的实体。与实体完整性是关系模型必须满足的完整性约束条件,目的是保证数据的一致性。MySQL 为关系型数据库(Relational Database Management System), 这种所谓的关系型可以理解为表格的概念, 一个关系型数据库由一个或数个表格组成, 如图所示的一个表格: 表头(header): 每一列的名称;列(col): 具有相同数据类型的数据的集合;行(row): 每一行用来描述某条记录的具体信息;(value): 行的具体信息, 每个必须与该列的数据类型相同;键(key): 键的在当前列中具有唯一性。MySQL数据库MySQL 是一个关系型数据库管理系统,由瑞典 MySQL AB 公司开发,目前属于 Oracle 公司。MySQL 是一种关联数据库管理系统,关联数据库将数据保存在不同的表中,而不是将所有数据放在一个大仓库内,这样就增加了速度并提高了灵活性。MySQL 是开源的,目前隶属于 Oracle 旗下产品。MySQL 支持大型的数据库。可以处理拥有上千万条记录的大型数据库。MySQL 使用标准的 SQL 数据语言形式。MySQL 可以运行于多个系统上,并且支持多种语言。这些编程语言包括 C、C++、Python、Java、Perl、PHP、Eiffel、Ruby 和 Tcl 等。MySQL 对PHP有很好的支持,PHP 是目前流行的 Web 开发语言。MySQL 支持大型数据库,支持 5000 万条记录的数据仓库,32 位系统表文件最大可支持 4GB,64 位系统支持最大的表文件为8TB。MySQL 是可以定制的,采用了 GPL 协议,你可以修改源码来开发自己的 MySQL 系统。Redis 教程REmote DIctionary Server(Redis) 是一个由 Salvatore Sanfilippo 写的 key-value 存储系统,是跨平台的非关系型数据库。Redis 是一个开源的使用 ANSI C 语言编写、遵守 BSD 协议、支持网络、可基于内存、分布式、可选持久性的键对(Key-Value)存储数据库,并提供多种语言的 API。Redis 通常被称为数据结构服务器,因为(value)可以是字符串(String)、哈希(Hash)、列表(list)、集合(sets)和有序集合(sorted sets)等类型。

27,579

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 应用实例
社区管理员
  • 应用实例社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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