Solved

sql query - to queries one result set

Posted on 2015-01-20
5
110 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
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 73

Accepted Solution

by:
sdstuber earned 500 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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

PL/SQL can be a very powerful tool for working directly with database tables. Being able to loop will allow you to perform more complex operations, but can be a little tricky to write correctly. This article will provide examples of basic loops alon…
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

776 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