473,383 Members | 1,984 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

time-series SQL code.


I am trying to write a stored proc the calculates a moving average over
three periods. In the following example, I need to stratify the data by
personID and RecordID in the #Temp table, but I am not sure how to do
it. Right now I am restricting the data I use to build my time series by
personID and I get the results I want *by PersonID*. If I can figure out
how stratify by personID so I don't have to use this restriction, I'm
sure I can extend it to the RecordID.
Create Table #Temp(
tmpID int identity,
DetailID int,
RecordID int,
AdminDate Datetime,
AdminTime datetime,
Status tinyint,--decimal(9,2),
Location varchar(100),
PersonID char(9),
PatientName varchar(100),
DOB Datetime,
Drug varchar(100),
Sort varchar(10))

--populate with data by personID
insert into
#Temp(DetailID,RecordID,AdminDate,AdminTime,Status ,Location,PersonID,Pat
ientName,DOB,Drug,Sort)
Select MD.PatMedOrderDetailID, MD.PatMedOrderID, M.Date as AdminDate,
Case M.Time When 'A' then '8:00:00 AM' When 'N' then '12:00:00 AM' When
'P' then '4:00:00 AM'
When 'H' then '8:00:00 PM' else M.Time End as Admintime,
100*M.Status, P.Location,P.PersonID, P.Name as PatientName, P.DOB,
D.GenericName + ' (' + D.TradeName + ') ' +
D.Strength,Left(P.Location,3)
From PatMedOrderDetail MD Inner Join PatMedOrder MO on MD.PatMedOrderID
= MO.PatMedOrderID
inner Join PatMedPass M on MD.PatMedOrderDetailID =
M.PatMedOrderDetailID
inner join Patient P on M.PersonID = P.PersonID
inner join Drugs D on MO.DrugID = D.DrugID
Where P.PersonID = '000126230'
Order by P.PersonID,MD.patMedorderID, M.Date, M.Time

Select * from #Temp -- to view entire set
--returns relevant rows
Select Derived.RefusalRate,T.* from #Temp T inner join
(select t1.tmpID, avg(t2.Status) as RefusalRate
from #Temp t1 cross join #Temp t2
WHERE t1.tmpID>=3 AND t1.tmpID BETWEEN t2.tmpID AND t2.tmpID+2
group by T1.tmpID
having avg(t2.Status)< 100
) as Derived on T.tmpID = Derived.tmpID
Drop Table #Temp

*** Sent via Developersdex http://www.developersdex.com ***
Jul 23 '05 #1
1 2138
On Fri, 15 Apr 2005 14:24:04 GMT, anthony hanson wrote:

(snip)

Hi anthony,

I just replied to your post in microsoft.public.sqlserver.programming.
Please post to one group only!

Best, Hugo
--

(Remove _NO_ and _SPAM_ to get my e-mail address)
Jul 23 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Bart Nessux | last post by:
am I doing this wrong: print (time.time() / 60) / 60 #time.time has been running for many hours if time.time() was (21600/60) then that would equal 360/60 which would be 6, but I'm not getting...
5
by: David Stockwell | last post by:
I'm sure this has been asked before, but I wasn't able to find it. First off I know u can't change a tuple but if I wanted to increment a time tuple by one day what is the standard method to do...
6
by: David Graham | last post by:
Hi I have asked this question in alt.php as the time() function as used in setcookie belongs to php - or does it belong equally in the javascript camp - bit confused about that. Anyway, can anyone...
8
by: peterbe | last post by:
What's the difference between time.clock() and time.time() (and please don't say clock() is the CPU clock and time() is the actual time because that doesn't help me at all :) I'm trying to...
3
by: Szabolcs Nagy | last post by:
I have to measure the time of a while loop, but with time.clock i always get 0.0s, although python manual sais: "this is the function to use for benchmarking Python or timing algorithms" So i...
5
by: Erich Schreiber | last post by:
In the Python Library Reference the explanation of the time.sleep() function reads amongst others: > The actual suspension time may be less than that requested because > any caught signal will...
1
by: Drew | last post by:
Is there a way to check if it is daylight savings or not via c#? I have heard you can use System.Globalization? Thanks - Drew
1
by: davelist | last post by:
I'm guessing there is an easy way to do this but I keep going around in circles in the documentation. I have a time stamp that looks like this (corresponding to UTC time): start_time =...
2
by: Roseanne | last post by:
We are experiencing very slow response time in our web app. We run IIS 6 - windows 2003. I ran iisstate. Here's what I got. Any ideas?? Opened log file 'F:\iisstate\output\IISState-812.log'...
9
by: Ron Adam | last post by:
I'm having some cross platform issues with timing loops. It seems time.time is better for some computers/platforms and time.clock others, but it's not always clear which, so I came up with the...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.