SQL: MAX of summing the values of 2 columns - mysql

so I have a table "records" like this:
name month year
Rafael 9 2018
Rafael 5 2018
Rafael 10 2017
And I want to get my last records (Rafael, 9, 2018). I thought about summing the month and the year and getting the max of that sum like this:
select * from records, max(month + year) as max_date
But doesn't seem to be right. Thanks for help

Using ORDER BY clause, you can get the highest year and month combo. Try the following:
SELECT *
FROM records
ORDER BY year DESC, month DESC
LIMIT 1

Do you mean the output of the follwing?
select *
from records
order by year desc, month desc
limit 1
In general, it would be more useful to use one DATE or DATETIME column type for this purpose where you can extract year and month if you want.

Use concat if you want to concat max month and max year
Select name ,concat (concat( max(month), '-'),max(year)) from records
Group by name
but if you want just year wise max year date information then use below
Select * from records
order by year desc
limit 1
https://www.db-fiddle.com/f/sqQz1WEEAukoWEWkbxBYxe/0
name month year
Rafael 9 2018

Related

How do i sum fields inside a column?

I have a table like this: Table Name: Accounting
year
acc
value
2018
in
500
2018
out
500
2019
in
600
2019
out
800
I need to show up to 10-year slots with the highest value (i.e in + out). For example, in this case, 2019 is the highest, my query should show
year
Max Value
2019
1400
My current SQL code is:
SELECT year,acc, MAX(value) as max_value
FROM Accounting
group by year,acc
LIMIT 10
How can I get the desired result?
You need SUM() and ORDER BY:
SELECT year,acc, SUM(value) as sum_value
FROM Accounting
GROUP BY year
ORDER BY sum_value DESC
LIMIT 1;
If you want only ten years to be considered, you need a WHERE clause, for instance:
SELECT year, SUM(value) as sum_value
FROM Accounting
WHERE year >= 2010
GROUP BY year
ORDER BY sum_value DESC
LIMIT 1;
I may be misunderstanding your question, but what it appears you are attempting to complete the following steps.
You are trying to SUM by year, regardless of account type, the amount in the value column.
You are trying to show only the years with the 10 highest summed values over some specified period of time.
One way to approach this would be as follows
SELECT year, SUM(value) as annual_value
FROM Accounting
GROUP BY year
ORDER BY annual_value desc
LIMIT 10
You have to use the SUM() not the MAX(), because you don't want only the highest value, but the total, and then you only have to GROUP BY them by year
SELECT year,sum(value) as max_value
FROM Accounting
group by year
LIMIT 10;
Here's the result:
+------+-----------+
| year | max_value |
+------+-----------+
| 2018 | 1000 |
| 2019 | 1400 |
+------+-----------+

MYSQL - Can't group a summed column

I have a table with faktura_kroner (decimal 9,2), and faktura_dato (BIGINT 11, which is a unix timestamp).
I am trying to get the sum of faktura_kroner per month so I can use it in a chart, but am struggling a bit to find the correct sql query. Also it is unique by year. So October 2016 should not be grouped with October 2017 as an example.
Any help appreciated..
SELECT sum(faktura_kroner) as sum_faktura,
MONTHNAME(FROM_UNIXTIME(faktura_dato)) as month,
YEAR(FROM_UNIXTIME(faktura_dato)) as year
FROM
faktura
WHERE
user_id = 1
AND
virksomhet_id = 1
GROUP BY YEAR(FROM_UNIXTIME(faktura_dato)) DESC,
MONTH(FROM_UNIXTIME(faktura_dato)) ASC, faktura_dato DESC;
You can try below query -
SELECT sum(faktura_kroner) as sum_faktura
,MONTHNAME(FROM_UNIXTIME(faktura_dato)) as month
,YEAR(FROM_UNIXTIME(faktura_dato)) as year
FROM faktura
WHERE user_id = 1
AND
virksomhet_id = 1
GROUP BY MONTHNAME(FROM_UNIXTIME(faktura_dato)) as month
,YEAR(FROM_UNIXTIME(faktura_dato)) as year

MySQL query group month into two periods

I want to do group select in MySQL, which is group my timestamp column into 2 periods of month and count each rows. just say, if there are rows in a month in date 1, 2 ,3, and also 17,19 in Sept it would be :
Period count
Sept 1 3
Sept 2 2
Anyone can help me please?
Thanks!
I assume you have 1 column period
SELECT period, count(period) AS count FROM table_name GROUP BY period

comparing with two maximum of columns get one row

i have table test with columns as id , month, year . i need to get id where maximum of month and maximum of year in one query.
for example
id month year
1 10 2012
2 9 2013
my result should be id 2 .
first checks with year based on that maximum month based on these to i need to get id
i give query like this in MySQL
select id from book where MAX(month) and MAX(year);
its produce error
Just sort using month and year:
Demo
SELECT id
FROM books
ORDER BY year DESC, month DESC
LIMIT 1
For this type of query, order by and limit are the best approach. You seem to want the latest date, so start with the year (in descending order) and then month (in descending order):
select *
from book
order by year desc, month desc
limit 1
In particular, you don't want the maximum month and maximum year. Based on your desired results, you want the most recent/latest month.
You can just ORDER BY year, month with DESC keyword :)
SELECT id
FROM book
ORDER BY year DESC, month DESC
LIMIT 0, 1

How to count and group items by day of the week?

I have the following (MySQL) table called "tweets":
tweet_id created_at
---------------------
1 1298027046
2 1298027100
5 1298477008
I want MySQL returning the number of tweets per day of the week; taking the above data it should return:
Wednesday 1
Friday 2
I now have the following query (which should return the day of the week index, not the full name):
SELECT
COUNT(`tweet_id`),
WEEKDAY(FROM_UNIXTIME(`created_at`))
FROM tweets2
ORDER BY WEEKDAY(FROM_UNIXTIME(`created_at`))
This however returns:
COUNT(`tweet_id`) WEEKDAY(FROM_UNIXTIME(`created_at`))
7377 4
(There are a total of 7377 tweets in the database). What am I doing wrong?
SELECT
COUNT(`tweet_id`),
DAYNAME(FROM_UNIXTIME(created_at)) AS Day_Name1
FROM tweets2
GROUP BY Day_Name1
ORDER BY Day_Name1;
You have a count, but you don't have a group by. You should include a
GROUP BY WEEKDAY(FROM_UNIXTIME(`created_at`))
You are not grouping by week day so you only get one grand total. Try this:
SELECT
COUNT(`tweet_id`),
WEEKDAY(FROM_UNIXTIME(`created_at`))
FROM tweets2
GROUP BY WEEKDAY(FROM_UNIXTIME(`created_at`))
ORDER BY WEEKDAY(FROM_UNIXTIME(`created_at`));