?
Solved

sql query - to queries one result set

Posted on 2015-01-20
5
Medium Priority
?
129 Views
Last Modified: 2015-01-20
I want to run two queries with one result set so i might have

Select firstname from Names
Select Company name from Company

With the result being

firstnames company

name1           abc comp
name 2         wel company
0
Comment
Question by:vbnetcoder
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 26

Expert Comment

by:Shaun Kline
ID: 40560158
Do you have a common field between the two tables? Without it, your results will be a match of every first name with every company name.
0
 

Author Comment

by:vbnetcoder
ID: 40560172
long story but that is fine.
0
 
LVL 74

Accepted Solution

by:
sdstuber earned 2000 total points
ID: 40560207
select firstname,company from names,company
0
 
LVL 50

Expert Comment

by:Lowfatspread
ID: 40560485
just to be clear
if
Select firstname from Names
gives
firstname
tom
dick
harry
and
Select Company name from Company
gives
company name
ibm
walmart
enron

what are you desiring?
firstname company
tom           ibm
dick           walmart
harry        enron

?

if you don't have  linking columns between the two tables then that is impossible to guarantee as a result set.... (a consistent , reproducible one ...)

the select firstname,company from [name],company solution
will return 9 rows with the above data not 3....

if you want to just match up the individual resultsets and display them side by side then this format may work for you

select a.* ,b.*
   from (select .....
                 ,row_number() over ( order by .... ) as rn
               from names) as a
  full outer join
          (select ....
             ,row_number() over ( order by ... ) as rn1
           from company) as b
  on a.rn=b.rn1
 order by coalesce(a.rn,b.rn1) asc


using the row_number() windowing function ...
the over clause can either be empty e.g  over ()    or you can specify an order by clause to determine the sequence used to determine the row sequencing....
over()  will be a "random" sequence (although likely to be physical table order)

does this help?

for more assistance please confirm which DBMS and version you are using...
0
 

Author Closing Comment

by:vbnetcoder
ID: 40560498
ty
0

Featured Post

Get your Disaster Recovery as a Service basics

Disaster Recovery as a Service is one go-to solution that revolutionizes DR planning. Implementing DRaaS could be an efficient process, easily accessible to non-DR experts. Learn about monitoring, testing, executing failovers and failbacks to ensure a "healthy" DR environment.

Question has a verified solution.

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

For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Suggested Courses

777 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