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


Select all data where one condition is met


Select all data where one condition is met

Author
Message
DataAnalyst011
DataAnalyst011
Old Hand
Old Hand (352 reputation)Old Hand (352 reputation)Old Hand (352 reputation)Old Hand (352 reputation)Old Hand (352 reputation)Old Hand (352 reputation)Old Hand (352 reputation)Old Hand (352 reputation)

Group: General Forum Members
Points: 352 Visits: 429
Below is some sample data. I want to SELECT all Stat's where an ID exists with a StatDate in AppRec >= 2012-12-01. So the result should be:

ID Stat StatDate
1 AppRec 2012-12-01
2 AppCom 2012-12-02

Note: ID 3 shouldn't get returned because although his "AppCom" Stat is after 2012-12-01, his "AppRec" wasn't. Also note that I need to return all statuses (both AppRec and AppCom in this case). So a WHERE Stat = 'AppRec" doesn't work. Any help is appreciated!

/*Create the table*/
CREATE TABLE Apps
(
ID VARCHAR(50) NOT NULL,
Stat VARCHAR(50) NOT NULL,
StatDate DATE NOT NULL
)
;

/*Putting data in the table to create the test enviroment*/
INSERT INTO Apps
(
ID,
Stat,
StatDate
)

Values
('1', 'AppRec', '2012-12-01'),
('1', 'AppCom', '2012-12-02'),
('2', 'AppRec', '2012-11-01'),
('2', 'AppCom', '2012-11-01'),
('3', 'AppRec', '2012-11-15'),
('3', 'AppCom', '2012-12-01')
;


Richard Fryar
Richard Fryar
Mr or Mrs. 500
Mr or Mrs. 500 (503 reputation)Mr or Mrs. 500 (503 reputation)Mr or Mrs. 500 (503 reputation)Mr or Mrs. 500 (503 reputation)Mr or Mrs. 500 (503 reputation)Mr or Mrs. 500 (503 reputation)Mr or Mrs. 500 (503 reputation)Mr or Mrs. 500 (503 reputation)

Group: General Forum Members
Points: 503 Visits: 1172
Is there a typo in your question? It looks like you want the following to be returned, unless I have misunderstood:

ID Stat StatDate
1 AppRec 2012-12-01
1 AppCom 2012-12-02


If so, this will do it:

SELECT ID, Stat, StatDate
FROM Apps
WHERE ID IN (SELECT ID FROM Apps WHERE Stat = 'AppRec' AND StatDate >= '2012-12-01')




Check Your SQL Servers Quickly and Easily
www.sqlcopilot.com
Alan.B
Alan.B
SSCertifiable
SSCertifiable (5.3K reputation)SSCertifiable (5.3K reputation)SSCertifiable (5.3K reputation)SSCertifiable (5.3K reputation)SSCertifiable (5.3K reputation)SSCertifiable (5.3K reputation)SSCertifiable (5.3K reputation)SSCertifiable (5.3K reputation)

Group: General Forum Members
Points: 5270 Visits: 7735
Your typo messed me up too...

You can also try:


;WITH x AS
( SELECT ID, Stat, StatDate
FROM #Apps
WHERE Stat='AppRec' AND StatDate >= '2012-12-01' )
SELECT ID, Stat, StatDate
FROM #Apps
WHERE ID in (SELECT ID FROM x)



Edit: I just realized I posted basically the exact same query as above which will produce the same query plan... Now you have the CTE and non-CTE-version... Smooooth

-- Alan Burstein



Best practices for getting help on SQLServerCentral
Need to split a string? Try DelimitedSplit8K or DelimitedSplit8K_LEAD (SQL 2012+)
Need a pattern-based splitter? Try PatternSplitCM
Need to remove or replace those unwanted characters? Try PatExclude8K and PatReplace8K.

"I can't stress enough the importance of switching from a 'sequential files' mindset to 'set-based' thinking. After you make the switch, you can spend your time tuning and optimizing your queries instead of maintaining lengthy, poor-performing code. " -- Itzek Ben-Gan 2001
DataAnalyst011
DataAnalyst011
Old Hand
Old Hand (352 reputation)Old Hand (352 reputation)Old Hand (352 reputation)Old Hand (352 reputation)Old Hand (352 reputation)Old Hand (352 reputation)Old Hand (352 reputation)Old Hand (352 reputation)

Group: General Forum Members
Points: 352 Visits: 429
I really apologize for the typo! Typo notwithstanding, you guys gave me exactly what I was looking for. Thanks alot for the help!
DataAnalyst011
DataAnalyst011
Old Hand
Old Hand (352 reputation)Old Hand (352 reputation)Old Hand (352 reputation)Old Hand (352 reputation)Old Hand (352 reputation)Old Hand (352 reputation)Old Hand (352 reputation)Old Hand (352 reputation)

Group: General Forum Members
Points: 352 Visits: 429
Again this is really helpful. I hope you all won't mind a follow-up question:

What is the difference between...

WHERE [column] IN ([subquery]) 



and

WHERE EXISTS ([subquery])



?

I've been trying to read on it and trying queries on my test server but can't come up with anything helpful. Would someone mind explaining? I want to be able to support my code :-)
dwain.c
dwain.c
SSCertifiable
SSCertifiable (7.5K reputation)SSCertifiable (7.5K reputation)SSCertifiable (7.5K reputation)SSCertifiable (7.5K reputation)SSCertifiable (7.5K reputation)SSCertifiable (7.5K reputation)SSCertifiable (7.5K reputation)SSCertifiable (7.5K reputation)

Group: General Forum Members
Points: 7453 Visits: 6431
DataAnalyst011 (12/17/2012)
Again this is really helpful. I hope you all won't mind a follow-up question:

What is the difference between...

WHERE [column] IN ([subquery]) 



and

WHERE EXISTS ([subquery])



?

I've been trying to read on it and trying queries on my test server but can't come up with anything helpful. Would someone mind explaining? I want to be able to support my code :-)


In the first case, the WHERE clause is true if the [column] exists in any of the rows returned by the subquery.

In the second case, the WHERE clause is true if any rows are returned by the subquery.


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
Lynn Pettis
Lynn Pettis
SSC-Forever
SSC-Forever (40K reputation)SSC-Forever (40K reputation)SSC-Forever (40K reputation)SSC-Forever (40K reputation)SSC-Forever (40K reputation)SSC-Forever (40K reputation)SSC-Forever (40K reputation)SSC-Forever (40K reputation)

Group: General Forum Members
Points: 40288 Visits: 38567
Here is the code using the EXISTS in the WHERE clause:



/*Create the table*/
CREATE TABLE dbo.Apps
(
ID VARCHAR(50) NOT NULL,
Stat VARCHAR(50) NOT NULL,
StatDate DATE NOT NULL
)
;

/*Putting data in the table to create the test enviroment*/
INSERT INTO dbo.Apps
(
ID,
Stat,
StatDate
)

Values
('1', 'AppRec', '2012-12-01'),
('1', 'AppCom', '2012-12-02'),
('2', 'AppRec', '2012-11-01'),
('2', 'AppCom', '2012-11-01'),
('3', 'AppRec', '2012-11-15'),
('3', 'AppCom', '2012-12-01')
;

go

select
a1.ID,
a1.Stat,
a1.StatDate
from
dbo.Apps a1
where exists(select
1
from
dbo.Apps a2
where
a1.ID = a2.ID and
a2.Stat = 'AppRec' and
a2.StatDate >= '20121201');


go

drop table dbo.Apps;
go




Cool
Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
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