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)
Related
I expect this query to give me the avg value from daily active users up to date and grouped by month (from Oct to December). But the result is 164K aprox when it should be 128K. Why avg is not working? Avg should be SUM of values / number of current month days up to today.
SELECT sq.month_year AS 'month_year', AVG(number)
FROM
(
SELECT CONCAT(MONTHNAME(date), "-", YEAR(DATE)) AS 'month_year', count(distinct id_user) AS number
FROM table1
WHERE date between '2020-10-01' and '2020-12-31 23:59:59'
GROUP BY EXTRACT(year_month FROM date)
) sq
GROUP BY 1
Ok guys thanks for your help. The problem was that on the subquery I was pulling the info by month and not by day. So I should pull the info by day there and group by month in the outer query. This finally worked:
SELECT sq.day_month, AVG(number)
FROM (SELECT date(date) AS day_month,
count(distinct id_user) AS number
FROM table_1
WHERE date >= '2020-10-01' AND
date < '2021-01-01'
GROUP BY 1
) sq
GROUP BY EXTRACT(year_month FROM day_month)
Do not use single quotes for column aliases!
SELECT sq.month_year, AVG(number)
FROM (SELECT CONCAT(MONTHNAME(date), '-', YEAR(DATE)) AS month_year,
count(distinct id_user) AS number
FROM table1
WHERE date >= '2020-10-01' AND
date < '2021-01-01'
GROUP BY month_year
) sq
GROUP BY 1;
Note the fixes to the query:
The GROUP BY uses the same columns as the SELECT. Your query should return an error (although it works in older versions of MySQL).
The date comparisons have been simplified.
No single quotes on column aliases.
Note that the outer query is not needed. I assume it is there just to illustrate the issue you are having.
I have a table that has a session name and session date, ex:
Name: Football game, date: 2020-05-31.
Is there a way to display these results in a group by date as in months?? ex:
Date X
1 (month 1) ...
2 (month 2) ...
...
SELECT Date(?) , count(*) nb from Session group by Date(?)
Please use below query,
SELECT month(Date) , count(*) nb from Session group by month(Date);
If you want to group by month, you can use date functions:
select year(date), month(date), count(*)
from t
group by year(date), month(date);
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
Basically I have a table like this:
Table Time:
ID.......Date
1......08/26/2016
1......08/26/2016
2......05/29/2016
3......06/22/2016
4......08/26/2015
5......05/23/2015
5......05/23/2015
6......08/26/2014
7......04/26/2014
8......08/26/2013
9......03/26/2013
The query should return like this
Year........CountNum
2016........4
2015........3
To find out which year does its value tend to increase in. I notice that I want to display the years that have more values (number of row in this case) than the previous year.
What I've done so far
SELECT Year, count(*) as CountNum
FROM Time
GROUP BY Year
ORDER BY CountNum DESC;
I don't know how to get the year from date format. I tried year(Date) function, but I got Null data.
Please help!
It should works fine.
select year(date), count(*) as countNum
from time
group by year(date)
order by countNum
Join the grouped data to itself with 1 year offset:
select
a.*
from
(
select year(`Date`) as _year, count(*) as _n
from time group by 1
) a
left join
(
select year(`Date`) as _year, count(*) as _n
from time group by 1
) b
on a._year = b._year-1
where a._n > b._n
order by 1
I have a table property_viewers with 3 fields; id, property_id, date. I need to find maximum property_id repeating in last week. How shall i proceed...
Try this:
select *, count(1) from property_viewers where DATEDIFF(date, DATE(now()))<8 group by property_id order by count(1) desc
select property_id, count(property_id) from property_viewers
where `date` between 'weekSTartDate' and 'weekEndDate'
group by property_id order by count(property_id) desc;
SELECT property_id
FROM property_viewers
WHERE date BETWEEN
CURRENT_DATE() - INTERVAL (7+WEEKDAY(CURRENT_DATE())) DAY AND //Monday last week
CURRENT_DATE() - INTERVAL (1+WEEKDAY(CURRENT_DATE())) DAY //Sunday last week
GROUP BY property_id
ORDER BY COUNT(property_id) DESC