首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > SQL Server >

sql group by 查询解决思路

2012-04-20 
sql group by 查询declare @table table(id int ,[type] int ,date datetime)insert into @tableselect 1,

sql group by 查询


declare @table table(id int ,[type] int ,date datetime)

insert into @table 
select 1,1,'2009-11-11 00:00:00' union all 
select 2,1,'2010-11-11 00:00:00' union all 
select 3,1,'2011-11-11 00:00:00' union all 
select 4,2,'2009-11-11 00:00:00' union all 
select 5,2,'2011-11-11 00:00:00' 


select id,type,max(date)as date from @table group by id,type


求改写得到的数据是
312011-11-11 00:00:00.000
522011-11-11 00:00:00.000

[解决办法]
select id ,[type] ,date from(
select *,row_number()over(partition by type id order by getdate) as num)a
where num=1
[解决办法]

SQL code
更改一下select id ,[type] ,date from(select *,row_number()over(partition by type id order by id desc) as num)awhere num=1
[解决办法]
SQL code

更改一下
select id ,[type] ,date from( 
select *,row_number()over(partition by type order by id desc) as num from @table
)a where num=1


[解决办法]
SQL code
declare @table table(id int ,[type] int ,date datetime)insert into @table select 1,1,'2009-11-11 00:00:00' union all select 2,1,'2010-11-11 00:00:00' union all select 3,1,'2011-11-11 00:00:00' union all select 4,2,'2009-11-11 00:00:00' union all select 5,2,'2011-11-11 00:00:00' select * from @table twhere id=(select max(id) from @table where [type]=t.[type])order by 1/*id          type        date----------- ----------- -----------------------3           1           2011-11-11 00:00:00.0005           2           2011-11-11 00:00:00.000*/
[解决办法]
SQL code
declare @table table(id int ,[type] int ,date datetime)insert into @table  select 1,1,'2009-11-11 00:00:00' union all  select 2,1,'2010-11-11 00:00:00' union all  select 3,1,'2011-11-11 00:00:00' union all  select 4,2,'2009-11-11 00:00:00' union all  select 5,2,'2011-11-11 00:00:00'  select t.* from @table t where id = (select max(id) from @table where type = t.type ) order by t.type/*id          type        date                                                   ----------- ----------- ------------------------------------------------------ 3           1           2011-11-11 00:00:00.0005           2           2011-11-11 00:00:00.000(所影响的行数为 2 行)*/select t.* from @table t where not exists (select 1 from @table where type = t.type and id > t.id) order by t.type/*id          type        date                                                   ----------- ----------- ------------------------------------------------------ 3           1           2011-11-11 00:00:00.0005           2           2011-11-11 00:00:00.000(所影响的行数为 2 行)*/
[解决办法]
SQL code
declare @table table(id int ,[type] int ,date datetime)insert into @table select 1,1,'2009-11-11 00:00:00' union all select 2,1,'2010-11-11 00:00:00' union all select 3,1,'2011-11-11 00:00:00' union all select 4,2,'2009-11-11 00:00:00' union all select 5,2,'2011-11-11 00:00:00' select id ,[type] ,date from( select *,row_number()over(partition by [type] order by id desc) as num from @table)a where num=1--select * from @table a where id=(select MAX(id) from @table b where a.[type]=b.[type])order by id/*id    type    date3    1    2011-11-11 00:00:00.0005    2    2011-11-11 00:00:00.000*/
[解决办法]

SQL code
declare @table table(id int ,[type] int ,date datetime)insert into @table  select 1,1,'2009-11-11 00:00:00' union all  select 2,1,'2010-11-11 00:00:00' union all  select 3,1,'2011-11-11 00:00:00' union all  select 4,2,'2009-11-11 00:00:00' union all  select 5,2,'2011-11-11 00:00:00'  --以时间算select t.* from @table t where date = (select max(date) from @table where type = t.type ) order by t.type/*id          type        date                                                   ----------- ----------- ------------------------------------------------------ 3           1           2011-11-11 00:00:00.0005           2           2011-11-11 00:00:00.000(所影响的行数为 2 行)*/--以时间算select t.* from @table t where not exists (select 1 from @table where type = t.type and date > t.date) order by t.type/*id          type        date                                                   ----------- ----------- ------------------------------------------------------ 3           1           2011-11-11 00:00:00.0005           2           2011-11-11 00:00:00.000(所影响的行数为 2 行)*/--以ID算select t.* from @table t where id = (select max(id) from @table where type = t.type ) order by t.type/*id          type        date                                                   ----------- ----------- ------------------------------------------------------ 3           1           2011-11-11 00:00:00.0005           2           2011-11-11 00:00:00.000(所影响的行数为 2 行)*/--以ID算select t.* from @table t where not exists (select 1 from @table where type = t.type and id > t.id) order by t.type/*id          type        date                                                   ----------- ----------- ------------------------------------------------------ 3           1           2011-11-11 00:00:00.0005           2           2011-11-11 00:00:00.000(所影响的行数为 2 行)*/
[解决办法]
探讨

时间大小不一定跟id大小一样 有可能是最开始的那条记录最大


引用:
SQL code


declare @table table(id int ,[type] int ,date datetime)

insert into @table
select 1,1,'2009-11-11 00:00:00' union all
se……

[解决办法]
SQL code
declare @table table(id int ,[type] int ,date datetime)insert into @table  select 1,1,'2009-11-11 00:00:00' union all  select 2,1,'2010-11-11 00:00:00' union all  select 3,1,'2011-11-11 00:00:00' union all  select 4,2,'2009-11-11 00:00:00' union all  select 5,2,'2011-11-11 00:00:00'  --以时间算select t.* from @table t where date = (select max(date) from @table where type = t.type ) order by t.type/*id          type        date                                                   ----------- ----------- ------------------------------------------------------ 3           1           2011-11-11 00:00:00.0005           2           2011-11-11 00:00:00.000(所影响的行数为 2 行)*/--以时间算select t.* from @table t where not exists (select 1 from @table where type = t.type and date > t.date) order by t.type/*id          type        date                                                   ----------- ----------- ------------------------------------------------------ 3           1           2011-11-11 00:00:00.0005           2           2011-11-11 00:00:00.000(所影响的行数为 2 行)*/--以ID算select t.* from @table t where id = (select max(id) from @table where type = t.type ) order by t.type/*id          type        date                                                   ----------- ----------- ------------------------------------------------------ 3           1           2011-11-11 00:00:00.0005           2           2011-11-11 00:00:00.000(所影响的行数为 2 行)*/--以ID算select t.* from @table t where not exists (select 1 from @table where type = t.type and id > t.id) order by t.type/*id          type        date                                                   ----------- ----------- ------------------------------------------------------ 3           1           2011-11-11 00:00:00.0005           2           2011-11-11 00:00:00.000(所影响的行数为 2 行)*/--同时按照时间+ID算,即如果最大时间相同,则取最大的IDselect t.* from @table t where not exists (select 1 from @table where type = t.type and (date > t.date or (date = t.date and id > t.id))) order by t.type/*id          type        date                                                   ----------- ----------- ------------------------------------------------------ 3           1           2011-11-11 00:00:00.0005           2           2011-11-11 00:00:00.000(所影响的行数为 2 行)*/ 


[解决办法]
我的两种方法都可以实现啊,他们的也都是那个意思

热点排行