Combining the results of two SQL queries as separate columns
You can aliasing both query and Selecting them in the select query
http://sqlfiddle.com/#!2/ca27b/1
SELECT x.a, y.b FROM (SELECT * from a) as x, (SELECT * FROM b) as y
How do I combine 2 select statements into one?
You have two choices here. The first is to have two result sets which will set 'Test1' or 'Test2' based on the condition in the WHERE
clause, and then UNION
them together:
select
'Test1', *
from
TABLE
Where
CCC='D' AND DDD='X' AND exists(select ...)
UNION
select
'Test2', *
from
TABLE
Where
CCC<>'D' AND DDD='X' AND exists(select ...)
This might be an issue, because you are going to effectively scan/seek on TABLE twice.
The other solution would be to select from the table once, and set 'Test1' or 'Test2' based on the conditions in TABLE:
select
case
when CCC='D' AND DDD='X' AND exists(select ...) then 'Test1'
when CCC<>'D' AND DDD='X' AND exists(select ...) then 'Test2'
end,
*
from
TABLE
Where
(CCC='D' AND DDD='X' AND exists(select ...)) or
(CCC<>'D' AND DDD='X' AND exists(select ...))
The catch here being that you will have to duplicate the filter conditions in the CASE
statement and the WHERE
statement.
Combine two queries into one with IN different condition
If you us OR in your WHERE clause you can do this in one query
SELECT * FROM table_name
WHERE
(col1 = 'name_1' AND col2 IN ('123', '456', '345')
OR
(col1 = 'name_2' AND col2 IN ('456', '876', '548', '111')
How to combine two sql queries into one
Use a UNION query - just stuff "UNION" between the two queries:
SELECT SUM(...) AS AEROWiz
FROM ...
UNION
SELECT SUM(...) AS AEROWiz
FROM ...
update
wrap the union in yet another query:
SELECT SUM(AEROWiz)
FROM (
.... unioned queries here
) AS child
SQL: Two select statements in one query
You can do something like this:
(SELECT
name, games, goals
FROM tblMadrid WHERE name = 'ronaldo')
UNION
(SELECT
name, games, goals
FROM tblBarcelona WHERE name = 'messi')
ORDER BY goals;
See, for example: https://dev.mysql.com/doc/refman/5.0/en/union.html
How to combine two different SQL queries in one SQL statement?
You can join twice with members, once for the sender name, once for the receiver name:
SELECT member_transaction_id
, mp.member_account_id
, m.member_user_name
, amount
, CASE
-- when the amount starts with "-" such as "-100" it means paid to recipient
WHEN amount LIKE '-%' THEN 'Paid to' + M2.member_user_name
-- else it is received from the sender
ELSE ' Received from '+ M.member_user_name
END AS from_to
FROM members_transaction MP (nolock)
LEFT JOIN members M (NOLOCK) ON M.MEMBER_ID = MP.MEMBER_ID AND M.IS_USE = 1
LEFT JOIN members M2 (NOLOCK) ON M2.member_account_id = MP.member_account_id AND M2.IS_USE = 1
WHERE MP.IS_USE = 1
Best way to merge two SQL queries into one query
You need conditional aggregation:
SELECT COUNT(CASE WHEN OH.OBLC BETWEEN 1 AND 8 THEN OO.ORNO END) AS "Count_of_Open_Order_Lines",
CAST(SUM(OO.ORQT) AS DECIMAL (8,0)) AS Sum_Quantity
FROM OOLINE OO INNER JOIN OOHEAD OH
ON OO.CONO = OH.CONO AND OO.ORNO = OH.ORNO
WHERE OO.ORST BETWEEN 22 AND 66;
In the WHERE
clause I kept only the condition OO.ORST BETWEEN 22 AND 66
which is common in both queries and for Count of Open Order Lines
I used a CASE
expression based on the condition OH.OBLC BETWEEN 1 AND 8
.
Combine two separate SQL queries in a single query
What you are showing is still two separate results. One query gives you one result. If you want to combine the two queries that gives one query and one result. One method:
SELECT what, name, value
FROM
(
SELECT 'INCOME' as what, name, income as value, 1 as sortkey1, -income as sortkey2
FROM table1
WHERE income > 150
UNION ALL
SELECT 'OUTCOME' as what, name, outcome as value, 2 as sortkey1, outcome as sortkey2
FROM table2
WHERE outcome < 200
)
ORDER BY sortkey1, sortkey2;
Related Topics
Unique Constraint Controlled by a Bit Column
Sqlserver Case Expressions - Short Circuit Evaluation
How to Use SQL Server Compact Edition Ce on Mono
Find Most Common Elements in Array with a Group By
How to Perform the Same Aggregation on Every Column, Without Listing the Columns
Does Ms Access Suppress Primary Key Violations on Inserts
Concat Group by in Vertica SQL
Join Two Different Tables and Remove Duplicated Entries
Grant Access to Just One Schema in Postgresql
Date Calculation with Parameter in Ssis Is Not Giving the Correct Result
Conditions in Left Join (Outer Join) VS Inner Join
Stored Procedure, Pass Table Name as a Parameter
Bigquery Update Nested Array Field
Why Postgres Is Not Using the Index in My Query
How to Find Duplicate Records in Postgresql
Calculating SQL Server Row_Number() Over() for a Derived Table