Solved

How can I combine SQL select by columns?

Posted on 2014-11-26
13
116 Views
Last Modified: 2014-11-27
I would like to combine results from a number of tables/queries to produce the table shown in ResultsD.
Please note I am limited to SQL server 2000.
The tables are not related and the fields in the rows in the output ResultsD are therefore not related to each other.

ResultsA
1
2
3
4

ResultsB
Apple
Pear
Lemon

ResultsC
1.0
3.0
5.0
6.0
7.0

ResultsD
1                Apple        1.0
2                Pear          3.0            
3                Lemon      5.0
4                Null           6.0
Null           Null           7.0
0
Comment
Question by:dgloveruk
[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
  • 4
  • 3
  • +1
13 Comments
 
LVL 51

Expert Comment

by:Vitor Montalvão
ID: 40466839
In SQL Server 2000 you can't. Being a relational database you need to find something that can relate those 3 tables.
In newer versions of SQL Server it's possible to attribute row numbers to the records and use it for relating data. In this case you can't do what you want.
0
 
LVL 66

Expert Comment

by:Jim Horn
ID: 40466906
Can you explain why you want this?  Perhaps there is an easier workaround.

>Please note I am limited to SQL server 2000.
Okay, but be advised that you're running six versions old (2005 > 2008 > R2 > 2012 > 2014), which means Microsoft support is over, experienced experts are unlikely to maintain skillsets exclusively to 2000, and in the event of any server/database crash the odds of not being able to restore back to 2000 diminish greatly.  I'd tell your employer to spend some money to protect their data and upgrade.
0
 
LVL 69

Accepted Solution

by:
Scott Pletcher earned 500 total points
ID: 40468061
You must use temp tables in SQL 2000; might be best anyway, given the joins you need to do.

--Setup: we must add an identity to each result to join on---------------------------------
SELECT IDENTITY(int, 1, 1) AS ident, *
INTO #ResultsA
FROM ResultsA a
ORDER BY a.col1

SELECT IDENTITY(int, 1, 1) AS ident, *
INTO #ResultsB
FROM ResultsB b
ORDER BY b.col1

SELECT IDENTITY(int, 1, 1) AS ident, *
INTO #ResultsC
FROM ResultsC c
ORDER BY c.col1

--Ok, now we're ready for the final join itself-------------------------------------------------------
SELECT a.col1, b.col1, c.col1
FROM #ResultsA a
FULL OUTER JOIN #ResultsB b ON
    b.ident = a.ident
FULL OUTER JOIN #ResultsC c ON
    c.ident IN (a.ident, b.ident)
0
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

 

Author Comment

by:dgloveruk
ID: 40468234
Thank you Scott, to answer Jim, my reason for asking is as follows.  I have a view of results.... For arguments sake lets say customer orders.  There are several things from this list of results that i need distinct lists of e.g salespersons, item categories and payment types to build filters in the user interface with options only relating to the orders being retrieved.  I cannot requery the view for distinct values of each column of interest since the underlying view is rapidly evolving and the view is fairly process intensive anyway to be doing a series of distinct selects on each column.  What i was going to try was querying the view for each of the columns of interest and their distinct values and return them as one table for building filter options from in the user interface.  I thought the approach above would allow me to return a complete set of filters options for several fields at once without having to action a series of queries from the client to generate each filter option.
0
 
LVL 51

Expert Comment

by:Vitor Montalvão
ID: 40468822
What i was going to try was querying the view for each of the columns of interest and their distinct values and return them as one table for building filter options from in the user interface.
Ok, but can't you add to your query a field that can be related to all these records so it can used for a JOIN?
0
 

Author Comment

by:dgloveruk
ID: 40468836
I think what I'm going to do is put the original Query results into a temporary table and then return the distinct selections from this temporary table to get the filter options as well as the temporary table itself.
Originally I didn't go in this direction because I did not want to break up the process of capturing the query results and then getting then querying for filter options since they could have changed and to get the distinct options based on the original query would have been further overhead for each filter.  But what I didn't know which I've read is that you can return multiple sets of data from a single execution of a stored procedure using NextResult and this basically solves my problem of getting the query and a list of filters based on that query, without resubmitting the original query.
Does this sound like a reasonable approach?
0
 
LVL 51

Expert Comment

by:Vitor Montalvão
ID: 40468843
The NextResult is a .NET method, right?
We are giving you only native SQL Server solutions. Can't talk about the NextResult since isn't my expertise area.
0
 

Author Comment

by:dgloveruk
ID: 40468849
That's right.
I'm assuming that internally to SQL server a stored procedure can return multiple sets of results for handling?
If so, I wasn't aware of this else I might have guessed earlier that there would be a method with .NET to cycle through the different sets of results.
0
 
LVL 51

Expert Comment

by:Vitor Montalvão
ID: 40468883
The only way a stored procedure has to return values is using OUTPUT parameters. You can read more about it here.
0
 

Author Comment

by:dgloveruk
ID: 40469425
I've requested that this question be closed as follows:

Accepted answer: 500 points for ScottPletcher's comment #a40468061
Assisted answer: 0 points for dgloveruk's comment #a40468836

for the following reason:

Thank you for your help guys!
0
 
LVL 69

Assisted Solution

by:Scott Pletcher
Scott Pletcher earned 500 total points
ID: 40469399
>> I'm assuming that internally to SQL server a stored procedure can return multiple sets of results for handling? <<

That is correct.  By default SQL will return the first result set to the caller, but there are methods to get any additional result set(s), if any.

You can also return a single integer return code, using
RETURN <int_value>
By default, SQL will return 0.

Finally, as noted by Vitor, you can return one or more scalar (single-value) parameters by specifying them as "OUTPUT" parameters.
0
 
LVL 69

Expert Comment

by:Scott Pletcher
ID: 40469414
I've done the same thing before.  You have distinct lists but you want to see them in a compact format.  Joining by line# works perfectly in those cases.
0
 

Author Closing Comment

by:dgloveruk
ID: 40469426
Thanks guys!
0

Featured Post

[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

Question has a verified solution.

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

Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
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.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

624 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