| Author |
Topic |
|
rcr69er
Constraint Violating Yak Guru
327 Posts |
Posted - 2009-01-27 : 06:56:07
|
| Hi GuysI have a table similar to the following:Week_Id, DayKey 200727, 20070702200727, 20070703200727, 20070704200727, 20070705200727, 20070706200727, 20070707200727, 20070708200728, 20070709200728, 20070710200728, 20070711200728, 20070712200728, 20070713200728, 20070714200728, 20070715I am trying to find the minimum DayKey value for each Week_Id , so the desired results would look like the following:Week_Id, DayKey 200727, 20070702200728, 20070709Is this possible to do?Thanking you in advance! |
|
|
Jai Krishna
Constraint Violating Yak Guru
333 Posts |
Posted - 2009-01-27 : 07:00:23
|
| select week_id,min(daykey) from urtable group by week_idJai Krishna |
 |
|
|
Nageswar9
Aged Yak Warrior
600 Posts |
Posted - 2009-01-27 : 07:01:03
|
| select * from ( select *,row_number() over ( partition by Week_Id order by DayKey ) as rn from table) as twhere t.rn = 1 |
 |
|
|
rcr69er
Constraint Violating Yak Guru
327 Posts |
Posted - 2009-01-27 : 07:20:31
|
| HiThanks for that.Sorry I forgot to mention that there other columns such as the day name, eg:Week_Id, DayKey, DayName 200727, 20070702, Mon200727, 20070703, Tue200727, 20070704, Wed200727, 20070705, Thur200727, 20070706, Fri200727, 20070707, Sat200727, 20070708, Sun200728, 20070709, Mon200728, 20070710, Tue200728, 20070711, Wed200728, 20070712, Thur200728, 20070713, Fri200728, 20070714, Sat200728, 20070715, SunThe desired result would then be:Week_Id, DayKey, DayName 200727, 20070702, Mon200728, 20070709, MonIs this possible to do?Thanks |
 |
|
|
raky
Aged Yak Warrior
767 Posts |
Posted - 2009-01-27 : 07:25:48
|
| try nageswar 9 solution |
 |
|
|
raky
Aged Yak Warrior
767 Posts |
Posted - 2009-01-27 : 07:33:50
|
| or try thisdeclare @temp table ( Week_Id bigint, DayKey bigint,DayName varchar(40))insert into @tempselect 200727, 20070702, 'Mon' union allselect 200727, 20070703, 'Tue' union allselect 200727, 20070704, 'Wed' union allselect 200727, 20070705, 'Thur' union allselect 200727, 20070706, 'Fri' union allselect 200727, 20070707, 'Sat' union allselect 200727, 20070708, 'Sun' union allselect 200728, 20070709, 'Mon' union allselect 200728, 20070710, 'Tue' union allselect 200728, 20070711, 'Wed' union allselect 200728, 20070712, 'Thur' union allselect 200728, 20070713, 'Fri' union allselect 200728, 20070714, 'Sat' union allselect 200728, 20070715, 'Sun'select t1.week_id,t2.dk as Daykey,t1.dayname from @temp t1inner join ( select week_id,min(daykey) as dk from @temp group by week_id ) t2 on t2.week_id = t1.week_id and t2.dk = t1.daykey |
 |
|
|
Jai Krishna
Constraint Violating Yak Guru
333 Posts |
Posted - 2009-01-27 : 07:46:20
|
| select b.* from (select week_id,min(daykey) as mindate from urtable group by week_id) across apply(select week_id,daykey,dayname from urtable where week_id = a.week_id and daykey = a.mindate) bJai Krishna |
 |
|
|
Nageswar9
Aged Yak Warrior
600 Posts |
Posted - 2009-01-27 : 08:06:02
|
| [code]declare @temp table ( Week_Id int, DayKey int,dayname varchar(32))insert into @tempselect 200727, 20070702, 'Mon' union allselect 200727, 20070703, 'Tue' union allselect 200727, 20070704, 'Wed' union allselect 200727, 20070705, 'Thur' union allselect 200727, 20070706, 'Fri' union allselect 200727, 20070707, 'Sat' union allselect 200727, 20070708, 'Sun' union allselect 200728, 20070709, 'Mon' union allselect 200728, 20070710, 'Tue' union allselect 200728, 20070711, 'Wed' union all select 200728, 20070712, 'Thur' union allselect 200728, 20070713, 'Fri' union allselect 200728, 20070714, 'Sat' union allselect 200728, 20070715, 'Sun'select Week_Id ,DayKey,DayNamefrom ( select *,row_number() over ( partition by Week_Id order by DayKey ) as rn from @temp) as twhere t.rn = 1 [/code] |
 |
|
|
rcr69er
Constraint Violating Yak Guru
327 Posts |
Posted - 2009-01-27 : 08:47:08
|
| Thanks Guys for all your help!!! |
 |
|
|
raky
Aged Yak Warrior
767 Posts |
Posted - 2009-01-27 : 08:50:26
|
| You are Welcome... |
 |
|
|
|
|
|