How to add results of two select commands in same query
Yes. It is possible :D
SELECT SUM(totalHours) totalHours
FROM
(
select sum(hours) totalHours from resource
UNION ALL
select sum(hours) totalHours from projects-time
) s
As a sidenote, the tablename projects-time
must be delimited to avoid syntax error. Delimiter symbols vary on RDBMS you are using.
sum two select statements in sql
Because the records come from the same table, a UNION
is superfluous.A simple IN
or OR
is sufficient:
select sum(amount) as hr from friday.dbo.FMDR_Friday_CSV
where [Account #] in (4060, 4061)
select sum(amount) as hr from friday.dbo.FMDR_Friday_CSV
where [Account #] = 4060 OR [Account #] = 4061)
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 add results of two select commands in same query and from same table - Oracle SQL
[edit]
This one is cleaner :
SELECT
RE.IDREUNIONFALLA as "IdReunion",
NVL(t1.pozos,0) as "PlanificadosFallas",
NVL(t2.pozos,0) as "PlanificadosPozos",
(NVL(t1.pozos,0)s + NVL(t2.pozos,0)) AS "Total"
FROM REUNIONFALLA RE
LEFT JOIN (
select IDREUNIONFALLA, count(1) as pozos
from REUNION_FALLAPREMATURA
group by IDREUNIONFALLA
) t1
ON t1.IDREUNIONFALLA = RE.IDREUNIONFALLA
LEFT JOIN (
select IDREUNIONFALLA, count(1) as pozos
from REUNION_FALLAPOZOS_AD
group by IDREUNIONFALLA
) t2
ON t2.IDREUNIONFALLA=RE.IDREUNIONFALLA
As you can see, you make the count a single time and not for each line : should run faster if you have a big number of line.
This should do it :
SELECT
t1.IdReunion,
t1.PlanificadosFallas,
t1.PlanificadosPozos,
(t1.PlanificadosFallas + t1.PlanificadosPozos) AS "Total"
FROM (
SELECT
RE.IDREUNIONFALLA as "IdReunion",
NVL((select count(1) as pozos from REUNION_FALLAPREMATURA where IDREUNIONFALLA=RE.IDREUNIONFALLA group by IDREUNIONFALLA),0) as "PlanificadosFallas",
NVL((select count(1) as pozos from REUNION_FALLAPOZOS_AD where IDREUNIONFALLA=RE.IDREUNIONFALLA group by IDREUNIONFALLA),0) as "PlanificadosPozos"
FROM REUNIONFALLA RE
) AS t1
combining results of two select statements
You can use a Union.
This will return the results of the queries in separate rows.
First you must make sure that both queries return identical columns.
Then you can do :
SELECT tableA.Id, tableA.Name, [tableB].Username AS Owner, [tableB].ImageUrl, [tableB].CompanyImageUrl, COUNT(tableD.UserId) AS Number
FROM tableD
RIGHT OUTER JOIN [tableB]
INNER JOIN tableA ON [tableB].Id = tableA.Owner ON tableD.tableAId = tableA.Id
GROUP BY tableA.Name, [tableB].Username, [tableB].ImageUrl, [tableB].CompanyImageUrl
UNION
SELECT tableA.Id, tableA.Name, '' AS Owner, '' AS ImageUrl, '' AS CompanyImageUrl, COUNT([tableC].Id) AS Number
FROM
[tableC]
RIGHT OUTER JOIN tableA ON [tableC].tableAId = tableA.Id GROUP BY tableA.Id, tableA.Name
As has been mentioned, both queries return quite different data. You would probably only want to do this if both queries return data that could be considered similar.
SO
You can use a Join
If there is some data that is shared between the two queries. This will put the results of both queries into a single row joined by the id, which is probably more what you want to be doing here...
You could do :
SELECT tableA.Id, tableA.Name, [tableB].Username AS Owner, [tableB].ImageUrl, [tableB].CompanyImageUrl, COUNT(tableD.UserId) AS NumberOfUsers, query2.NumberOfPlans
FROM tableD
RIGHT OUTER JOIN [tableB]
INNER JOIN tableA ON [tableB].Id = tableA.Owner ON tableD.tableAId = tableA.Id
INNER JOIN
(SELECT tableA.Id, COUNT([tableC].Id) AS NumberOfPlans
FROM [tableC]
RIGHT OUTER JOIN tableA ON [tableC].tableAId = tableA.Id
GROUP BY tableA.Id, tableA.Name) AS query2
ON query2.Id = tableA.Id
GROUP BY tableA.Name, [tableB].Username, [tableB].ImageUrl, [tableB].CompanyImageUrl
JOIN two SELECT statement results
SELECT t1.ks, t1.[# Tasks], COALESCE(t2.[# Late], 0) AS [# Late]
FROM
(SELECT ks, COUNT(*) AS '# Tasks' FROM Table GROUP BY ks) t1
LEFT JOIN
(SELECT ks, COUNT(*) AS '# Late' FROM Table WHERE Age > Palt GROUP BY ks) t2
ON (t1.ks = t2.ks);
Sum results from two select statements
Try this
SELECT PerceptionistID, SSNLastFour, SUM(CommissionPay) CommissionPay,
SUM(PTOPay) PTOPay, SUM(HolidayPay) HolidayPay, SUM(Overtime) Overtime, SUM(TotalPay) TotalPay
FROM
(
SELECT PerceptionistID, SSNLastFour, CommissionPay,
PTOPay, HolidayPay, Overtime, TotalPay
FROM [dbo].fnCalculateCommissionForWeekOf(@MondayOfCurrentWeek)
UNION ALL
-- Need to get the following week's data and sum the two together
SELECT PerceptionistID, SSNLastFour, CommissionPay,
PTOPay, HolidayPay, Overtime, TotalPay
FROM [dbo].fnCalculateCommissionForWeekOf(@MondayOfFollowingWeek)
) t
GROUP BY PerceptionistID, SSNLastFour
Sum results from two different queries (SQL)
You could union all
each counting query in a derived table/subquery like so:
select
origin
, Received = sum(ReceivedCount)
, Sent = sum(SentCount)
, Total = sum(ReceivedCount)+sum(SentCount)
from (
select origin, ReceivedCount = count(*), SentCount=0
from bd.received
group by origin
union all
select origin, ReceivedCount = 0, SentCount=count(*)
from db.sent
group by origin
) s
group by origin
How to merge the result of two select mysql query
You can use either LEFT JOIN, RIGHT JOIN or JOIN depending on what you are aiming to get,
SELECT *
FROM ( select GEAR,count(GEAR)
from new_failure
where STN_CODE = "BVH" group by(Gear) AS A
JOIN ( select GEAR,count(GEAR)
from new_failure
where STN_CODE = "BVH"
and MONTH(fail_time) = 4 AS B
ON A.orders_id=B.orders_id
or you can refer to this link for a similar question
joining two select statements
How to combine two select query with different columns
Consider your first query as Query1
and second query as Query2
, you can use simple join between these two.
As you said PatientAppointment
table is common in both, use its primary key(CDRId
) as joining between these two. So your query would look like.
SELECT *
FROM ( Query1 ) AS table1
INNER JOIN ( Query2) AS table2 ON table1.CDRId = table2.CDRId;
Related Topics
Why Can't I Use "Create Schema" in a Begin/End Block in SQL Management Studio
How to Add a Variable Number of Hours to a Date in Postgresql
How to Select the Comparison of Two Columns as One Column in Oracle
When to Use an Enum or a Small Table in a Relational Database
Postgresql 9.1: How to Concatenate Rows in Array Without Duplicates, Join Another Table
How to Rollback an Update Query in SQL Server 2005
Do You Put Your Database Static Data into Source-Control? How
Postgresql: Between with Datetime
Select Multiple (Non-Aggregate Function) Columns with Group By
SQL Merge Tables Side-By-Side with Nothing in Common
How to Convert a Bunch of Boolean Columns to a Single Bitmap in Postgresql
Rename a Constraint in SQL Server
Referencing Outer Query's Tables in a Subquery
Drop All Temporary Tables for an Instance
Postgresql: Not in Versus Except Performance Difference (Edited #2)