Go4Expert

Go4Expert (http://www.go4expert.com/)
-   SQL Server (http://www.go4expert.com/forums/sql-server-forum/)
-   -   filtering data correctly? (http://www.go4expert.com/forums/filtering-data-correctly-t2793/)

PaulB 1Feb2007 08:20

filtering data correctly?
 
Current Code
Code:

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?


All times are GMT +5.5. The time now is 10:16.