Combine results of 2 SQL queries into 1
NickName:Shree Kiran Vasista Ask DateTime:2018-01-19T00:33:47

Combine results of 2 SQL queries into 1

I have two sql queries as below

1. ;with test as (.........) 
   select  abc from (select ... from test)

2. ;with test1 as (....)
   select  abc from (select ... from test1)

Both of the queries have the same columns, but different conditions. I want to combine the results of these into a single datafile.

I tried doing query 1 union query 2, but

I am not able to use union as "Incorrect syntax near ';'" when it encounters the ';' of the second query

Copyright Notice:Content Author:「Shree Kiran Vasista」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/48326098/combine-results-of-2-sql-queries-into-1

Answers
Felix Pamittan 2018-01-18T16:35:41

You could declare multiple CTEs in a single query:\n\nWITH Test AS (\n SELECT ...\n),\nTest1 AS (\n SELECT ..\n)\nSELECT <column_list> FROM Test\n\nUNION\n\nSELECT <column_list> FROM Test1;\n",


Pரதீப் 2018-01-18T16:35:35

use stacked CTE\n\n;with test as (.......), \n test1 as (......)\n select abc from (select ... from test)\n union all\n select abc from (select ... from test1)\n\n\nshow the original query, may be a better alternative could be suggested ",


Thom A 2018-01-18T16:36:40

Something like this..? Your SQL is rather unclear as it's been heavily redacted.\n\nWITH test AS (.........),\ntest1 AS (....)\nSELECT abc\nFROM test\nUNION\nSELECT abc\nFROM test1;\n",


More about “Combine results of 2 SQL queries into 1” related questions

Combine results of 2 SQL queries into 1

I have two sql queries as below 1. ;with test as (.........) select abc from (select ... from test) 2. ;with test1 as (....) select abc from (select ... from test1) Both of the queries ...

Show Detail

Combine results of 2 Queries

I have a python script that queries DBpedia using 2 SPARQL queries, and, for each of the queries, puts the result in a list. Then I make a set of this list to remove duplicates, and I have the resu...

Show Detail

How do I combine the results of two queries in SQL?

My bad if I don't understand simple things, as I am just beginning to write SQL queries. I have two queries: SELECT * FROM status WHERE author IN (SELECT user1 FROM friends WHERE user2='$us...

Show Detail

Combine results from 2 SQL Server queries into 2 columns

I currently have 2 SQL queries: select SUM(CASE T1.DOCTYPE WHEN '1' THEN T1.CURTRXAM *1 WHEN '4' THEN T1.CURTRXAM *-1 WHEN '5' THEN T1.CURTRXAM *-1 WH

Show Detail

How to combine 2 SQL queries

We have 2 SQL queries we use to look up a database lock from a user login, as sometimes we need to unlock it manually. In short, we run the following query, which will return a few results: SELECT

Show Detail

Combine results of two SQL queries

I want to combine the results of two queries into one resulttable with three columns, result1, result2 and dateday, the queries are contradictory SELECT COUNT( DISTINCT `cust` ) AS result1, DATE( ...

Show Detail

Combine SQL queries

I'm trying to combine two very similar SQL queries with separate date ranges to produce a single output table. (to compare results from this week with the corresponding week last year.) I've had ...

Show Detail

Combine 2 SQL Query results

I want to combine my 2 sql results and 1 more filter which is Date. How Can I combine my these 2 sql queries. First sql SELECT e.Name,COUNT(l.userName) as UsersVisit FROM Table1 l INNER JOIN

Show Detail

Combine Loop Query Results into 1

I have a loop that queries a SP and returns results in a query, I want to combine that query (however many times the loop runs) into one final query so I can output in my jgrid table. Here is the c...

Show Detail

combine sql queries into one using IF

I have three sql queries which retrieves results from different conditions and I want to combine these queries into one using IF statement.how can i do that?

Show Detail