Avatar of dgloveruk
dgloveruk
 asked on

How can I combine SQL select by columns?

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
Microsoft SQL Server

Avatar of undefined
Last Comment
dgloveruk

8/22/2022 - Mon
Vitor Montalvão

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.
Jim Horn

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.
ASKER CERTIFIED SOLUTION
Scott Pletcher

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
dgloveruk

ASKER
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.
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck
Vitor Montalvão

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?
dgloveruk

ASKER
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?
Vitor Montalvão

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.
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
dgloveruk

ASKER
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.
Vitor Montalvão

The only way a stored procedure has to return values is using OUTPUT parameters. You can read more about it here.
dgloveruk

ASKER
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!
Your help has saved me hundreds of hours of internet surfing.
fblack61
SOLUTION
Log in to continue reading
Log In
Sign up - Free for 7 days
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
Scott Pletcher

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.
dgloveruk

ASKER
Thanks guys!