Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
Author |
Topic |
Anthem
Starting Member
5 Posts |
Posted - 2013-10-23 : 16:53:36
|
Hi, hope someone can help. I have a query running without issue on around 100,000 results however i have a couple where my MAX aggregate is returning two results for a MAX(date time) and i cannot figure out why. I have paired down the query to try and work it out but still don't know.my query now looks like this (i have remove most columns)SELECT STATUS, RunOn, sort, T_IDFROM dbo.ResultsWHERE (RunOn IN (SELECT MAX(RunOn) FROM dbo.Results group BY T_ID) AND T_ID = 21405) ORDER BY sort I added the T_ID = 21405 to restrict the results to the problematic results, if i set T_ID to any other test result i get one value based on the latest RunOn, but for some reason with this T_ID i get two values, the latest one and one slightly older... i am lost as to why, please help. The date time looks ok, i just can't work out why the latest and one older result are returned !The original qury was grouping by more items but i removed those to try and work out what is going one, as mentioned for 99.9% of results it works perfectly, but for this one i don't know.Thanks a lot ! |
|
bitsmed
Aged Yak Warrior
545 Posts |
Posted - 2013-10-23 : 17:16:54
|
try thisselect a.status ,a.runon ,a.sort ,a.t_id from dbo.results as a inner join (select t_id ,max(runon) as runon from dbo.results group by t_id ) as b on b.t_id=a.t_id and b.runon=a.runon where a.t_id=21405 order by a.sort |
|
|
Anthem
Starting Member
5 Posts |
Posted - 2013-10-23 : 17:22:22
|
thanks a lot , why would my version not work ?, could you explain what are you doing in the above example ?, i can't fathom why it works 99% of the time for a simple MAX(Datetime) Many thanks |
|
|
bitsmed
Aged Yak Warrior
545 Posts |
Posted - 2013-10-23 : 17:35:11
|
quote: Originally posted by Anthem thanks a lot , why would my version not work ?, could you explain what are you doing in the above example ?, i can't fathom why it works 99% of the time for a simple MAX(Datetime) Many thanks
Your inner select returns max runid for each t_id. Now, t_id 21405 must have several runid's matching the max runid's returned, and this is why you get more than one result.In other Words, your query matches max runid's across all t_id's.My suggestion matches on both t_id and runid. |
|
|
Anthem
Starting Member
5 Posts |
Posted - 2013-10-23 : 17:52:31
|
Again thanks for replying, i am confused as i am using MAX(RunOn) which is a datetime, so it should return the most recent ?, they are both different dates and times, 2 days apart, but it returns both, should MAX not just return the newest ?. You are right that there are many runs under 21405, but each one has a unique datetime, so i am expecting just the newest one ?, sorry for being a bit slow if this is obvious the RunOn values are :-14/10/20013-09:30:0116/10/2013-12:25:05Should the 16/10 not be returned ?Thanks again |
|
|
bitsmed
Aged Yak Warrior
545 Posts |
Posted - 2013-10-23 : 18:11:24
|
quote: Originally posted by Anthem Again thanks for replying, i am confused as i am using MAX(RunOn) which is a datetime, so it should return the most recent ?, they are both different dates and times, 2 days apart, but it returns both, should MAX not just return the newest ?. You are right that there are many runs under 21405, but each one has a unique datetime, so i am expecting just the newest one ?, sorry for being a bit slow if this is obvious the RunOn values are :-14/10/20013-09:30:0116/10/2013-12:25:05Should the 16/10 not be returned ?Thanks again
Try searching for runid's = 14/10/2013-09:30:01My guess is, this runid is the max runid, for another t_id.Now if you would like to focus your select only on t_id 21405, your sql could be like thisselect status ,runon ,sort ,t_id from dbo.results where runon in (select max(runon) from dbo.results where t_id=21405 ) and t_id=21405 order by sort |
|
|
Anthem
Starting Member
5 Posts |
Posted - 2013-10-23 : 18:22:12
|
You are right !, i have searched and there is another test with exactly the same time stamp but under a different t_id !, can you explain how this would affect my requests for results just for 21405 ?Thanks a lot !, i think i can see light at the end of the tunnel ! ;) |
|
|
Anthem
Starting Member
5 Posts |
Posted - 2013-10-23 : 18:33:17
|
I have looked into it and you are right, the timestamp matches another test so it is being included !, thanks so much, really appreciate it !, i was going mad !, so to get round that problem, is your suggestion at the top the best way forward ?Thanks again ! |
|
|
bitsmed
Aged Yak Warrior
545 Posts |
Posted - 2013-10-23 : 18:39:59
|
quote: Originally posted by Anthem You are right !, i have searched and there is another test with exactly the same time stamp but under a different t_id !, can you explain how this would affect my requests for results just for 21405 ?Thanks a lot !, i think i can see light at the end of the tunnel ! ;)
Using the latest sql I gave you, will return only one row, but you can only focus on one t_id. Using the first sql I gave you, will do exactly the same (returning one row per t_id), but you will be able to focus on multiple/all t_id's.I know which one I would chose, if I were the one to decide.Now, you could speed up the first sql I gave you, if you are focusing on one/a few t_id's, like thisselect a.status ,a.runon ,a.sort ,a.t_id from dbo.results as a inner join (select t_id ,max(runon) as runon from dbo.results where t_id in (21405,21406) group by t_id ) as b on b.t_id=a.t_id and b.runon=a.runon order by a.sort |
|
|
bitsmed
Aged Yak Warrior
545 Posts |
Posted - 2013-10-23 : 18:44:58
|
quote: Originally posted by Anthem I have looked into it and you are right, the timestamp matches another test so it is being included !, thanks so much, really appreciate it !, i was going mad !, so to get round that problem, is your suggestion at the top the best way forward ?Thanks again !
I don't know if it the best way, but it's the best way of the two mentioned. I bet some of the experts in here will be able to come up with even better ways. I'm curious to see what they suggest. |
|
|
waterduck
Aged Yak Warrior
982 Posts |
Posted - 2013-10-23 : 22:00:05
|
[code]select a.status, a.runon, a.sort, a.t_idfrom dbo.results as awhere runon = (select max(runon) from dbo.results as b where a.t_id = b.t_id)[/code] |
|
|
|
|
|
|
|