Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Query that uses another Query within itself

Posted on 2011-05-11
6
Medium Priority
?
189 Views
Last Modified: 2012-05-11
Hello All;

OK, this is a weird question, and hopefully I will be able to explain it well enough to get some good advice.

OK
This was originally an Access Database
#1 There was a Query that was made and SAVED with name: Query1
#2 There was another Query that was made, that used the Query1 within it.

So.

(Query1)
select some, thing from table

(2nd Query)
Select some.table2, thing.Query1 from Query1, table2

Now.

I need to know how to make this type of Query work inside of SQL Server.
Since in Access, the Queries are saved within the database itself, and can then be used when needed.
SQL Server does not have this luxery (Or does it?)

Any help on this is greatly appreciated.
Carrzkiss
0
Comment
Question by:Wayne Barron
  • 3
  • 2
6 Comments
 
LVL 53

Accepted Solution

by:
_agx_ earned 2000 total points
ID: 35741976
If I'm understanding correctly an Access saved query is the same as a SQL Server "view".  So just create a view.  Then do a normal select blah, blah from "Query1".  

create view Query1
as
select some, thing from table

Assuming that's even needed in sql server ;-)
0
 
LVL 32

Expert Comment

by:Ephraim Wangoya
ID: 35741979
YOU CAN USE A DERIVED TABLE,

here is simplified example

select field1, field2, field3
from (select field1, field2, field3
         from table2) A
where A.id > 1000
0
 
LVL 32

Expert Comment

by:Ephraim Wangoya
ID: 35741988

or
  select field1, field2, (select field3 from table2 where table2.id = table1.id) as field3
  from table1
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 53

Expert Comment

by:_agx_
ID: 35742025
>> Assuming that's even needed in sql server ;-)

In other words, make sure there's a reason you actually need the "view".  For example, if you had special formatting or filters that you wanted to reuse in multiple places,  that might be a good to create a "view".  It would avoid duplication.  Otherwise, you might just combine the sql and skip the "view" altogether.

0
 
LVL 31

Author Closing Comment

by:Wayne Barron
ID: 35742155
In this case, it is needed.
Reason:
It is not mine.
It belongs to a right big company, and the manager creates the Queries (Query1)
So, the only thing that I do is create the Query to works with the (Query1), so they need to be able to change this Query anytime they need to.

And, once I created the VIEW, I did not have to use word VIEW in the Query, I named the VIEW the same as Query1 and it worked without issue.

Thank you
Carrzkiss
0
 
LVL 53

Expert Comment

by:_agx_
ID: 35742188
Yep, that's a good reason to create a VIEW.  Glad I could help!
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It is possible to export the data of a SQL Table in SSMS and generate INSERT statements. It's neatly tucked away in the generate scripts option of a database.
This month, Experts Exchange sat down with resident SQL expert, Jim Horn, for an in-depth look into the makings of a successful career in SQL.
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.
Suggested Courses

571 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question