Assume I have these two queries in SQL Server 2014, both of which return one row from unrelated tables:
SELECT SUM(A) A, SUM(B) B FROM X
SELECT SUM(C) C, SUM(D) D FROM Y
I'd like to combine these queries into a single resultset that contains A, B, C, D
.
What's a good way to do this within a single query, as opposed to multi-query solutions like selecting the results into and out of scalar variables?
Justin GrantJustin Grant
asked Jun 28, 2015 at 23:06
2 Answers 2
Two subqueries?
SELECT Q1.A, Q1.B, Q2.C, Q2.D
FROM (SELECT SUM(A) A, SUM(B) B FROM @X) Q1(A, B)
CROSS APPLY (SELECT SUM(C) C, SUM(D) D FROM @Y) Q2(C, D);
answered Jun 28, 2015 at 23:48
-
1Or
CROSS JOIN
of course but hardly worth a separate answer.Martin Smith– Martin Smith2015年06月29日 08:02:47 +00:00Commented Jun 29, 2015 at 8:02 -
@martin, yes but my fingers are used to typing cross apply, I almost never type cross join.Mister Magoo– Mister Magoo2015年06月29日 10:00:52 +00:00Commented Jun 29, 2015 at 10:00
-
@MartinSmith - I tried to get the CROSS JOIN syntax to work with MySQL - no joy! Could you show me (us?) how that syntax would work with this problem?Vérace– Vérace2015年06月29日 15:03:43 +00:00Commented Jun 29, 2015 at 15:03
-
1@Vérace just replace
CROSS APPLY
withCROSS JOIN
. (You will also need to replaceQ1(A,B)
withQ1
andQ2(C,D)
withQ2
. This is not needed in SQL-Server either but will give you error in MySQL.)ypercubeᵀᴹ– ypercubeᵀᴹ2015年06月29日 15:11:17 +00:00Commented Jun 29, 2015 at 15:11 -
Seems that the "CROSS JOIN" is redundant - it can just be replaced with a comma, as per @Lennart 's answer to my other question. Thanks for your comment on my (now deleted!) answer - brain burp! Which would be more efficient - the CROSS JOIN or the SELECT NULL syntax of Lennart?Vérace– Vérace2015年06月29日 15:22:08 +00:00Commented Jun 29, 2015 at 15:22
Here's one way:
SELECT MAX(A), MAX(B), MAX(C), MAX(D)
FROM (
SELECT SUM(A) AS A, SUM(B) AS B, NULL AS C, NULL AS D FROM X
UNION ALL
SELECT NULL AS A, NULL AS B, SUM(C) AS C, SUM(D) AS D FROM Y
) AS T
You may have to cast NULL to whatever type A, B, C and D have
answered Jun 29, 2015 at 7:48
-
Brilliant - also has the advantage of running in MySQL - racking my brains last night as to how to get this to work. How would I do this using a CROSS JOIN in MySQL?Vérace– Vérace2015年06月29日 15:02:36 +00:00Commented Jun 29, 2015 at 15:02
-
@Vérace, something like:
select A, B, C, D from ( SELECT SUM(A) as A, SUM(B) as B FROM X ) as U, ( SELECT SUM(C) as C, SUM(D) as D FROM Y ) as V
should work I guessLennart - Slava Ukraini– Lennart - Slava Ukraini2015年06月29日 15:14:22 +00:00Commented Jun 29, 2015 at 15:14 -
Works with an explicit CROSS JOIN as well:
select A, B, C, D from ( SELECT SUM(A) as A, SUM(B) as B FROM X ) as U CROSS JOIN ( SELECT SUM(C) as C, SUM(D) as D FROM Y ) as V
Lennart - Slava Ukraini– Lennart - Slava Ukraini2015年06月29日 15:25:01 +00:00Commented Jun 29, 2015 at 15:25 -
Indeed - as I found out, replacing CROSS JOIN with a comma works - you've done the inverse!:-)Vérace– Vérace2015年06月29日 15:26:27 +00:00Commented Jun 29, 2015 at 15:26
-
I noticed the discussion on Mister Magoo's answer after I posted the comment :-)Lennart - Slava Ukraini– Lennart - Slava Ukraini2015年06月29日 15:28:05 +00:00Commented Jun 29, 2015 at 15:28
lang-sql