halo i want to count the data and group by month (month start date) then show in date time, any idea ?
table_a
timestamp
2020-11-28 04:00:00
2020-11-28 05:00:00
2020-12-29 01:00:00
2020-12-29 02:00:00
2020-12-29 03:00:00
expected result:
timestamp count
2020-11-01 00:00:00 2
2020-12-01 00:00:00 3
my query is:
SELECT STR_TO_DATE(DATE_FORMAT(timestamp, '%Y-%m-dd HH'), '%Y-%m-dd HH'), count(*) as count
from table_a
but my results was:
timestamp count
2020-11-00 2
2020-12-00 3
You can use:
select str_to_date(concat_ws('-', year(timestamp), month(timestamp), 1)) as yyyymm,
count(*)
from table_a
group by yyyymm;
i fixed the question with add group by month(timestamp) , i m ignored the minuted, the important things is group by month , here are my full query :
SELECT timestamp, count(*) as count
from table_a
group by month(timestamp)
then the output show :
timestamp count
2020-11-01 04:00:00 2
2020-12-01 01:00:00 3
Related
I have a datetime field in my db. Now i want to select upcoming dates from NOW() based on the dates. If two datetimes is the same I want to select them. And if it's only one I want to select that date. Limit is that I never want to select more than two rows.
So if NOW() is 2021-06-18 12:00:00 row number 1 should be selected.
If NOW() is 2021-06-20 18:15:00 row number 3 and 4 should be selected.
1 2021-06-19 18:00:00
2 2021-06-20 18:00:00
3 2021-06-21 15:00:00
4 2021-06-21 15:00:00
5 2021-06-21 18:00:00
6 2021-06-21 18:00:00
I've tried
SELECT gamedate
FROM games
WHERE gamedate > NOW()
ORDER BY gamedate LIMIT 0 , 1
but that doesn't make any sense or what I want to do.
Use a subquery to get the next gamedate. Then use that in the main query to select at most two rows with that date.
SELECT id, gamedate
FROM games AS g
WHERE gamemedate = (SELECT MIN(gamedate) FROM games WHERE gamedate > NOW())
ORDER BY id
LIMIT 2
How can we query a set of records to get data for particular dates where there might be gaps in dates.
Example Data
date | Price
------------------
2018-03-31 | 115
2018-03-29 | 114
2018-03-28 | 113
...
2017-03-29 | 117
2017-03-28 | 118
...
2016-12-30 | 143
2016-12-29 | 140
...
2015-12-31 | 110
2015-12-30 | 111
Required Data for dates: 2018-03-31, 2017-03-31, 2016-12-31, 2015-12-31
date | Price
------------------
2018-03-31 | 115
2017-03-31 | 117
2016-12-31 | 143
2015-12-31 | 110
You can do this with correlated sub query. The following will return the price for the exact date or the closest prior date:
SELECT dates.dt, (
SELECT price
FROM t
WHERE date <= dates.dt
ORDER BY date DESC
LIMIT 1
) AS price
FROM (
SELECT '2018-03-31' AS dt UNION ALL
SELECT '2017-03-31' UNION ALL
SELECT '2016-12-31' UNION ALL
SELECT '2015-12-31'
) AS dates
Demo on db<>fiddle
You can use MySQL Last day function and date_format to achieve the desired result. Check out the query :-
select last_day(a11.d_date), a11.price
from test a11
join
(select MAX(d_date) d_date, DATE_FORMAT(d_date, "%M %Y")
from test
group by DATE_FORMAT(d_date, "%M %Y")
) a12
on a11.d_date = a12.d_date
SQL Fiddle
seem you need the max price for year
select max(date), max(price)
from my_table m
inner join (
select max(date), year(date) my_year
from my_table
group by year(date)
) t on t.my_year = year(m.date)
group by year(m.date)
I have a table like this:
Date Name Qty
2016-09-13 00:00:00 John 2
2016-09-15 00:00:00 Matt 3
2016-09-21 00:00:00 Rich 1
2016-09-23 00:00:00 Matt 1
2016-10-05 00:00:00 John 1
2016-10-07 00:00:00 Matt 3
2016-10-12 00:00:00 Rich 0
2016-10-23 00:00:00 Matt 2
How can I do, using MySQL, to retrieve the addition of all the Qty values that corresponds to the same month and place that info on a view?
SELECT sum(Qty) as sum, month(date) as month, year(date) as year FROM table_name GROUP BY month(date), year(date)
will return
sum month year
4 12 2015
10 12 2016
What you probably want could be one of the following queries:
SELECT YEAR(`Date`) AS yr, MONTH(`Date`) AS mnt, SUM(Qty) AS Qty
FROM `table1`
GROUP BY YEAR(`Date`), MONTH(`Date`)
or
SELECT EXTRACT(YEAR_MONTH FROM `Date`) AS mnt, SUM(Qty) as Qty
FROM `table1`
GROUP BY EXTRACT(YEAR_MONTH FROM `Date`)
This query should produce something like this:
mnt | qty
---------+-----
2016-09 | 7
2016-10 | 6
The MySQL function EXTRACT() is able to return only some components of a date.
I need to calculate total number of users "aggregated", day by day, ex:
table users:
id create_date
0 2016-09-01 00:00:00
1 2016-09-01 00:00:00
2 2016-09-01 00:00:00
3 2016-09-02 00:00:00
4 2016-09-02 00:00:00
5 2016-09-02 00:00:00
6 2016-09-03 00:00:00
7 2016-09-03 00:00:00
8 2016-09-04 00:00:00
9 2016-09-04 00:00:00
using the following query:
select date(u.create_date),count(u.id)
from user u
group by date(u.create_date)
returns:
date(u.create_date) count(u.id)
2016-09-01 3
2016-09-02 3
2016-09-03 2
2016-09-04 2
but I need to return data aggregated like this:
date(u.create_date) count(u.id)
2016-09-01 3
2016-09-02 6
2016-09-03 8
2016-09-04 10
thx,
Note: table key "id" has holes, (Non-sequentially).
You want a cumulative sum. In MySQL, this is probably easiest using variables:
select dte, cnt, (#c := #c + cnt) as running_cnt
from (select date(u.create_date) as dte, count(u.id) as cnt
from user u
group by date(u.create_date)
) d cross join
(select #c := 0) params
order by dte;
Note: When using aggregation with variables, I find that the subquery is necessary.
You could also do:
select d.dte,
(select count(*)
from users u
where u.create_date < date_add(u.dte, interval 1 day)
) as running_cnt
from (select distinct date(u.create_date) as dte from user u) d;
For small amounts of data, this is fine performance-wise.
I am stuck at a query to compare different times on same table and list one of the common and all uncommon time.
ID ID_STORE Opentime Closetime
---------- -------- ---------- -------------------------------
1 6 09:30:00 18:00:00
2 6 09:30:00 18:00:00
3 6 09:30:00 18:00:00
4 6 09:30:00 18:00:00
5 6 09:30:00 16:00:00
6 6 10:30:00 15:00:00
Like In this table the output should be like
ID ID_STORE Opentime Closetime
---------- -------- ---------- -------------------------------
4 6 09:30:00 18:00:00
5 6 09:30:00 16:00:00
6 6 10:30:00 15:00:00
AS I've observed, you are getting the latest ID on every unique opentime and closetime and on every store.
SELECT MAX(ID) as ID,
ID_Store,
OpenTime,
CloseTime
FROM tableName
GROUP BY ID_Store,
OpenTime,
CloseTime
From the example you give, it looks to me like all you need is a GROUP BY and decide on what aggregate function you use on the ID.
I have used MAX as it returns the result as per your example.
SELECT MAX(ID), OpenTime, CloseTime
FROM YourTable
GROUP BY
OpenTime, CloseTime
You can read up on
Aggregate functions
You need to use GROUP BY clause. And for ID you can use aggregate function like MAX() or MIN() as per your requirement.
SELECT MAX(ID) AS ID, ID_STORE, Opentime, Closetime
FROM myTable
GROUP BY ID_STORE, Opentime, Closetime