Join two tables (with calculation) - mysql

Let's say I have these two tables in MySQL:
1. table_name
name
------
Max
John
Sarah
Peter
2. table_money
name | amount | month
-------------------------------
Max | 100 | January
Sarah | 90 | January
Max | 100 | February
Sarah | 90 | February
Peter | 90 | February
How has the SQL query to look, so that I can get a table like this:
name | total (amount * #month)
-------------------------------
Max | 200
Sarah | 180
Peter | 90

Try using group by keyword
SELECT table_name.name,sum(table_money.amount) as total
FROM table_name JOIN table_money
on table_name.name = table_money.name
GROUP BY table_money.name
Hope this works

Try this
SELECT name,sum(amount) Total
FROM table_money
GROUP BY name

Related

How can one group by ranges and perform aggregation in mysql?

I have a table as shown below with year and quantity of goods sold in each year, I want to group the year column into ranges of Decades and sum the quantity sold in each decade. Having in mind that the First decade is 1980 - 1989, Second decade is 1981 - 1990, so on... The expected result is also shown in the second table below
sample: expected_result:
+------+----------+ +-----------+------------+
| year | qty | | Decades | Total_qnty |
+------+----------+ +-----------+------------+
| 1980 | 2 | | 1980-1989 | 13 |
| 1981 | 1 | | 1981-1990 | 12 |
| 1983 | 8 | | 1982-1991 | 12 |
| 1989 | 2 | | 1983-1992 | 12 |
| 1990 | 1 | | . | . |
| 1992 | 1 | | . | . |
| 1994 | 4 | | . | . |
+------+----------+ +-----------+------------+
Below is the sample code I tried with a couple of others but the result is not as expected,
SELECT t.range AS Decades, SUM(t.qty) as Total_qnty
FROM (
SELECT case
when s.Year between 1980 and 1989 then '1980 - 1989'
when s.Year between 1981 and 1990 then '1981 - 1990'
when s.Year between 1982 and 1991 then '1982 - 1991'
when s.Year between 1983 and 1992 then '1983 - 1992'
else '1993 - above'
end as range, s.qty
FROM sample s) t,
group by t.range
I tried this and this but still could not get the expected result. Also I wouldn't want to hardcode things. Please any help will be appreciated.
After getting insight from xObert's answer to hank99's question I was able to work around the problem with self join as shown below. Note: The raw table contains the name of product and the year it was sold, with repeated product names and year sold. Which explains why I was able to use COUNT(*) to obtain total number of products sold in each decade. Thank you all!
SELECT year1 ||' - '|| year2 AS Decades, Count_of_qnty
FROM
(SELECT s1.year year1, s1.year+9 year2, COUNT(*) AS Count_of_qnty
FROM
(SELECT DISTINCT year FROM sample) s1
JOIN sample s2
ON s2.year>=year1 AND s2.year <= year2
GROUP BY year1)

An interesting SQL query CHALLENGE - list the AVERAGE value of the top 3 scores of each athlete

An interesting SQL query CHALLENGE:
A table named athelets consisting of id, ath_id, name, score, date.
+----+--------+-----------------+--------+------------+
| id | ath_id | name | record | r_date |
+----+--------+-----------------+--------+------------+
| 1 | 2 | John Wayne | 79 | 2010-07-08 |
| 2 | 7 | Ronald Regan | 51 | 2000-03-22 |
| 3 | 1 | Ford Harrison | 85 | 2009-11-13 |
| 4 | 2 | John Wayne | 69 | 2017-01-01 |
Please write a sql query to list the average value of the top three scores of each athlete, something like:
ath_id: 1, the arithmetic mean of his/her top 3 records: 77
ath_id: 2, the arithmetic mean of his/her top 3 records: 73
ath_id: 3, the arithmetic mean of his/her top 3 records: 47
select ath_id, avg(record)
from
(select ath_id, record
from atheletes as t1
where
(select count(*) from atheletes where t1.ath_id=ath_id and record > t1.record) < 3) as d
group by ath_id;
The above query should works as expected.
Assuming combinations of athletes and records are unique...
SELECT ath_id
, ROUND(AVG(record),2) top3_avg
FROM
( SELECT x.*
FROM athletes x
JOIN athletes y
ON y.ath_id = x.ath_id
AND y.record >= x.record
GROUP
BY x.id
HAVING COUNT(*) <=3
) a
GROUP
BY ath_id;

get sum for multiple blocks

I have a table like this:
name | day | score
------------------
John | 1 | 4
John | 2 | 5
John | 3 | 6
Marc | 1 | 7
Marc | 2 | 4
Marc | 3 | 5
Paul | 1 | 8
Paul | 2 | 2
Paul | 3 | 3
I want to get the sum of the score for each person, but only for certain days, sorted by this sum. let's say I want to get the score-sum of the 1. and 2. day, this is what I expect:
name | sum(score)
-----------------
Marc | 11
Paul | 10
John | 9
this is what failed:
SELECT name, sum(score) FROM mytable WHERE day<=2
I think I have to surround the sum(score)-part with some IF-statement, but I have no idea how.
Just add group by
SELECT name, sum(score) FROM mytable WHERE day<=2 group by name
Use sum function and group by clause for grouping the result.
query
select name,sum(score) as score
from myTable
where day in (1,2)
group by name
order by sum(score) desc;
fiddle demo

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

Count columns according to dates in SQL

I need help with a SQL statement. The goal is to count the amount of alarms of each date. My table looks something like this:
|----DATE----|---COUNTER---|---ALARM_ID---|
|2012-01-01 | 30 | 1 |
|2012-01-01 | 20 | 2 |
|2012-01-01 | 10 | 3 |
|2012-01-02 | 5 | 1 |
|2012-01-02 | 25 | 2 |
|2012-01-02 | 12 | 3 |
|2012-01-03 | 33 | 1 |
|2012-01-03 | 43 | 2 |
|2012-01-03 | 11 | 3 |
And I'm looking for a SQL statement that gives this result:
|----DATE----|---COUNTER---|
|2012-01-01 | 60 |
|2012-01-02 | 42 |
|2012-01-03 | 87 |
I've been working on this SELECT date, SUM(counter) FROM all_stats but all I get is:
|----DATE----|---COUNTER---|
|2012-01-01 | 60 |
Do I have to create a loop to go through all dates and count?
Thanks in advance, Steve-O
SELECT date, SUM(counter)
FROM all_stats
GROUP BY date
Try this instead
SELECT date, SUM(counter) FROM all_stats GROUP BY date;
"GROUP BY date" puts all the individual dates on a separate line and does the sum separately per date.
select date, sum(counter)
from all_stats
group by date