Solved

sql query - to queries one result set

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
I have a large data set and a SSIS package. How can I load this file in multi threading?
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.

895 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now