comparing with two maximum of columns get one row - mysql

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

Related

SQL: MAX of summing the values of 2 columns

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

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 - latest month/year

My SQL isn't the greatest, obviously, but what I'm trying to do is get the latest date in a database by finding the maximum year and month in an entry. Right now I have:
select max(Month), max(Year) from posts where postID = 25;
...but that results in the latest month and the latest year, though they're not part of the same entry. How can I make sure month and year are from one entry and not separate?
SELECT Month, Year FROM posts WHERE postID = 25 ORDER BY Year DESC, Month DESC LIMIT 1

MySQL sorting multiple columns with different sort order

I have a table in which I have three fields with data type date, int and bigint.
I want to sort my select query using all these three columns. I want to sort them all in descending order. For example:
Select * From mytbl
order by date desc,intnum desc, bigintnum desc;
Is it possible that i could get a result starting from max of all three columns.
like latest date, highest intnum and higest bigintnum.
no
What your query does is get the max date, followed by the max intnum of the max date followed by the max bigintnum of the max intnum of the max date
In other words, your query would not return the maximum value of all three columns
It orders by the date first, then the intnum, then the bigintnum
The results would be something like this
2011-07-20 12 14
2011-07-20 12 13
2011-07-20 11 16
2011-07-20 10 12
2011-07-19 20 15
2011-07-18 60 30
2011-07-18 50 14
It is not possible to get a result starting from max of all three columns. like latest date, highest intnum and higest bigintnum.
Select * From mytbl
order by date desc,intnum desc, bigintnum desc;
As you know what ORDER BY does, if you have multiple columns in order by clause, It will first order by DATE Desc then for the very first Date it will order by INTNUM Desc and then order by BIGINTNUM.

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`));