Merging (or union) two result set with MySQL - mysql

I have two seperate MySQL queries which return two set of results that look like this
Query 1 result:
country | buyers | payment | num_of_sales
UK | 5 | 106.45 | 4
Thailand | 6 | 250.10 | 3
and:
Query 2 result:
country | buyers | payment | num_of_sales
UK | 2 | 150.00 | 1
Norway | 9 | 310.80 | 2
All I need is to merge / union them so the final result will look like this:
Expected result
country | buyers | payment | num_of_sales
UK | 7 | 256.45 | 5
Thailand | 6 | 250.10 | 3
Norway | 9 | 310.80 | 2
Please help, and if possible, with a bit of explaination. Thank you!

Use UNION ALL to merge the two queries results' sets into one result set, then use GROUP BY country with SUM to get the totals you are looking for, something like this:
SELECT
country,
SUM(buyers) AS buyers,
SUM(payment) AS payment,
SUM(num_of_sales) AS num_of_sales
FROM
(
SELECT country, buyers, payment, num_of_sales
FROM -- your query1 results
UNION ALL
SELECT country, buyers, payment, num_of_sales
FROM -- your query2 result2
) AS t
GROUP BY country

Related

GROUP BY according to different conditions for each column

I have the table "Tcustomer":
+--------------+----------+---------+------------------+---------------+
| CustomerName | Product | Country | Submitted_apps | Finished_apps |
+--------------+----------+---------+------------------+---------------+
| Customer 1 | book1 | CZ | 2020-11-06 | 2020-11-06 |
| Customer 2 | book1 | SK | 2020-11-06 | 2020-11-07 |
| Customer 3 | book2 | CZ | 2020-11-06 | null |
+--------------+----------+---------+------------------+---------------+
Now, I need to have one output that will say to me, how many submitted_apps was in each day, how many finished_apps was in each day per the product and the Country.
So in the output, something like this:
+----------+---------+----------+------------------+---------------+
| Product | Country | Date | Submitted_apps | Finished_apps |
+----------+---------+----------+------------------+---------------+
| book1 | CZ |2020-11-06| 1 | 1 |
| book2 | CZ |2020-11-06| 1 | 0 |
| book1 | SK |2020-11-06| 1 | 0 |
| book1 | SK |2020-11-07| 0 | 1 |
+----------+---------+----------+------------------+---------------+
I did this, but it summarize only the date "submitted_apps". If I add also "finished_apps" to the GROUP BY statement, I have a lot of rows with nulls there.
My SQL code is this:
SELECT
COUNT (Submitted_apps) AS number_submitted_per_day,
COUNT (Finished_apps) AS number_finished_per_day,
Submitted_apps AS date
WHERE Submitted_apps > 2020-01-01
GROUP BY product, country, submitted_apps
But this SQL query tells me now, how many submitted and finished applications for the books was only for the date mentioned in "submitted_apps". But I need number of Finished_apps to the date "Finished_apps" and number of Submitted_apps" to the date "Submitted_apps".
Can you please help me?
You can unpivot the data using union all and then aggregate:
select Product, Country, date, sum(submitted) as num_submitted,
sum(finished) as num_finished
from ((select Product, Country, Submitted_apps as date, 1 as submitted, 0 as finished
from Tcustomer
) union all
(select Product, Country, finished_apps as date, 0 as submitted, 1 as finished
from Tcustomer
)
) c
group by Product, Country, date;

MySQL query to sum 2 rows

I'm not a specialist in MySQL and need some help with a query.
table (t_teams)
team | group
Germany | A
Russia | B
Danmark | A
Japan | C
.....
table (t_matches)
id | fk_team1 | fk_team2 | points_team1 | points_team2
1 | Germany | Russia | 3 | 0
2 | Danmark | Japan | 3 | 0
3 | Germany | Japan | 3 | 0
4 | Russia | Danmark | 1 | 1
Foreign keys are:
fk_team1
fk_team2
Now, I want to sum all points in t_matches for each team by a mysql query.
The final result should looks like
**team | points**
Germany | 6
Russia | 1
Danmark | 1
Japan | 0
I tried something like this, but it doesn't work :-(
SELECT
t1.team,
SUM(t2.points_team1) + SUM(t2.points_team2) AS points
FROM t_teams t1, t_matches t2
WHERE (t1.team = t2.fk_team1) OR (t1.team = t2.fk_team2)
Would be great when someone can help me with this simple query.
You need to unpivot the data and then aggregate the results:
select team, sum(points)
from ((select fk_team1 as team, points_team1 as points from t_teams t
) union all
(select fk_team2 as team, points_team2 as points from t_teams t
)
) t
group by team;

SQL query SUM() AND GROUP BY

I have a MySQL table like this:
acco_id | room_id | arrival | amount | persons | available
1 | 1 | 2015-19-12 | 3 | 4 | 1
1 | 2 | 2015-19-12 | 1 | 10 | 1
1 | 1 | 2015-26-12 | 4 | 4 | 1
1 | 2 | 2015-26-12 | 2 | 10 | 1
2 | 3 | 2015-19-12 | 2 | 6 | 0
2 | 4 | 2015-19-12 | 1 | 4 | 1
What im trying to achieve is a single query with a result like:
acco_id | max_persons_available
1 | 22
2 | 4
I tried using a GROUP BY accommodation_id using a query like:
SELECT
accommodation_id,
SUM(amount * persons) as max_persons_available
FROM
availabilities
WHERE
available = 1
GROUP BY
accommodation_id
Only now the result of acco_id uses all arrival dates. When I add arrival to the query no more unique acco_id's.
Does anyone know a good Single SQL which can use the table indexes?
If I'm understanding the question correct (the last part is a bit confusing). You want to have the accomodation id and numbers as you have now but limited to specific arrival dates.
If so the following statement should do exactly that as it is not necessary to put arrival into the select if you "just" use it in the where statement. As else you would need to put it into the group by and thus have non unique accomodation id's.
SELECT
accommodation_id,
SUM(amount * persons) as max_persons_available
FROM
availabilities
WHERE
available = 1 and arrival >= '2015-12-19' and arrival < '2015-10-26'
GROUP BY
accommodation_id
I guess (reading your question) what you are looking for is this but im not sure as your question is a bit unclear:
SELECT
accommodation_id,
arrival,
SUM(amount * persons) as max_persons_available
FROM
availabilities
WHERE
available = 1
GROUP BY
accommodation_id, arrival

MySQL select for global sales report

I have a web app in .Net with Reportviewer and I use as sourcedata of reportviewer MySql query, I need to make a global sales report I have this Table sales:
|IdSale|Number|Date|Price| etc...
(The important part of my problem is the next, i need to calculate the sum of price for each row with the same Number. Why? Well the Number datafield is the receipt or bill, so I can have one sale for the receipt / bill number 6 and i selled 2 items:
|IdSale|Number| Date |Price|
| 1 | 6 |20/08 | 50€|
| 2 | 6 |20/08 | 10€|
----------------------------
So i need to get the sum of that but, with the others Numbers i mean i can do that with this query:
Select distinct sales.Number, sales.Date, SUM(sales.Price) as 'Importe',
from sales where number = 6;*
and return
|Number|Date|Importe(price)|
| 6 |20/8| 100€ |
That's ok but when i have this
|IdSale|Number| Date |Price|
| 1 | 6 |20/08 | 50€|
| 2 | 6 |20/08 | 10€|
| 3 | 7 |20/08 | 30€|
| 4 | 8 |20/08 | 20€|
----------------------------
I neet to get this output
|Number|Date|Importe(price)|
| 6 |20/8| 100€ |
| 7 |20/8| 30€ |
| 8 |20/8| 20€ |
But the only thing i made is using this query
Select distinct sales.Number, sales.Date, SUM(sales.Price) as 'Importe',
from sales where number >= 0;
|Number|Date|Importe(price|
| 2 |20/8| 150€ |
So im getting the sum of all but i just need the sum of each row with the same Number can i do this?
Are you just looking for group by?
Select s.Number, s.Date, SUM(s.Price) as Importe,
from sales s
group by s.Number, s.Date;

select sum of top 10 rows grouped by location column

I have a table of sales paired to the employee that sold it and at which location.
+---------------+----------------------+-----------+-------------+
| Units | location | name | mnt |
+---------------+----------------------+-----------+-------------+
| 5 | abc | bob | 2014-03-01 |
| 3 | abc | tim | 2014-03-01 |
| 4 | xyz | paul | 2014-03-01 |
| 1 | nyc | joe | 2014-03-01 |
+---------------+----------------------+-----------+-------------+
I want to get the stores with the highest sales (sum of units). The query should return the top 10 stores, with the units they sold ordered descending.
I tried this but only got 1 row returned and that too looks wrong.
SELECT * FROM myTable WHERE region='NE' ORDER BY SUM(units) LIMIT 10
FYI: there are additional columns in the table that i have omitted as they dont add much value to the question. One such column is the region column that is in the where clause.
try this
SELECT SUM(units), myTable.* FROM myTable GROUP BY location ORDER BY SUM(units) DESC LIMIT 10
Something like this:
SELECT location, COUNT(Units) FROM myTable WHERE region='NE' GROUP BY location ORDER BY COUNT(Units) LIMIT 10