SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Return Empty is a specific record is encontered.


Return Empty is a specific record is encontered.

Author
Message
FastRider30
FastRider30
SSC Rookie
SSC Rookie (27 reputation)SSC Rookie (27 reputation)SSC Rookie (27 reputation)SSC Rookie (27 reputation)SSC Rookie (27 reputation)SSC Rookie (27 reputation)SSC Rookie (27 reputation)SSC Rookie (27 reputation)

Group: General Forum Members
Points: 27 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!!
Erland Sommarskog
Erland Sommarskog
SSCrazy
SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)

Group: General Forum Members
Points: 2745 Visits: 872
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
dwain.c
dwain.c
SSCrazy Eights
SSCrazy Eights (9.5K reputation)SSCrazy Eights (9.5K reputation)SSCrazy Eights (9.5K reputation)SSCrazy Eights (9.5K reputation)SSCrazy Eights (9.5K reputation)SSCrazy Eights (9.5K reputation)SSCrazy Eights (9.5K reputation)SSCrazy Eights (9.5K reputation)

Group: General Forum Members
Points: 9509 Visits: 6431
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!
My temporal SQL musings: Calendar Tables, an Easter SQL, Time Slots and Self-maintaining, Contiguous Effective Dates in Temporal Tables
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search