How to Combine Two Completely Different SQL Queries into One Result

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

combine two query results into one with conditions in SQL Server

You have altered your request. Suddenly both queries select from the same tables and a UNION (or UNION ALL for that matter) doesn't seem a good solution anymore.

There are very few differences between the two queries even. And looking at the whole it boils down to: select records for member_type = 'm' and tp.ldr = 1 and then keep only one record per name, preferredly one with log_text like '%LEADER Change%'. This is mere ranking, as already shown in my other answer. You only need one query to select all records in question and use TOP (1) WITH TIES to keep the best matches per name.

select top(1) with ties
n.co_id,
n.full_name,
n.id,
case when log_text like '%LEADER Change%' then rpt.date else year(tc.pst_date_lead) end
as startdate,
c.target_id as coordid,
rd.target_id as rdid
from name n
inner join tops_profile tp on n.id = tp.id
left outer join vw_mz_rpt_leader_log rpt on n.co_id = rpt.id
left outer join vw_regdirs rd on n.co_id = rd.chapter
left outer join vw_coords c on n.co_id = c.chapter
left outer join tops_chapter tc on tc.id = n.co_id
where n.member_type = 'm'
and tp.ldr = 1
order by row_number() over (
partition by n.id
order by case when log_text like '%LEADER Change%' then 1 else 2 end);

As you said you just want only one record per name, I am using ROW_NUMBER. If you want more, use RANK instead.

It's not clear why you are joining the tops_chapter table. Is log_text a column in that table? (You should use a table qualifier for this column in your query.) If it isn't, then the join is superfluous and you can remove it from your query.

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/Hive How to combine two different queries into one result with different columns

Use conditional aggregation:

select sum(case when set_id = 1530880217 then 1 else 0 end) as prev,
sum(case when set_id = 1530901756 then 1 else 0 end) as curr
from myTable
where set_id in (1530880217, 1530901756);

This assumes that columnA is never NULL. If you really want the NULL check:

select sum(case when set_id = 1530880217 and ColumnA is not null then 1 else 0 end) as prev,
sum(case when set_id = 1530901756 and ColumnA is not null then 1 else 0 end) as curr
from myTable
where set_id in (1530880217, 1530901756);

SQL combine 2 queries to one where 2 queries are from different database

Are you looking for 3-part naming? If so, this will probably work:

select (select COUNT(DISTINCT BaseVehicleID)
from BaseVehicle
) as Old,
(Select COUNT(DISTINCT BaseVehicleID)
from [EnhancedStandard_VCDB_Exported_PRD_3006].dbo.BaseVehicle
) New


Related Topics



Leave a reply



Submit