SELECT [Patient Identifier], Date, [Operator Index], Time
FROM (SELECT ISNULL(t9.[Patient Identifier], t8.[Patient Identifier]) AS [Patient Identifier], ISNULL(t9.Date, t8.Date) AS Date, ISNULL(t9.Rows, t8.Rows)
AS Rows, c.[Operator Index], c.Time, ROW_NUMBER() OVER (PARTITION BY ISNULL(t9.[Patient Identifier], t8.[Patient Identifier]),
ISNULL(t9.Date, t8.Date)
ORDER BY c.Time) AS RowNum
FROM (SELECT [Patient Identifier], Date, 2 AS [Rows]
FROM [First Step]
WHERE [Operator Index] >= 90
GROUP BY [Patient Identifier], Date
HAVING COUNT(*) >= 2) AS t9 FULL JOIN
(SELECT [Patient Identifier], Date, 4 AS [Rows]
FROM [First Step]
WHERE [Operator Index] >= 80
GROUP BY [Patient Identifier], Date
HAVING COUNT(*) >= 4) AS t8 ON t8.[Patient Identifier] = t9.[Patient Identifier] AND t8.Date = t9.Date INNER JOIN
Complete AS c ON c.[Patient Identifier] = ISNULL(t9.[Patient Identifier], t8.[Patient Identifier]) AND c.Date = ISNULL(t9.Date, t8.Date)) AS d
WHERE d .RowNum <= d .[Rows]
Current Input
Patient ID DATE Time Operator Index
51700003 18OCT2006 11:48 91
51700003 18OCT2006 11:50 100
51700004 17OCT2006 11:41 89
51700004 17OCT2006 11:50 93
51700004 17OCT2006 11:52 91
51700004 17OCT2006 12:00 93
Current Output
Patient ID DATE Time Operator Index
0517_00003 18OCT2006 11:48 91
0517_00003 18OCT2006 11:50 100
0517_00004 17OCT2006 11:41 89
0517_00004 17OCT2006 11:50 93
It should be
Patient ID DATE Time Operator Index
51700003 18OCT2006 11:48 91
51700003 18OCT2006 11:50 100
51700004 17OCT2006 11:50 93
51700004 17OCT2006 11:52 91
The data is organized by patient id, date, time (ascending)
For a given patient id, on a certain data, testing was performed. A value between 80 and 100 is acceptable data. I need either the first 2 tests with a score above 90 or the first 4 tests above 80. (The tests are further sorted by time because the testing is time dependant. On some occassions, there is just too much data. What is wrong with my current query?
5 1356 iburyak 1,017
Recognized Expert Top Contributor
I checked and it works try this:
[PHP]Declare @patient_res table ([Patient Identifier] int, Date varchar(10), Time varchar(10), [Operator Index] int)
Declare @PatientID int, @Date varchar(10)
DECLARE Patient CURSOR FOR
SELECT Distinct [Patient Identifier], Date
FROM [First Step]
OPEN Patient
FETCH NEXT FROM Patient INTO @PatientID, @Date
WHILE @@FETCH_STATUS = 0
BEGIN
IF (select Count(*) from [First Step] where [Patient Identifier] = @PatientID and Date = @Date and [Operator Index] > 89) > 1
INSERT INTO @patient_res
SELECT TOP 2 [Patient Identifier], Date, Time,[Operator Index]
FROM [First Step]
WHERE [Patient Identifier] = @PatientID and Date = @Date and [Operator Index] > 89
ORDER BY Time
ELSE IF (select Count(*) from [First Step] where [Patient Identifier] = @PatientID and Date = @Date and [Operator Index] between 80 and 89) > 3
INSERT INTO @patient_res
SELECT TOP 4 [Patient Identifier], Date, Time,[Operator Index]
FROM [First Step]
WHERE [Patient Identifier] = @PatientID and Date = @Date and [Operator Index] between 80 and 89
ORDER BY Time
ELSE
INSERT INTO @patient_res
SELECT TOP 4 [Patient Identifier], Date, Time,[Operator Index]
FROM [First Step]
WHERE [Patient Identifier] = @PatientID and Date = @Date
ORDER BY Time
FETCH NEXT FROM Patient INTO @PatientID, @Date
END
CLOSE Patient
DEALLOCATE Patient
select * from @patient_res [/PHP]
iburyak 1,017
Recognized Expert Top Contributor
You kept changing your nick and said that you already solved this problem so I didn't bother to post my answer before.
Good Luck.
I thought that I had corrected the problem. The only issue that I am having with the current code is that if there are more than 2 values in the 90's, it is keeping all of them. I am close though. Thanks for all of your help.
Patient ID Date Time Operator Index
55100003 09NOV2006 09:17 91
55100003 09NOV2006 09:18 100
55100003 14NOV2006 10:43 97 (should be discarded)
55100003 14NOV2006 10:44 94 (should be discarded)
I think I figured it out, there is an error in my raw data. I need to go back and recompile that. Your codes works excellent. Thanks for all of your help.
Sign in to post your reply or Sign up for a free account.
Similar topics |
by: Mark V. |
last post by:
Here's what I have and I'm stumped. I have a table that has several
thousand names and addresses. I only want to send to one address in a
household. So what I would like to do is create a new column that runs
a macro or whatever it takes to flag records so they are housholded.
Some records have a Father and son at the same address, so I would...
|
by: Jason |
last post by:
I am trying to filter records in a primary form based on records in
related tables. The data in the related tables is being displayed in
the primary form
through subforms. To be more specific, I have a primary form named
TestResults, which is connected to data in a table named TestResults.
There are basically two other tables that are...
|
by: Ken |
last post by:
I wrote a function to use in queries that takes a date and adds or
subtracts a certain length time and then returns the new value. There
are times when my function needs to return Null values.
Function DateCalc (blah...) As Variant
Do Stuff...
If Not IsNull(varNewDate) Then
DateCalc = varNewDate
End If
End Function
|
by: Raffle |
last post by:
Hi all,
I'm still fairly new to Access and I was hoping to get some assistance
with something I have yet to accomplish. The desired result is a form
where users can enter data but for a few select fields, the data that
is available to be entered is filtered based on a selection made in
another field.
Right now, I have a table created for...
|
by: |
last post by:
Hello,
Does anyone have an idea on how I can filter the data in the gridview
control that was returned by an sql query?
I have a gridview that works fine when I populate it with data. Now I want
to look at that data and filter it based on what is in it.
I know that this could have been done with data sets and data views in
asp.net 1.1...
| |
by: vncntj |
last post by:
the problem i'm having is that i can't get the Sql statement set to
search for Department or Last_Name%..
:(
<asp:SqlDataSource ID="SqlDataSource2" runat="server"
ConnectionString="<%$ ConnectionStrings:dbPMAEventsConnectionString %>"
SelectCommand="SELECT , , ,
, , FROM WHERE
(( = @Department_ID) OR ( = @Last_Name%))">
|
by: JUAN ERNESTO FLORES BELTRAN |
last post by:
Hi you all,
I am developping a python application which connects to a database
(postresql) and displays the query results on a treeview. In adittion to
displaying the info i do need to implement filtering facility for all the
columns of the treestore/liststore model in order to allow the user an easy
search method to find the desired...
|
by: Shawn Ramirez |
last post by:
As with most web applications speed is a huge deal to me in my applications.
My customers don't really care if my app is a true 3 tier application or not,
they just want it to be faster then it was yesterday. Because of this I try
to limit my calls to the database by using a loading lookup data for a
customer and then filtering down to what I...
|
by: Harry Haller |
last post by:
Hello, I want to implement a generic list which will be used to
display 7 columns in a GridView. One should be able to sort, filter
and page each of the 7 columns. Ideally the filter should be
implemented simultaneously for multiple columns - but the data need
only be sorted by a single column at a time. Sorting should be both
ascending and...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it. ...
| |
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |