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
SQL combining two row results into one row
I am Not Clear about your Expected Output. I assume the output you got is as below
employee fname lname whpcity rspcity
a mr x city1 city2
b mr y city1 city2
a mr x city2 city1
b mr y city2 city1
In the above you need to use Stuff along with xmlpath to concatenate the city's belong to each employee
SELECT employee, fname, lname, STUFF ((SELECT ','+ whpcity
from #emp e1
where e1.employee = e2.employee
FOR XML PATH (''))
,1,1,'') as whpcity
from #emp e2
group by employee, fname, lname
the output you will get is as below
employee fname lname whpcity
a mr x city1,city2
B mr Y city2,city1
Also I suggest you to use ANSI-syntax while Joining two tables.
SELECT e.employeeID,e.fName, e.mName, e.lName,whp.city as whpcity,rsp.city as rspcity
FROM Employee e
INNER JOIN
Warehouse w
ON ( e.warehouseID=w.warehouseID)
INNER JOIN Warehouse_Province whp
on (w.provinceID = whp.provinceID)
inner join Retailshop r
on (e.retailshopID=r.retailshopID)
inner join Retailshop_Provice rsp
on (r.retailprovinceID = rsp.retailprovinceID)
SQL: Combining two query results that have a common column but each has its own specific unique column
In the 1st query add a NULL
column for totalFail
and in the 2nd query add a NULL
column for totalPass
.
Use UNION ALL to get all rows and aggregate:
SELECT TrainingID,
MAX(totalPass) AS totalPass,
MAX(totalFail) AS totalFail
FROM (
SELECT TrainingID, totalPass, NULL AS totalFail
FROM QueryA
UNION ALL
SELECT TrainingID, NULL, totalFail
FROM QueryB
) t
GROUP BY TrainingID
See a simplified demo.
Combine the result of 3 SQL queries?
A derived query approach to get SUM on a total column:
SELECT SSN_ID, TIME_ID, TIME_LIBELLE, DEBUT, FIN, SSN_NB_JOURS, SUM(Total) as Total
FROM
(
-- Your original SELECT with UNION
SELECT .. FROM ..
UNION ALL
SELECT .. FROM ..
UNION ALL
SELECT .. FROM ..
) d
GROUP BY SSN_ID, TIME_id, TIME_LIBELLE, DEBUT, FIN, SSN_NB_JOURS
Such way should be a valid syntax on both: mysql and sql server
Merge two SQL SELECT queries results into one query and one result
Using UNION:
SELECT
'#b= '+CAST(COUNT(b.b_id) AS VARCHAR) b_COUNT,
'#a = '+CAST(COUNT(a.a_id) AS VARCHAR) a_COUNT
FROM #a as a LEFT JOIN #b as b ON a.a_id = b.a_id AND a.is_active = 0
WHERE CONVERT(VARCHAR(10), a.cr_date, 111) BETWEEN @STARTDATE AND @ENDDATE
UNION
SELECT
'#d= '+CAST(COUNT(d.d_id) AS VARCHAR) d_COUNT,
'#c = '+CAST(COUNT(c.c_id) AS VARCHAR) c_COUNT
FROM #c as c LEFT JOIN #d as d ON c.c_id = d.c_id AND c.is_active = 0
WHERE CONVERT(VARCHAR(10), c.cr_date, 111) BETWEEN @STARTDATE AND @ENDDATE
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 the results of two different queries into a single query in SQL
Union unions two result sets. You want to have both results, something like a merged.
The following statement isn't proved, but it should show you the idea of the solution. You have to join both, the results and the played games in one query.
select t.name, count(distinct m.id) 'Matches_Won', count(distinct r.id) 'Matches_PLayed'
from test.team t
left join test.match_scores m on m.winner = t.id
left join test.results r on r.home_team = t.id or r.away_team = t.id
group by t.name
order by Matches_Won, Matches_Played desc;
Related Topics
Transposing Rows in to Colums in SQL Server 2005
Transfer Data Between Databases with Postgresql
What Does "Select Count(1) from Table_Name" on Any Database Tables Mean
SQL Query for Finding a Value in Multiple Ranges
Compress Rows with Nulls and Duplicates into Single Rows
How to Drop Multiple Columns with a Single Alter Table Statement in SQL Server
Sp Taking 15 Minutes, But the Same Query When Executed Returns Results in 1-2 Minutes
How to Take Last Four Characters from a Varchar
Informix SQL - List All Fields & Tables
How to Force a SQL Server 2008 Database to Go Offline
How to Use Explain Plan to Optimize Queries
Sql: When It Comes to Not in and Not Equal To, Which Is More Efficient and Why
How to Set a Column Value to Null in SQL Server Management Studio
Creating a New Database and New Connection in Oracle SQL Developer