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
Related
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
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
How do you count the total number of entries by month?
id, date
01, 2010-09-28
02, 2010-03-28
03, 2010-09-28
04, 2010-03-28
For example. How could you pick only dates from September and have them count as an output of 2?
Edit: Edited the dates to put them in the right format.
select month(date) , count(*)
from yourtable
group by month(date)
Still new to MySQL and wanted to test my own tables. Sorry to cause any trouble.
Here's the simple solution to my problem:
SELECT COUNT(DATE)
FROM TEST_TABLE
WHERE month(DATE) = 9;
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
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