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

How to write Query on the Result Set of Another Query Expand / Collapse
Author
Message
Posted Thursday, October 10, 2013 7:05 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Friday, February 14, 2014 7:24 PM
Points: 25, Visits: 40


Hai Every One. Can Any body help me Please.


I Want to Write a Query On the Result Set Of Another Query.

How to Write
Can Any body help me Please.
Post #1503541
Posted Thursday, October 10, 2013 7:14 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 12:44 PM
Points: 12,966, Visits: 10,740
You mean something like this:

SELECT * FROM
(SELECT * FROM myTable WHERE col = 'A') tmp;

Subquery Fundamentals




How to post forum questions.
Need an answer? No, you need a question.
What’s the deal with Excel & SSIS?

Member of LinkedIn. My blog at LessThanDot.

MCSA SQL Server 2012 - MCSE Business Intelligence
Post #1503554
Posted Friday, October 11, 2013 1:53 PM
SSC-Addicted

SSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-Addicted

Group: General Forum Members
Last Login: Today @ 9:32 AM
Points: 471, Visits: 1,452
Maybe something like this

with cte (ID, somefield) as
(
select ID, FieldName from TableA
)

select TableB.Field
from TableB
join cte
on cte.ID = TableB.ID
Post #1504140
Posted Friday, October 11, 2013 3:32 PM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: General Forum Members
Last Login: Today @ 7:36 AM
Points: 35,218, Visits: 31,677
perireddy.arikatla (10/10/2013)


Hai Every One. Can Any body help me Please.


I Want to Write a Query On the Result Set Of Another Query.

How to Write
Can Any body help me Please.



There are a lot of ways to do this. Koen's code demonstrates a "Derived Table". There are other methods, as well.

CTE
Derived Tables (Sub-queries)
Correlated Sub-Queries
Views
Inline Table Valued Functions
APPLY (CROSS or OUTER)
Temp Tables
INSERT/EXEC
OpenRowSet
OpenDataSource


--Jeff Moden
"RBAR is pronounced "ree-bar" and is a "Modenism" for "Row-By-Agonizing-Row".

First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column."

(play on words) "Just because you CAN do something in T-SQL, doesn't mean you SHOULDN'T." --22 Aug 2013

Helpful Links:
How to post code problems
How to post performance problems
Post #1504151
Posted Friday, October 11, 2013 4:10 PM
SSC-Addicted

SSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-Addicted

Group: General Forum Members
Last Login: Today @ 9:32 AM
Points: 471, Visits: 1,452
As Jeff showed there are many ways to do this. I only provided one possibility.

I've been a silent observer on this site for along time and finally feel that I have some knowledge to begin offering solutions to some questions.
Post #1504165
Posted Saturday, October 12, 2013 9:04 AM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: General Forum Members
Last Login: Today @ 7:36 AM
Points: 35,218, Visits: 31,677
lrosini (10/11/2013)
As Jeff showed there are many ways to do this. I only provided one possibility.

I've been a silent observer on this site for along time and finally feel that I have some knowledge to begin offering solutions to some questions.


Ver y cool. Welcome to the fun.


--Jeff Moden
"RBAR is pronounced "ree-bar" and is a "Modenism" for "Row-By-Agonizing-Row".

First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column."

(play on words) "Just because you CAN do something in T-SQL, doesn't mean you SHOULDN'T." --22 Aug 2013

Helpful Links:
How to post code problems
How to post performance problems
Post #1504207
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse