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

Select all data where one condition is met Expand / Collapse
Author
Message
Posted Monday, December 17, 2012 4:03 PM
SSC Journeyman

SSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC Journeyman

Group: General Forum Members
Last Login: Thursday, August 28, 2014 7:49 AM
Points: 92, Visits: 341
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')
;

Post #1397460
Posted Monday, December 17, 2012 5:00 PM
SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: Friday, September 26, 2014 5:09 AM
Points: 283, Visits: 1,118
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
Post #1397476
Posted Monday, December 17, 2012 5:03 PM


Mr or Mrs. 500

Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500

Group: General Forum Members
Last Login: Today @ 3:45 AM
Points: 592, Visits: 2,764
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...


-- Alan Burstein



Read this article for best practices on asking questions.
Need to split a string? Try this (Jeff Moden)
Need a pattern-based string spitter? Try this (Dwain Camps)
My blog
Post #1397477
Posted Monday, December 17, 2012 5:38 PM
SSC Journeyman

SSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC Journeyman

Group: General Forum Members
Last Login: Thursday, August 28, 2014 7:49 AM
Points: 92, Visits: 341
I really apologize for the typo! Typo notwithstanding, you guys gave me exactly what I was looking for. Thanks alot for the help!
Post #1397480
Posted Monday, December 17, 2012 6:49 PM
SSC Journeyman

SSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC Journeyman

Group: General Forum Members
Last Login: Thursday, August 28, 2014 7:49 AM
Points: 92, Visits: 341
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
Post #1397496
Posted Monday, December 17, 2012 7:17 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: Yesterday @ 11:32 PM
Points: 3,420, Visits: 5,351
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!
Post #1397497
Posted Monday, December 17, 2012 9:08 PM


SSC-Insane

SSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-Insane

Group: General Forum Members
Last Login: Yesterday @ 7:58 PM
Points: 20,705, Visits: 32,356
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





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)
Post #1397532
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse