Select last n record within group by year month - mysql

I have a table where I need to get the last 12 records which is grouped by year() month() ASC. I try to use the query below but the result is not as expected.
SELECT * FROM
(
SELECT
id,
tanggal,
date_format(tanggal,'%b-%Y') as bulan,
sum(sisa_pokok) as jumlah
FROM transaksi_detail
GROUP BY date_format(tanggal,'%b-%Y')
ORDER BY id DESC LIMIT 12
) sub
ORDER BY id ASC
the query result is as below
My expected result is sort by bulan column order by year(), month() as follows
Bulan jumlah
Mar-2018 26600000
Oct-2017 1000000
Sept-2017 4500000
and so on....

EXTRACT(YEAR FROM bulan) as year
SELECT EXTRACT(YEAR FROM tanggal) as year , EXTRACT(MONTH FROM tanggal) as month, id FROM table_name group by year order by month
you can get year same like you can get month after that put group by and order i hope it will help you

This works for my situation
SELECT * FROM
(
SELECT
id,
tanggal,
month(tanggal),
year(tanggal),
date_format(tanggal,'%b-%Y') as bulan,
sum(sisa_pokok) as jumlah
FROM transaksi_detail
GROUP BY date_format(tanggal,'%b-%Y')
ORDER BY id DESC LIMIT 12
) sub
ORDER BY year(tanggal), month(tanggal) ASC

Related

Group data by Year and Month

I have a table with 4 columns:
1. customerID
2. dateAdded
3. productID
4. quantity
The format of dateAdded is like this: 20180730 (YearMonthDay).
I want to group the rows in the table by year and month.
I tried the code below but it doesn't work. I still see rows with the same year and month repeated, but different day.
SELECT dateAdded
, SUM(quantity)
FROM testTable
GROUP
BY DATE_FORMAT(dateAdded, '%Y%m')
, dateAdded
ORDER
BY dateAdded DESC
Any idea how to fix this?
Thank you
Do not group by what you do not want the data to be grouped. Since you provide dateAdded as a parameter to be grouped with, it splits the data by dateAdded and you gain nothing out of year-month grouping. Remove the columns from select/groupby that you use for grouping like this:
SELECT DATE_FORMAT(dateAdded, '%Y%m')
, SUM(quantity)
FROM testTable
GROUP BY
DATE_FORMAT(dateAdded, '%Y%m')
If you store dateAdded as 20180730, looks like it's a string, so it wont work with DATE_FORMAT function, you can use SUBSTRING instead
something like
SELECT dateAdded, SUM(quantity), SUBSTRING(dateAdded, 1, 6) as d
FROM testTable
GROUP BY d
ORDER BY dateAdded DESC
use year and month function
SELECT Year(dateAdded) as YearOfDate,Month(dateAdded) as MonthOfdate,
, SUM(quantity) as Qty
FROM testTable
GROUP
BY Year(dateAdded), Month(dateAdded)
ORDER BY dateAdded DESC

Mysql query - GROUP BY and show the highest COUNT(*)

I'm doing a small project and I'm trying to get the restaurant with the most votes each week to be displayed once.
This is my query:
SELECT votedRestaurant,
week,
COUNT(*)
FROM mylunch.votes
GROUP BY votedRestaurant,
week
ORDER BY week DESC;
This gets me the following result:
I would only like to have the one with the highest COUNT(*) displayed per week.
Thanks for any help.
you can try use LIMIT and ordering by count, for example
SELECT votedRestaurant,
week,
COUNT(*) AS tcount
FROM mylunch.votes
GROUP BY votedRestaurant,
week
ORDER BY tcount DESC
LIMIT 1;
Also, you can use subquery, so says the documentation.
Using Mysql 8 you can make use of window functions
WITH
cte AS (SELECT votedRestaurant, WEEK, COUNT(*) total
FROM votes
GROUP BY votedRestaurant,WEEK
ORDER BY WEEK DESC, votedRestaurant)
SELECT *
FROM (
SELECT *,
row_number() over (PARTITION BY WEEK ORDER BY total DESC) AS rn
FROM
cte
) t
WHERE rn = 1
Demo
Another way would be by using string fuctions
SELECT t.week, SUBSTRING_INDEX(GROUP_CONCAT(t.votedRestaurant ORDER BY t.total DESC),',',1) votedRestaurant, MAX(t.total)
FROM(
SELECT votedRestaurant, WEEK, COUNT(*) total
FROM votes
GROUP BY votedRestaurant,WEEK
ORDER BY WEEK DESC
) t
GROUP BY t.week
ORDER BY t.week DESC
Demo

Calculate Sum grouping by day

SELECT *,
DATE(date) AS post_day
FROM notes
WHERE MONTH(date) = '08'
AND userid = '2'
AND YEAR(date) = '2016'
ORDER BY post_day DESC,
timestamp ASC
In this query I'm grouping my posts by day.
What I'm struggling with is calculating the total word count of all notes for each day. There is a word count column which contains the word count for each post. Is it possible to calculate this sum in the same query or does it need to be made separately?
By table columns:
NoteID UserID Date Note WordCount
SELECT date, SUM(wordCount) AS monthWordCount
FROM Notes
WHERE userID = 2
AND MONTH(date) = '08'
AND YEAR(date) = '2016'
GROUP BY userID, date
Check out this demo using the above code.
If you want to return the notes as well you can do a subquery like below:
SELECT noteID, userID, date, note, wordCount,
(SELECT SUM(wordCount)
FROM Notes
WHERE userID = a.userID
AND date = a.date
GROUP BY userID) AS dayTotalWordCount
FROM Notes a
WHERE a.userID = 102
AND MONTH(date) = '08'
AND YEAR(date) = '2016'
Here's a demo using the above code.
First, don't use select * with a group by query. select * just doesn't make sense with aggregation . . . you need to apply aggregation functions.
I assume that you want something like this:
SELECT DATE(date) as post_day, SUM(WordCount)
FROM notes
WHERE MONTH(date)= '08' AND userid = '2' AND YEAR(date)= '2016'
GROUP BY DATE(date)
ORDER BY post_day DESC, timestamp ASC

MySQL count and group by day

I have the following structure
ID DATE(DATETIME) TID
1 2012-04-01 23:23:23 8882
I'm trying to count the amount of rows and group them by each day of the month that matches TID = 8882
Thanks
You can group using the DAY function:
SELECT DAY(Date), COUNT(*)
FROM table
WHERE TID = 8882
GROUP BY DAY(Date)
Not sure exactly what you mean by day of the month -- do you want to group the 1st of Feb with the 1st of March? Or do you just mean date? Assuming the latter, how about this:
SELECT DATE(date) as d,count(ID) from TABLENAME where TID=8882 GROUP by d;
Try this query:
SELECT COUNT(id), DAY(dat), MONTH(dat), YEAR(dat)
FROM table
WHERE TID=8882
GROUP BY YEAR(dat), MONTH(dat), DAY(dat);
Try this:
SELECT DAY(date) AS `DAY`, COUNT(1) AS `COUNT` FROM
table1
WHERE TID = 8882
GROUP BY DAY(date)
What about MySQL Query GROUP BY day / month / year
count for every date:
SELECT date(created_at), COUNT(*)
FROM message_requests
GROUP BY date(created_at)

Most common hour query?

I have this table:
ID(INT) DATE(DATETIME)
Under the DATE column there are a lot of different dates, and I want to figure out the most common hour between all the rows of the table, regardless of the day.
How can I do that with a MySQL query?
SELECT HOUR(date) AS hr, COUNT(*) AS cnt
FROM yourtable
GROUP BY hr
ORDER BY cnt DESC
LIMIT 1
relevant docs: http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_hour
Try this -
SELECT HOUR(`DATE`) AS `hour`, COUNT(*)
FROM `table`
GROUP BY `hour`
You could do a query like:
SELECT COUNT(daterow) AS occurrences FROM table GROUP BY daterow ORDER BY occurrences DESC LIMIT 1;
SELECT COUNT( id ) , HOUR( date )
FROM test
GROUP BY HOUR( date )
ORDER BY COUNT( id ) DESC
LIMIT 1