Hi folks,
Not been very active of late here on tek-tips. I've been very busy in every are of life, so not hardly time to even check in but every few days, it seems.
Well, at work, I'm trying to clean up some SQL code as well as some other things, in a new department I'm working in.
I've already got some cleanup done, but lots more to do.
Anyway, I've got this one type of TIME conversion that is apparently necessary in a given query, and I'm hoping there's a way to do this a little easier on the server, so it'll run faster.
Here's the current SQL:
The Account field contains account numbers in a varchar format.
Apparently, with the way this table was designed, you have to combine the DATE field with the TIME field to get the full datetime stamp. For the life of me, I can't figure that one out, as most everything else I've seen at work has just one datetime field, so everything is covered. This one might have something to do with the data originally coming out of an Oracle database, but I do not know that for certain.
My thought is this: Is there not some way I could get the same results in a way that wouldn't put such a heavy load on the server? I mean, with this (unless I'm wrong), it appears we have to first convert the time value a couple of times, then add it to the date, and THEN get the Maximum value for each Account... before a particular date.
So, is it just me, and perhaps I am not understanding something, or is there a better way?
Any thoughts?
Below is a sample of the data in the table I'm attempting to query from (of course, adjusted to protect the innocent:
[TT] Account Date Time
11111 9/8/2010 23593
77777 9/14/2010 23593
88888 9/15/2010 25071
99999 9/12/2010 25071
10101 9/21/2010 25071
10102 9/5/2010 25078
10105 9/13/2010 25078
10106 9/7/2010 25131
10107 9/25/2010 25131
10111 9/4/2010 25895
10112 9/20/2010 25895
10117 9/13/2010 25895[/TT]
And here is an example of what the returned data looks like:
[TT] account LastUpdate
31312 5/14/08 8:30 AM
31351 6/21/06 9:29 AM
135131 11/30/04 6:51 AM
51313 3/24/08 9:29 AM
3131 3/24/08 9:29 AM
513131 2/25/09 10:01 AM[/TT]
Thanks for any thoughts, suggestions, references.
Not been very active of late here on tek-tips. I've been very busy in every are of life, so not hardly time to even check in but every few days, it seems.
Well, at work, I'm trying to clean up some SQL code as well as some other things, in a new department I'm working in.
I've already got some cleanup done, but lots more to do.
Anyway, I've got this one type of TIME conversion that is apparently necessary in a given query, and I'm hoping there's a way to do this a little easier on the server, so it'll run faster.
Here's the current SQL:
Code:
SELECT r.Account
,MAX(r.adate + CONVERT(datetime,CONVERT(decimal(6,6), '.' + CONVERT(varchar(50),r.atime)))) AS LastUpdate
FROM StatusTable r
WHERE r.active_date < '2010-09-30'
The Account field contains account numbers in a varchar format.
Apparently, with the way this table was designed, you have to combine the DATE field with the TIME field to get the full datetime stamp. For the life of me, I can't figure that one out, as most everything else I've seen at work has just one datetime field, so everything is covered. This one might have something to do with the data originally coming out of an Oracle database, but I do not know that for certain.
My thought is this: Is there not some way I could get the same results in a way that wouldn't put such a heavy load on the server? I mean, with this (unless I'm wrong), it appears we have to first convert the time value a couple of times, then add it to the date, and THEN get the Maximum value for each Account... before a particular date.
So, is it just me, and perhaps I am not understanding something, or is there a better way?
Any thoughts?
Below is a sample of the data in the table I'm attempting to query from (of course, adjusted to protect the innocent:
[TT] Account Date Time
11111 9/8/2010 23593
77777 9/14/2010 23593
88888 9/15/2010 25071
99999 9/12/2010 25071
10101 9/21/2010 25071
10102 9/5/2010 25078
10105 9/13/2010 25078
10106 9/7/2010 25131
10107 9/25/2010 25131
10111 9/4/2010 25895
10112 9/20/2010 25895
10117 9/13/2010 25895[/TT]
And here is an example of what the returned data looks like:
[TT] account LastUpdate
31312 5/14/08 8:30 AM
31351 6/21/06 9:29 AM
135131 11/30/04 6:51 AM
51313 3/24/08 9:29 AM
3131 3/24/08 9:29 AM
513131 2/25/09 10:01 AM[/TT]
Thanks for any thoughts, suggestions, references.