filtering data correctly?

Discussion in 'SQL Server' started by PaulB, Feb 1, 2007.

  1. PaulB

    PaulB New Member

    Joined:
    Feb 1, 2007
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    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?
     
    Last edited by a moderator: Feb 1, 2007

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice