Author |
Topic |
sgcool
Starting Member
5 Posts |
Posted - 2006-11-10 : 02:32:33
|
My data is like Below:id abc def---- --------- ------------------------------1 1.584795 2006-11-04 13:36:06.0 2 1.584795 2006-11-04 14:03:09.0 3 1.584795 2006-11-04 17:42:31.0 4 1.584795 2006-11-05 03:13:23.0 5 1.598802 2006-11-05 08:53:48.0 6 1.598802 2006-11-05 09:25:50.0 7 1.598802 2006-11-05 09:35:49.0 8 1.598802 2006-11-05 12:53:43.0 here columns id, abc, defi want only rows with id 4, 8. these are latest updated rows in to table based on date. Here column abc is having duplciate values with distinct date/time value.I appriciate for any help using sql queries. i tried, but am poor in SQL.can any body pls help me...Rgds,sgcool.-------------------------------------------------------------------------------- |
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2006-11-10 : 02:43:50
|
[code]create table #temp( id int, abc float, def datetime)insert into #tempselect 1, 1.584795, '2006-11-04 13:36:06.0' union allselect 2, 1.584795, '2006-11-04 14:03:09.0' union allselect 3, 1.584795, '2006-11-04 17:42:31.0' union allselect 4, 1.584795, '2006-11-05 03:13:23.0' union allselect 5, 1.598802, '2006-11-05 08:53:48.0' union allselect 6, 1.598802, '2006-11-05 09:25:50.0' union allselect 7, 1.598802, '2006-11-05 09:35:49.0' union allselect 8, 1.598802, '2006-11-05 12:53:43.0'select *from #temp twhere def = (select max(def) from #temp x where x.def >= dateadd(day, datediff(day, 0, t.def), 0) and x.def < dateadd(day, datediff(day, 0, t.def), 1) )[/code]Should be id 3 and 8 ? KH |
 |
|
sgcool
Starting Member
5 Posts |
Posted - 2006-11-10 : 03:49:05
|
If possible help me in Oracle please..here i need both abc,def columns as result.Sorry to troubling you..Rgds,sgcool.. |
 |
|
sgcool
Starting Member
5 Posts |
Posted - 2006-11-10 : 03:52:06
|
Hi KhTan,thanks for information. but iam in bit confusion as am doing in oracle...and i need only 4 and 8 rows as 4 is latest than 3 and 8 also latest for abc = '1.598802'my result should beid abc def---- ------ --------4 1.584795 2006-11-05 03:13:23.0 8 1.598802 2006-11-05 12:53:43.0 Rgds,prasad.CH |
 |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2006-11-10 : 04:03:52
|
Do you want latest per date or latest per id (which i imagine is an identity column)?Peter LarssonHelsingborg, Sweden |
 |
|
sgcool
Starting Member
5 Posts |
Posted - 2006-11-10 : 04:15:04
|
Hi Peter,thanks., i need latest by date not id.thanks.,Rgds,sgcool |
 |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2006-11-10 : 04:19:39
|
[code]create table #temp( id int, abc float, def datetime)insert into #tempselect 1, 1.584795, '2006-11-04 13:36:06.0' union allselect 2, 1.584795, '2006-11-04 14:03:09.0' union allselect 3, 1.584795, '2006-11-04 17:42:31.0' union allselect 4, 1.584795, '2006-11-05 03:13:23.0' union allselect 5, 1.598802, '2006-11-05 08:53:48.0' union allselect 6, 1.598802, '2006-11-05 09:25:50.0' union allselect 7, 1.598802, '2006-11-05 09:35:49.0' union allselect 8, 1.598802, '2006-11-05 12:53:43.0'select t.*from #temp tinner join ( select abc, max(def) md from #temp group by abc ) q on q.abc = t.abc and q.md = t.defdrop table #temp[/code]Peter LarssonHelsingborg, Sweden |
 |
|
sgcool
Starting Member
5 Posts |
Posted - 2006-11-10 : 04:57:15
|
Hi Peter,thanks for solution. but unfortunately, my data may get like1, 1.584795, '2006-11-04 13:36:06.0' 2, 1.584795, '2006-11-04 14:03:09.0' 3, 1.584795, '2006-11-04 17:42:31.0' 4, 1.584795, '2006-11-05 03:13:23.0' 5, 1.598802, '2006-11-05 08:53:48.0' 6, 1.598802, '2006-11-05 09:25:50.0' 7, 1.598802, '2006-11-05 09:35:49.0' 8, 1.598802, '2006-11-05 12:53:43.0' 9, 1.584795, '2006-11-05 12:53:49.0'in above case id 9 row have another value at diff time. so i cannot use group by clause of abc. my main aim is get distinct abc values at different and latest timings. bit tricky one..thanksRgds,sgcool |
 |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2006-11-10 : 05:11:22
|
This is not as per required in the original posting!!What you want is MAX (latest) id for every abc in sequence def?In your example above, you want rows 4 (1-3 omitted since not last in this sequence), 8 (5-7 omitted since not last in this sequnce) and 9?LEARN HOW TO EXPLAIN WHAT YOU REALLY WANT THE FIRST TIME!Peter LarssonHelsingborg, Sweden |
 |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2006-11-10 : 06:10:50
|
Try thiscreate table #temp( id int, abc float, def datetime)insert into #tempselect 1, 1.584795, '2006-11-04 13:36:06.0' union allselect 2, 1.584795, '2006-11-04 14:03:09.0' union allselect 3, 1.584795, '2006-11-04 17:42:31.0' union allselect 4, 1.584795, '2006-11-05 03:13:23.0' union allselect 5, 1.598802, '2006-11-05 08:53:48.0' union allselect 6, 1.598802, '2006-11-05 09:25:50.0' union allselect 7, 1.598802, '2006-11-05 09:35:49.0' union allselect 8, 1.598802, '2006-11-05 12:53:43.0' union allselect 9, 1.584795, '2006-11-05 12:53:49.0'select t1.*from #temp t1left join #temp t2 on t2.id -1 = t1.idwhere t2.id is null or t2.abc <> t1.abcdrop table #temp Peter LarssonHelsingborg, Sweden |
 |
|
|
|
|