How to combine the output of two querys in MYSQL when they have non-compatible conditions

4

1

I'm looking for some assistance in adding together the output of the two following queries. They have two incompatible conditions so I'm struggling to combine them in the one query.

Total Revenue Year to Date

How to combine the output of both queries?

SELECT 
sum(datediff(returndate, dueDate)*(products.rentalfee*0.2)) AS 'Late Fees YTD'
FROM products INNER JOIN orderdetails
ON products.productID = orderdetails.productID
WHERE returndate > duedate

And

SELECT 
sum(products.RentalFee*orderdetails.quantity)
AS 'Total Revenue YTD'
FROM products INNER JOIN orderdetails
ON products.productID = orderdetails.productID
WHERE returndate > duedate OR duedate = returndate 

NiallBC

Posted 2016-04-16T16:21:42.390

Reputation: 41

Answers

0

You could try using both queries in the select clause of a third query where the table you are selecting from is DUAL. This allows one row to be returned with the results from both queries.

Example:

Query:

SELECT
(SELECT 
sum(datediff(returndate, dueDate)*(products.rentalfee*0.2)) 
FROM products INNER JOIN orderdetails
ON products.productID = orderdetails.productID
WHERE returndate > duedate) AS 'Late Fees YTD'
,(SELECT 
sum(products.RentalFee*orderdetails.quantity)
FROM products INNER JOIN orderdetails
ON products.productID = orderdetails.productID
WHERE returndate > duedate OR duedate = returndate) AS 'Total Revenue YTD'

FROM DUAL;

Result:

+---------------+-------------------+
| Late Fees YTD | Total Revenue YTD |
+---------------+-------------------+
| 3             | 4                 |
+---------------+-------------------+

Brandon

Posted 2016-04-16T16:21:42.390

Reputation: 1