Join two queries with sum of fields as total - mysql

I have two queries to sum count all sales of products and other for services in the same database, I am wondering if it is possible to make these 2 queries to just one. Not because they are just queries but also because they sum count. I better show the example here
QUERY 1
SELECT SUM(totalone) AS `total` FROM `sold_products`
WHERE `date`=DATE(NOW()) AND `payment_method`='cash'
QUERY 2
SELECT SUM(totaltwo) AS `total` FROM `sold_services`
WHERE `date`=DATE(NOW()) AND `payment_method`='cash'
Right now I add the two values in my PHP code. It works fine but I just want to know how to do this in just one query. I always query the same payment method and the same date, but just two different tables in the same database.
Any suggestion ?

You can use a UNION and then SUM the values like this:
SELECT SUM(total) FROM (
SELECT totalone AS `total` FROM `sold_products`
WHERE `date`=DATE(NOW()) AND `payment_method`='cash'
UNION ALL
SELECT totaltwo AS `total` FROM `sold_services`
WHERE `date`=DATE(NOW()) AND `payment_method`='cash'
)
Make sure you use UNION ALL instead of UNION, otherwise you will end up discarding any duplicate values in your total column, giving you the wrong sum.

What you want is the UNION operator. You can take the two queries and get them combined as one.
SELECT SUM(total) AS `total` FROM (
SELECT `total` FROM `sold_products`
WHERE `date`='$today' AND `payment_method`='cash'
UNION
SELECT `total` FROM `sold_services`
WHERE `date`='$today' AND `payment_method`='cash')
Here is an article with more information about MySQL UNION:
http://www.mysqltutorial.org/sql-union-mysql.aspx

Related

Average after group by in SQL query

select avg(select count(aid)
from athlete
group by codepays)
I get a "more than one row error".
How with I go about getting the average of the result from my fist select ?
You need to use a table expression (subquery).
For example:
select avg(cnt)
from (
select count(aid) as cnt
from athlete
group by codepays
) x
You can do this using division:
select count(aid) / count(distinct codepay)
from athlete;
No subqueries are necessary.
(Although the arithmetic needs to be tweaked if codepay can actually be NULL.)

SQL - Union select issue, results are not described properly

My Union Select statement works, but the results are on top of each other.
SQL Statement:
SELECT Cust_ID, Cust_FirstName, Cust_LastName
FROM kmsrutge_xoxoxo.Customers
UNION
SELECT Item_Type, Quantity, Total_Amount
FROM kmsrutge_xoxoxo.Invoices
WHERE Total_Amount >= '$5.00'
You should pick (select) at least one column that is the same of both tables, and make a join statement per that column in order to get desired result.

Mysql Union in different columns

I have the following query
SELECT *
FROM(
(SELECT
MAX(c.start_time) as start_1
FROM
c1 c)
UNION ALL
(SELECT
MAX(cc.created_at) as ccmax
FROM
cc1)
) as t
I'd like to have the result in a table with 2 columns start_1 and cmax instead of the single column I get with all the different results listed.
How should I do it? I ended up in a subselect believing this would have done the job.
For the data to be in two columns you would have to use a sub select.
SELECT
MAX(c1.start_time) as start_1, (SELECT MAX(cc1.created_at) FROM cc1) as ccmax
FROM c1

order multiple tables according to one column

Is it possible, using SQL to pull data from different tables, then sort the data according to one column that is in all tables. eg, I have 3 tables. Base, Selects, Sub. They all 3 have a position column,
Select base_layers.position, selects.position, subbases.position
from base_layers,selects,subbases
Order By (alls)position;
That is exactly what I actually want to do... But have a feeling it is not possible.
Use a union:
(I'm assuming you don't want to cross join all 3 tables)
select Position
from
(
select base_layers.position AS Position
from base_layers
union
select selects.position
from selects
union
select subbases.position
from subbases
) x
order by Position ASC;
Your syntax was incorrect. Try this version.
SELECT * FROM
(
SELECT base_layers.position
FROM base_layers
UNION ALL
SELECT selects.position
FROM selects
UNION ALL
SELECT subbases.position
FROM subbases
) tbl ORDER BY tbl.position;

Exclude columns from MySQL SELECT WITH ROLLUP?

Following on from this question I posed earlier, say I want to add ID to the query:
SELECT client, ID, job_type, SUM(actual_value_fee) FROM jo2details GROUP BY client, job_type WITH ROLLUP
but I don't want MySQL to try to total the ID column as it's an identifier for the records on the table rather than something "summable". How can I exclude ID from the WITH ROLLUP please?
One way is to UNION two queries. The first query will have the id and the second query will generate the subtotals. Using your example, it would look like this:
SELECT a.id, a.client, a.job_type, sum(a.actual_value_fee)
FROM jo2details a
GROUP BY a.client, a.job_type, a.id
UNION
SELECT NULL, b.client, NULL, sum(b.actual_value_fee)
FROM jo2details b
GROUP BY b.client
WITH ROLLUP
From here, you'll have to play around with it to get the subtotals to sort correctly.