| Author |
Topic |
|
krishna_yess
Yak Posting Veteran
81 Posts |
Posted - 2009-07-02 : 06:20:25
|
this is my table (extract)Project PoweUptime dateAddedAstro DMR 57.789999999999999 2009-01-01 00:00:00.000Astro DMR 5.0 2009-06-23 11:41:01.000 if i run this queryselect project, poweuptime,dateadded from summary project where project='astro dmr' i am gettingProject PoweUptime dateAdded Astro DMR 57.789999999999999 2009-01-01 00:00:00.000Astro DMR 5.0 2009-06-23 11:41:01.000 if i rum this queryselect project, PoweUpTime, dateadded from summary where project='astro dmr' and dateadded in ( select max(dateadded) from summary group by project) order by project i am still getting same thingProject PoweUptime dateAddedAstro DMR 57.789999999999999 2009-01-01 00:00:00.000Astro DMR 5.0 2009-06-23 11:41:01.000 actually i am expecting:Project PoweUptime dateAddedAstro DMR 5.0 2009-06-23 11:41:01.000 what i need is for each project i need ONE row which is added RECENTLYwhat is the problem with the second query |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2009-07-02 : 06:26:05
|
[code]select project, PoweUpTime, dateadded from summary where project = 'astro dmr' and dateadded in ( select max(dateadded) from summary group by project) order by project[/code]you used group by project in the sub query. So you are getting the max dateadded by project KH[spoiler]Time is always against us[/spoiler] |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2009-07-02 : 06:28:05
|
this will be better. The max(dateadded) will be depending on the filtered project value in your where clause.select project, PoweUpTime, dateadded from summary swhere project = 'astro dmr' and dateadded in ( select max(dateadded) from summary x where x.proejct = s.project)order by project KH[spoiler]Time is always against us[/spoiler] |
 |
|
|
krishna_yess
Yak Posting Veteran
81 Posts |
Posted - 2009-07-02 : 06:36:14
|
In fact there are other values in my tablethis is my tableProject PoweUpTime DateAdded---------- ------------------ ----------------a 128 1/1/2009Astro DMR 5 6/23/2009Astro DMR 57.79 1/1/2009b 56.96 1/1/2009SDFG 82.34333333 6/10/2009GSDF 28.43 1/1/2009G 0 1/1/2009SDF 0 1/1/2009G 43.83 1/1/2009SDF 54.17 1/1/2009G 59 1/1/2009SDF 0 5/22/2009G 21.05 1/1/2009SDF 33.58 1/1/2009G 35.17 1/1/2009SDFGSDFG 0 1/1/2009FFF 23.33 1/1/2009HHH 24.6 1/1/2009JJERT 141 1/1/2009F 141 1/1/2009E 30 1/1/2009RTYERT 0 5/25/2009ERTYERT 0 5/29/2009ERTYERT 82 6/8/2009ET 105.66 1/1/2009ERTYERT 72.4 1/1/2009ERTYERT 64.06 1/1/2009ETRY 58.4 1/1/2009IIII 96.6 1/1/2009FASD 65.64 1/1/2009GDFG 37.9 1/1/2009DFG 23.8 1/1/2009SDF 6 6/10/2009G 83.33 1/1/2009DFG 0 1/1/2009DFG 0 1/1/2009FG 293.4 1/1/2009 |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2009-07-02 : 06:37:53
|
so just use the last query i posted. KH[spoiler]Time is always against us[/spoiler] |
 |
|
|
krishna_yess
Yak Posting Veteran
81 Posts |
Posted - 2009-07-02 : 06:45:59
|
in both the cases it will return ONLY 1 row Astro DMR 5.0 2009-06-23 11:41:01.000but for each project i want ONE such rowmy query was right,select project, PoweUpTime, dateadded from summary where project='astro dmr' and dateadded in ( select max(dateadded) from summary group by project) order by project since there are too many cuplicate dates -1/1/2009 - it will return almost all rows |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2009-07-02 : 06:58:51
|
[code]SELECT Project, PowerUpTime, DateAddedFROM ( SELECT Project, PowerUpTime, DateAdded, ROW_NUMBER() OVER (PARTITION BY Project ORDER BY DateAdded DESC) AS recID ) AS dWHERE recID = 1[/code] Microsoft SQL Server MVPN 56°04'39.26"E 12°55'05.63" |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2009-07-02 : 07:32:27
|
use the query i posted on 07/02/2009 : 06:26:05 KH[spoiler]Time is always against us[/spoiler] |
 |
|
|
Lamprey
Master Smack Fu Yak Hacker
4614 Posts |
Posted - 2009-07-02 : 11:56:34
|
another wayselect project, PoweUpTime, dateadded from summary sINNER JOIN( select Project, MAX(dateadded) AS DateAdded from summary GROUP BY Project) AS T S.proejct = T.project AND S.DateAdded = T.DateAddedwhere project = 'astro dmr' order by project |
 |
|
|
Lamprey
Master Smack Fu Yak Hacker
4614 Posts |
Posted - 2009-07-02 : 12:03:41
|
quote: Originally posted by krishna_yess my query was right,select project, PoweUpTime, dateadded from summary where project='astro dmr' and dateadded in ( select max(dateadded) from summary group by project) order by project since there are too many cuplicate dates -1/1/2009 - it will return almost all rows
I'm not sure about right.. Syntactically correct, yes. The reason it is not getting the results you want is because your IN clause is getting ALL the max dates by project. So, as you discovered, a lot of projects have that same date. In order to make sure you get the correct row you need to look at the MAX date by project, not all projects. So, you can use the suggestion made by Peso or myself. Khtan's query will also work, but only for a single Project. Which is what you initially indicated you wanted. |
 |
|
|
|