Reputation: 17437
I want to do following two select query result A and B in two column_A
and column_B
. I am trying following logic but it doesn't working in SQL Server 2008 R2
select Column_A, Column_B
From (
SQL query for A
,
SQL query for B
)
Could someone please verify whether my logic is wrong or right?
Upvotes: 2
Views: 18002
Reputation: 112402
You can nest select queries like this
SELECT A.Column, B.Column
FROM
(SELECT Column FROM table1 ...) A
INNER JOIN
(SELECT Column FROM table2 ...) B
ON A.id = B.id
This combines the rows of one data source (table or sub query) with the rows of the other one. If you query from several data sources, you will usually have to join them in some way. If you do not, you will get a Cartesian product, which combines each row of one source with each row of the other source.
Upvotes: 3
Reputation: 50855
If you just want a scalar result do this:
SELECT
(<sub-query for value A>) Column_A,
(<sub-query for value B>) Column_B
;
For scalar result sets you need to make sure that they always return only a single value, or the query will fail at run-time.
If you actually want to use sub-queries you need to name them:
SELECT Column_A, Column_B
FROM (<sub-query A>) AS A,
(<sub-query B>) AS B
...and you'll want either a JOIN
condition or WHERE
clause to properly join the tables together.
Upvotes: 4