MySQL query - latest month/year - mysql

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

Related

Collect last 7 days data from SQL and group by days

how can I count last 7 days data from SQL and group them by day/date (excluding today).
I should be able to use the result as $resultday1, $resultday2, $resultday3 etc.
If there was 10 total SQL entries in day 1 (yesterday) $resultday1 should show "10".
and the days should be last 7 only, and today/current day should not consider.
The following PHP SQL script shows the total count only
SELECT COUNT(1) FROM orders WHERE username='jondoe'
database is a list of referrals made by a registered user in previous days.
a single table contains all user's referral details, table name "orders" as per above example.
This is the exact query as you want
SELECT
COUNT(*), DATE(order_date) order_date
FROM
orders
WHERE
order_date < CURDATE()
AND order_date > DATE_SUB(NOW(), INTERVAL 7 DAY)
GROUP BY order_date
ORDER BY order_date DESC;

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

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

Optimizing MySQL Query With Complicated Predicate

Thanks for trying to help. I have a table called posts where there is a row for each post. A post has a post_date (in unix timestamp format) and an author_id.
I am trying to get the number of unique authors that posted 5 or more times within a month grouped by month and year.
The query I have now for unique authors by month and year (without the filter for 5 or more posts within that month) is:
select
month(from_unixtime(p.post_date)) as month,
year(from_unixtime(p.post_date)) as year,
count(distinct p.author_id)
from posts p
group by month,year
order by year desc,month desc
Can you please help me add a filter that will only count authors that had 5 or more posts in that given month?
UPDATE
The following query works but is extremely slow, even when adding predicates for just this month and year. Can someone think of a better way to do it?
select
month(from_unixtime(p.post_date)) as month,
year(from_unixtime(p.post_date)) as year,
count(distinct p.author_id)
from posts p
where (
select count(*) from posts p2 where p2.author_id=p.author_id
and month(from_unixtime(p.post_date))=month(from_unixtime(p2.post_date))
and year(from_unixtime(p.post_date))=year(from_unixtime(p2.post_date))
)>=5
group by month,year order by year desc,month desc
You can use having count >5 .It will solve the problem
select month(from_unixtime(p.post_date)) as month,
year(from_unixtime(p.post_date)) as year,
count( p.author_id) c from posts p
group by author , month,year having c >5
order by year desc,month desc