SQL 分组后获取其中一个字段最大值的整条记录
--有id,name,createDate的一张表testTable
--根据name分组,获取每组中createDate最大的那条记录(整条)查询出来
----------------------------------------------
创建一张表,语句如下:
- CREATE TABLE [dbo].[testTable]
- (
- [id] [int] NOT NULL IDENTITY(1, 1),
- [name] [nchar] (10) COLLATE Chinese_PRC_CI_AS NULL,
- [counts] [int] NULL,
- [createDate] [datetime] NULL
- )
- GO
- -- Constraints and Indexes
- ALTER TABLE [dbo].[testTable] ADD CONSTRAINT [PK_testTable] PRIMARY KEY CLUSTERED ([id])
- GO
插入测试数据:
- insert into testTable(id,name,counts,createDate) values(1,'A ',20,'01 14 2011 10:52PM')
- insert into testTable(id,name,counts,createDate) values(2,'A ',10,'02 14 2011 10:52PM')
- insert into testTable(id,name,counts,createDate) values(3,'B ',20,'03 14 2011 10:52PM')
- insert into testTable(id,name,counts,createDate) values(4,'B ',40,'04 14 2011 10:52PM')
- insert into testTable(id,name,counts,createDate) values(5,'B ',10,'05 14 2011 10:52PM')
- insert into testTable(id,name,counts,createDate) values(6,'C ',20,'06 14 2011 10:52PM')
- insert into testTable(id,name,counts,createDate) values(7,'C ',40,'07 14 2011 10:52PM')

查询SQL语句:
- select * from (
- select id,name,counts,createDate,row_number() over(partition by name order by createDate desc) rn
- from testTable
- ) t where t.rn <=1
结果如下:
SQL 分组后获取其中一个字段最大值的整条记录的更多相关文章
- SQL分组后获取其中一个字段最大值的整条记录
SELECT * FROM( SELECT id,name,counts,createDate,row_number() OVER(partition BY name ORDER BY createD ...
- SQL 分组后获取每组中最大值
场景:sql server 2008 drop table ID CREATE TABLE ID ( id ,) not null, code int , D date, PRIMARY KEY (i ...
- SQL 分组后,获取每组中的最大值对应的数据
select gr,num,dt,(select bys from test where gr=b.gr and dt=b.dt) bys from ( select gr,count(0) num, ...
- sql 分组后查询出排序字段
select row_number() over(partition by CODE order by SEQUENCE) as RowIndex from Table 注:根据表的CODE 字 ...
- 在论坛中出现的比较难的sql问题:16(取一个字段中的数字)
原文:在论坛中出现的比较难的sql问题:16(取一个字段中的数字) 所以,觉得有必要记录下来,这样以后再次碰到这类问题,也能从中获取解答的思路. 问题:取一个字段中的数字http://bbs.csdn ...
- SQL排除重复结果只取字段最大值
如何用SQL排除重复结果只取字段最大值的记录?要求得到的结果(即是PID相同的记录只取ID值最大的那一条). select * from [Sheet1$] a from [Sheet1$] wher ...
- sql 分组后显示每组的前几条记录
sql 分组后显示每组的前几条记录 如 表中记录是 code serialno A1 1 ...
- sql 分组后按时间降序排列再取出每组的第一条记录
原文:sql 分组后按时间降序排列再取出每组的第一条记录 竞价记录表: Aid 为竞拍车辆ID,uid为参与竞价人员ID,BidTime为参与竞拍时间 查询出表中某人参与的所有车辆的最新的一条的竞价记 ...
- SQL语句 删除表user 中字段name 内容重复的记录,
public class T01 { public static void main(String[] args) { int j=4; j=j+=j-=j*=j; System.out.printl ...
随机推荐
- TortoiseGit与GitHub项目关联设置
一.常规克隆GitHub上的项目: 1.在本地硬盘上放置项目的地方上[右键]->[Git 克隆]->在[url地址]上输入https的GitHub的链接,然后就是等待完成,之后即可完成拉取 ...
- eclipse+SVN文件只显示版本号,不显示时间和作者解决办法
SVN默认是显示提交次数的 改成这样 就可以了...
- shutil模块
shutil模块 提供了大量的文件的高级操作,特别针对文件拷贝和删除,主要功能为目录和文件操作以及压缩操作 常用方法 shutil.copyfile(src, dst) 复制文件内容(不包含元数据)从 ...
- LaTeX test
\begin{equation} x^2+1=2 \end{equation} \begin{equation} x^2+y=3 \end{equation} $\dfrac{2\pi}{N}$
- xpath中/和//的差别
xpath中 "/"是在子节点中查找,"//"是在所有子节点中查找,包括子节点的子节点. example: leve1/leve2:得到文本leve2 leve ...
- java函数substring()
String str; str=str.substring(int beginIndex);截取掉str从首字母起长度为beginIndex的字符串,将剩余字符串赋值给str: str=str.sub ...
- ubuntu14.04安装GoldenDict
1. 软件商店搜索goldendict安装或者$ sudo apt-get install goldendict 2. 配置网页翻译源 编辑-->词典--->网站选项卡,点击添加 以下是有 ...
- gnuplot使用2
设置图中连线的颜色.宽度.连线样式等 set style line 每个显示终端都有默认的线类型和点类型集合,可以通过在命令行输入: test查看,如下图显示了在wxt终端模式下默认的线的集合和点的集 ...
- CSS3自适配手机屏幕
@media only screen and (max-width:350px){ .img{ width: 80px; height:70px; background-image: url(./im ...
- 10月14日上午PHP环境搭建
第一步:安装wampserver2.5-Apache-2.4.9-Mysql-5.6.17-php5.5.12-64b文件,安装过程中可能会遇到问题,把遇到的问题代码复制粘贴到360人工服务,查找方案 ...