Avatar of sqldba2013
sqldba2013
 asked on

SQL query

Hi,

I am fetching data from three different tables and column names are different in all three tables and there is no relationship between these three tables.

/*Report1*/
Select a1,a2,a3 from temp1
/*Report2*/
Select b1,b2,b3,b4,b5 from temp2
/*Report3*/
Select c1,c2 from temp3

At the end of query I have to display all three tables results in single output using sql query.
For ex:
a1	a2	a3	b1	b2	b3	b4	b5	c1	c2

Open in new window


Please advise how to achieve above requirement using sql query.

Thanks in advance.
Microsoft SQL ServerMicrosoft SQL Server 2008SQL

Avatar of undefined
Last Comment
sqldba2013

8/22/2022 - Mon
Paul Jackson

If there is no logical relationship between the 3 tables then its not possible, how else are you going to determine which row data from temp1 is joined with which row data from temp2 and temp3
sqldba2013

ASKER
Hi Paul Jackson,

I don't want to add any condition/join, Just I want to merge all three tables results and need to display results in single output using normal select statement.

For ex,
report 1 query returns 10 rows,
report 2 query returns 80 rows,
report 3 query returns 2 rows

In final output, I need 92 rows with all 3 table columns headers.

All 3 tables are temp tables not physical tables.
Paul Jackson

Ok then just use UNION operator :

Select a1,a2,a3 from temp1
UNION
Select b1,b2,b3,b4,b5 from temp2
UNION
Select c1,c2 from temp3
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
sqldba2013

ASKER
Thanks for your suggestion.

I can't use UNION or UNION ALL because number of columns count is different in all tables.

I got below error.
All queries combined using a UNION, INTERSECT or EXCEPT operator must have an equal number of expressions in their target lists.

Any other suggestions?
SOLUTION
Anthony Perkins

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
ASKER CERTIFIED SOLUTION
PortletPaul

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
sqldba2013

ASKER
--