university picture groups
北京大学光华管理学院 01-1.jpg 1
北京大学光华管理学院 01-2.jpg 1
北京大学光华管理学院 01-3.jpg 1
北京大学光华管理学院 01-4.jpg 1
清华大学经济管理学院 02-1.jpg 2
清华大学经济管理学院 02-2.jpg 2
清华大学经济管理学院 02-3.jpg 2
复旦大学管理学院 03-1.jpg 3
复旦大学管理学院 03-2.jpg 3
复旦大学管理学院 03-3.jpg 3
想通过一sql语句,得到这样的结果
university picture groups
北京大学光华管理学院 01-1.jpg 1
清华大学经济管理学院 02-1.jpg 2
复旦大学管理学院 03-1.jpg 3也就是说按groups的递增,依次取出每个group中的第一条记录。
对sql语句较熟的朋友,望赐教!
北京大学光华管理学院 01-1.jpg 1
北京大学光华管理学院 01-2.jpg 1
北京大学光华管理学院 01-3.jpg 1
北京大学光华管理学院 01-4.jpg 1
清华大学经济管理学院 02-1.jpg 2
清华大学经济管理学院 02-2.jpg 2
清华大学经济管理学院 02-3.jpg 2
复旦大学管理学院 03-1.jpg 3
复旦大学管理学院 03-2.jpg 3
复旦大学管理学院 03-3.jpg 3
想通过一sql语句,得到这样的结果
university picture groups
北京大学光华管理学院 01-1.jpg 1
清华大学经济管理学院 02-1.jpg 2
复旦大学管理学院 03-1.jpg 3也就是说按groups的递增,依次取出每个group中的第一条记录。
对sql语句较熟的朋友,望赐教!
北京大学光华管理学院 01-1.jpg 1
北京大学光华管理学院 01-2.jpg 1
北京大学光华管理学院 01-3.jpg 1
北京大学光华管理学院 01-4.jpg 1
清华大学经济管理学院 02-1.jpg 2
清华大学经济管理学院 02-2.jpg 2
清华大学经济管理学院 02-3.jpg 2
复旦大学管理学院 03-1.jpg 3
复旦大学管理学院 03-2.jpg 3
复旦大学管理学院 03-3.jpg 3
想通过一sql语句,得到这样的结果
university picture groups
北京大学光华管理学院 01-1.jpg 1
清华大学经济管理学院 02-1.jpg 2
复旦大学管理学院 03-1.jpg 3
也就是说,随着groups的递增,依次取出每个group中的第一条记录。
对sql语句较熟的朋友,望赐教!
insert into table1
select '北京大学光华管理学院 ','01-1.jpg', 1
union all
select '北京大学光华管理学院 ','01-2.jpg', 1
union all
select '北京大学光华管理学院 ','01-3.jpg', 1
union all
select '北京大学光华管理学院 ','01-4.jpg', 1
union all
select '清华大学经济管理学院 ','02-1.jpg', 2
union all
select '清华大学经济管理学院 ','02-2.jpg', 2
union all
select '清华大学经济管理学院 ','02-3.jpg', 2
union all
select '复旦大学管理学院 ','03-1.jpg', 3
union all
select '复旦大学管理学院 ','03-2.jpg', 3
union all
select '复旦大学管理学院 ','03-3.jpg', 3
--------
select * ,identity(int,1,1) as ID into #t from table1--利用中间表
--------
--Try It
select A.university,A.Picture,A.groups from #t A
where not exists
(select * from #t where university=A.university and groups=A.groups and ID<A.id)
---
Drop table #t
Drop table Table1
insert into tb
select '北京大学光华管理学院 ','01-1.jpg', 1
union all
select '北京大学光华管理学院 ','01-2.jpg', 1
union all
select '北京大学光华管理学院 ','01-3.jpg', 1
union all
select '北京大学光华管理学院 ','01-4.jpg', 1
union all
select '清华大学经济管理学院 ','02-1.jpg', 2
union all
select '清华大学经济管理学院 ','02-2.jpg', 2
union all
select '清华大学经济管理学院 ','02-3.jpg', 2
union all
select '复旦大学管理学院 ','03-1.jpg', 3
union all
select '复旦大学管理学院 ','03-2.jpg', 3
union all
select '复旦大学管理学院 ','03-3.jpg', 3 select *
from tb tm
where not exists(select 1 from tb where tm.groups = tb.groups and cast(substring(tm.picture,charindex('.',tm.picture)-1,1) as int)>cast(substring(tb.picture,charindex('.',tb.picture)-1,1) as int))drop table tb
/*
university picture groups
-------------------------------------------------- -------------------- -----------
北京大学光华管理学院 01-1.jpg 1
清华大学经济管理学院 02-1.jpg 2
复旦大学管理学院 03-1.jpg 3(所影响的行数为 3 行)
*/
university,min(picture) as picture,groups
from
table1
group by
university,groups
order by
groups
from table1
where picture like '%#_01.jpg' ESCAPE '#'
where picture=(select min(picture) from tablename where groups=a.groups)
declare @t table(university varchar(8000), picture varchar(8000), groups int)
insert into @t
select '北京大学光华管理学院', '01-1.jpg', 1 union
select '北京大学光华管理学院', '01-2.jpg', 1 union
select '北京大学光华管理学院', '01-3.jpg', 1 union
select '北京大学光华管理学院', '01-4.jpg', 1 union
select '清华大学经济管理学院', '02-1.jpg', 2 union
select '清华大学经济管理学院', '02-2.jpg', 2 union
select '清华大学经济管理学院', '02-3.jpg', 2 union
select '复旦大学管理学院', '03-1.jpg', 3 union
select '复旦大学管理学院', '03-2.jpg', 3 union
select '复旦大学管理学院', '03-3.jpg', 3select university , min(picture) as picture, groups
from @t
group by university ,groups
order by groups
picture=(select min(picture) from tablename where groups=a.groups)
没有grouy by 也可以分组?
还有啊 picture中的等于号只能查一条语句,那个怎么能查出三条呢?
请高人指点~
第一条记录 北京大学光华管理学院 01-1.jpg 1
首先拿出第一条记录,然后
select min(picture) from tablename where groups=a.groups
这句话是把groups=1的所有都找出来在找出最小的picture值也就是第一条
然后picture=01-1.jpg
下面的第二条
北京大学光华管理学院 01-2.jpg 1
再如上比较再得出picture=01-1.jpg
那不是有多少条记录就要返回都少吗?
迷茫中~~
select min(picture) from tablename where groups=a.groups
中的groups=a.groups啊因为都是1
所以会不会仍然选出各最小的来呢
不能理解它的执行过程