Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Return Empty is a specific record is encontered. Expand / Collapse
Author
Message
Posted Thursday, July 4, 2013 2:18 PM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Thursday, August 21, 2014 7:50 PM
Points: 19, Visits: 111
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!!





Post #1470507
Posted Thursday, July 4, 2013 2:25 PM


SSC Eights!

SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!

Group: General Forum Members
Last Login: Thursday, August 21, 2014 3:04 PM
Points: 801, Visits: 715
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'



Erland Sommarskog, SQL Server MVP, www.sommarskog.se
Post #1470508
Posted Thursday, July 4, 2013 6:50 PM


Hall of Fame

Hall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of Fame

Group: General Forum Members
Last Login: Thursday, August 28, 2014 8:30 PM
Points: 3,627, Visits: 5,273
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!

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?
Since random numbers are too important to be left to chance, let's generate some!
Learn to understand recursive CTEs by example.
Splitting strings based on patterns can be fast!
Post #1470542
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse