0

Is it possible to make a query containing 4 SELECT statements where each result is placed in its own column? How?

I'm currently sitting with 4 queries that I need to somehow combine into 1 and I've tried using UNION but it seems to put the results in the same column, just a new row.

Thanks.

1
  • Try to follow this pattern: select 1 as one, (select 2 from dual) as two from dual Commented Nov 12, 2012 at 12:36

2 Answers 2

2

If the SELECT statements each return a SCALAR result, i.e. single-row, single-column - then yo can just do this:

SELECT (select .... ) Column1,
       (select .... ) Column2,
       (select .... ) Column3,
       (select .... ) Column4;
Sign up to request clarification or add additional context in comments.

2 Comments

Would this work different if I needed to use SUM in all SELECT statements?
No it wouldn't. It's still the same flat pattern.
1

You can use sub-queries and a column alias:

SELECT (SELECT TOP 1 Col1 From dbo.Table2 WHERE Condition1)AS Col1
,      (SELECT TOP 1 Col1 From dbo.Table3 WHERE Condition2)AS Col2
FROM dbo.Table1

Using a Subquery in a T-SQL Statement

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.