SQL 分组后获取其中一个字段最大值的整条记录

--有id,name,createDate的一张表testTable
--根据name分组,获取每组中createDate最大的那条记录(整条)查询出来
----------------------------------------------

创建一张表,语句如下:

[sql] view plaincopyprint?

  1. CREATE TABLE [dbo].[testTable]

  2. (

  3. [id] [int] NOT NULL IDENTITY(1, 1),

  4. [name] [nchar] (10) COLLATE Chinese_PRC_CI_AS NULL,

  5. [counts] [int] NULL,

  6. [createDate] [datetime] NULL

  7. )

  8. GO

  9. -- Constraints and Indexes

  10. ALTER TABLE [dbo].[testTable] ADD CONSTRAINT [PK_testTable] PRIMARY KEY CLUSTERED ([id])

  11. GO

插入测试数据:

[sql] view plaincopyprint?

  1. insert into testTable(id,name,counts,createDate) values(1,‘A         ‘,20,‘01 14 2011 10:52PM‘)

  2. insert into testTable(id,name,counts,createDate) values(2,‘A         ‘,10,‘02 14 2011 10:52PM‘)

  3. insert into testTable(id,name,counts,createDate) values(3,‘B         ‘,20,‘03 14 2011 10:52PM‘)

  4. insert into testTable(id,name,counts,createDate) values(4,‘B         ‘,40,‘04 14 2011 10:52PM‘)

  5. insert into testTable(id,name,counts,createDate) values(5,‘B         ‘,10,‘05 14 2011 10:52PM‘)

  6. insert into testTable(id,name,counts,createDate) values(6,‘C         ‘,20,‘06 14 2011 10:52PM‘)

  7. insert into testTable(id,name,counts,createDate) values(7,‘C         ‘,40,‘07 14 2011 10:52PM‘)

查询SQL语句:

[sql] view plaincopyprint?

  1. select * from (

  2. select id,name,counts,createDate,row_number() over(partition by name order by createDate desc) rn

  3. from testTable

  4. ) t where t.rn <=1

结果如下:

时间: 2024-11-07 05:06:18

SQL 分组后获取其中一个字段最大值的整条记录的相关文章

sql 分组后 组内排名

语法:ROW_NUMBER() OVER(PARTITION BY COLUMN ORDER BY COLUMN) 简单的说row_number()从1开始,为每一条分组记录返回一个数字,这里的ROW_NUMBER() OVER (ORDER BY xlh DESC) 是先把xlh列降序,再为降序以后的没条xlh记录返回一个序号. 示例: xlh           row_num 1700              1 1500              2 1085             

SQL 分组后拼接字符串

with t as( select 'Charles' parent, 'William' child union select 'Charles', 'Harry' union select 'Anne', 'Peter' union select 'Anne', 'Zara' union select 'Andrew', 'Beatrice' union select 'Andrew', 'Eugenie' ) SELECT parent, STUFF( ( SELECT ','+ chil

SQL Server分组查询某最大值的整条数据(包含linq写法)

想实现如下效果,就是分组后时间最大的那一条数据: 1.SQL SELECT * FROM ( SELECT * , ROW_NUMBER() OVER ( PARTITION BY RIP_GUID ORDER BY RU_CreatedTime DESC ) rn FROM RIP_FlowInfo ) t WHERE t.rn <= 1; 2.LINQ var groupQuery = from t in query group t by t.RIP_Guid into g select n

SQL server插入数据后,获取自增长字段的值

? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 insert into Tb_People(uname,era,amount) values( '兆周','老年','10000')   select @@identity   --当运行完插入语句后,执行select   @@identity就可得到自动生成的id   --如果是sql server 最好用select SCOPE_IDENTITY() as

oracle通过一个字段分组,取另一个字段的最大值

select * from bdcdj.lqentry1 a  where 顺序号 in (select max(顺序号) from bdcdj.lqentry1 b WHERE b.archival_code IS NOT NULL  group by archival_code): 通过archival_code分组  ,取顺序号的最大值. 原文地址:https://www.cnblogs.com/wangleping/p/11712175.html

mysql分组后获取每个组排序后的第一条数据(整行)

https://blog.csdn.net/persistencegoing/article/details/92764058 All rights reserved.No part of this article may be reproduced or distributed by any means,or stored in a database or retrieval system,without the prior written permission of persistenceG

django获取某一个字段的列表,values/values_list/flat

class Building(models.Model): corporation = models.ForeignKey('company.Corporation', verbose_name=u'学校', related_name='buildings') number = models.CharField(u'楼栋编号', max_length=10, unique=True, db_index=True) create_time = models.DateTimeField(u'创建时间

SQl 分组后按照某一列拼接字符串

/* 分组之后拼接字符串 */ ;with t as( SELECT WorkflowId,Remark FROM dbo.OperatorAutomationProcess GROUP BY WorkflowId,Remark ) , H AS( SELECT WorkflowId, -- 分组的主键 STUFF( ( SELECT '_'+ Remark -- 要拼接的列 FROM t a WHERE b.WorkflowId = a.WorkflowId FOR XML PATH('')

sql server 2008获取表的字段注释

1 SELECT 2 表名=case when a.colorder=1 then d.name else '' end, 3 表说明=case when a.colorder=1 then isnull(f.value,'') else '' end, 4 字段序号=a.colorder, 5 字段名=a.name, 6 标识=case when COLUMNPROPERTY( a.id,a.name,'IsIdentity')=1 then '√'else '' end, 7 主键=case