Return Empty is a specific record is encontered.

  • Hi all,

    This seems to be very easy (and probably is!!) but I'm just stuck here.

    I have a table that contains an ID, a date and a Status.

    I want to return the top row (most recent datetime) if any of the status for the same Id is not C (as in Canceled).

    Here's an example:

    create table #Temp1

    (RecID int, DateEntered Datetime, Status Varchar(1))

    INSERT INTO #Temp1 VALUES (1,'01-01-2013 10:20:10', 'N')

    INSERT INTO #Temp1 VALUES (1,'01-02-2013 08:14:00', 'R')

    INSERT INTO #Temp1 VALUES (1,'01-03-2013 03:30:00', 'E')

    INSERT INTO #Temp1 VALUES (2,'01-01-2013 14:58:00', 'N')

    INSERT INTO #Temp1 VALUES (2,'01-02-2013 08:23:00', 'R')

    INSERT INTO #Temp1 VALUES (2,'01-04-2013 22:14:00', 'C')

    select * from #Temp1

    drop table #temp1

    In this examples my result set should be:

    1 ; 01-03-2013 03:30:00 ; E

    No record should be returned from RecId 2 because the last line is a 'C'.

    The 'C' status will always be in the last row.

    Thank you all for your help!!

  • This is easily achieved with CTE that numbers the rows and then you can filter on rowno = 1:

    ; WITH numbering AS (

    SELECT RecID, DateEntered, Status,

    row_number() OVER(PARTITION BY RecID ORDER BY DateEntered DESC) AS rowno

    FROM #Temp1

    )

    SELECT RecID, DateEntered, Status

    FROM numbering

    WHERE rowno = 1

    AND Status <> 'C'

    [font="Times New Roman"]Erland Sommarskog, SQL Server MVP, www.sommarskog.se[/font]

  • Erland Sommarskog (7/4/2013)


    This is easily achieved with CTE that numbers the rows and then you can filter on rowno = 1:

    ; WITH numbering AS (

    SELECT RecID, DateEntered, Status,

    row_number() OVER(PARTITION BY RecID ORDER BY DateEntered DESC) AS rowno

    FROM #Temp1

    )

    SELECT RecID, DateEntered, Status

    FROM numbering

    WHERE rowno = 1

    AND Status <> 'C'

    Mr. Sommarskog,

    Excuse my impetuous presumtiveness, but I just wanted to say I've read a number of your articles and learned a lot from them. I noticed you posting here just yesterday in fact and I just had to say something, but most especially thanks for your contribution to the SQL body of knowledge.

    Dwain


    My mantra: No loops! No CURSORs! No RBAR! Hoo-uh![/I]

    My thought question: Have you ever been told that your query runs too fast?

    My advice:
    INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?
    The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.

    Need to UNPIVOT? Why not CROSS APPLY VALUES instead?[/url]
    Since random numbers are too important to be left to chance, let's generate some![/url]
    Learn to understand recursive CTEs by example.[/url]
    [url url=http://www.sqlservercentral.com/articles/St

Viewing 3 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic. Login to reply