List of records per date - mysql

I am trying to get records as per the following query :-
select distinct type, count(type), created_at from study_aids where date(created_at) = '2015-09-02'
GROUP BY type
and i get the the result in following manner
type count(type) created_at
FlashCardDeck 752 2015-09-02T15:29:34.000Z
MindMap 6692 2015-09-02T13:52:38.000Z
I need to manually go and replace date,whereas i need the query which displays results as per next dates in following way autmoatically :-
type count(type) created_at
FlashCardDeck 752 2015-09-02T15:29:34.000Z
MindMap 6692 2015-09-02T13:52:38.000Z
XYZ 1234 ****2015-09-03T13:52:38.000**Z**
PS - i don't need the sum, i need the count of each type of records for a particular date.
Thanks

Guessing this must be mysql. The query including the date of created_at would be something like this.
select type
, count(type)
, date(created_at)
from study_aids
where created_at >= '2015-09-02'
AND created_at < '2015-09-03'
GROUP BY type
, date(created_at)

Related

Get COUNT of id when GROUP BY Id and DATE

Currently, I have a table with 3 columns with id, clicks and dates. Every id has an entry with clicks for each day of a month with the format like '20180420' in the date column, let's say for 20th April 2018.
SELECT
id,
month(date),
COUNT(id)
FROM mydb
WHERE (date >= 20180101 AND date < 20180424) clicks > 0
GROUP BY id, MONTH(date);
I run this query to get if I have more than one clicks each month for this id and I want to do something like that to get the count of how many months every id has more than one clicks, but I don't get the correct result.
Am I missing something basic?
P.S: I am using MySQL 5.6
I guess the date is stored as a varchar, if so then convert string into date.Try this:
SELECT MONTH(STR_TO_DATE(A.date,'%y%m%d')) MONTH, COUNT(*) NUMBER_OF_CLICKS
FROM mydb A
WHERE STR_TO_DATE(A.date,'%y%m%d') BETWEEN STR_TO_DATE('20180101','%y%m%d') AND CURRENT_DATE
GROUP BY 1
ORDER BY 1 DESC;

MySQL: Selecting In Between DateTime records

I want to get the count of records between two date-time entries.
I have a column in my table named created_date with the datetime data type.
I want to select rows which were created between 2017-01-10 and 2017-01-30
I have written the following query but it doesn't seem to be inclusive
SELECT* FROM table WHERE created_date BETWEEN '2017-01-10' AND '2017-01-30'
The issue you are having has to do with that the date literal 2017-01-31 represents that date at midnight. To get around this, phrase your query as follows:
SELECT * FROM table WHERE created_date >= '2017-01-10' AND created_date < '2017-01-31';
This says to take any date on or after the very start of 2017-01-10 and before the start of 2017-01-31. This implies including the entire day 2017-01-30.
For get count of records between two date you can try below query
SELECT COUNT(*)
FROM tableName
WHERE date(created_date) >= '2017-01-10' AND date(created_date) <= '2017-01-30'
Try This One
SELECT COUNT(1)
FROM tableName
WHERE Cast(created_date as date) Between Cast('2017-01-10' as date) AND Cast('2017-01-30' as date)

MySQL Group Grouped By Result

I have a very simple table which consists of the following columns:
id | customer_id | total | created_at
I was running this query to get the results per day for the last ten days:
SELECT SUM(total) AS total, DATE_FORMAT(created_at, "%d/%m/%Y") AS date
FROM table
WHERE created_at BETWEEN "2017-02-20" AND "2017-03-01"
GROUP BY created_at
ORDER BY created_at DESC
This works fine, but I've just noticed that there's an issue with imported rows being duplicated for some reason so I'd like to update the query to be able to handle the situation if it ever happens again, in other words select one row instead of all when the date and customer id are the same (the total is also identical).
If I add customer_id to the group by that seems to work but the trouble with that is then the query returns a result per day for each customer when I only want the overall total.
I've tried a couple of things but I haven't cracked it yet, I think it will be achievable using a sub query and/or an inner join, I have tried this so far but the figures are very wrong:
SELECT
created_at,
(
SELECT SUM(total)
FROM table test
WHERE test.created_at = table.created_at
AND test.customer_id = table.customer_id
GROUP BY customer_id, created_at
LIMIT 1
) AS total
FROM table
WHERE created_at BETWEEN "2017-02-20" AND "2017-03-01"
GROUP BY created_at
ORDER BY created_at DESC
It's also a large table so finding a performant way to do this is also important.
First, are you sure that created_at is a date and not a datetime? This makes a big difference.
You can do what you want using two levels of aggregation:
SELECT SUM(max_total) AS total, DATE_FORMAT(created_at, '%d/%m/%Y') AS date
FROM (SELECT t.customer_id, t.created_at, MAX(total) as max_total
FROM table t
WHERE t.created_at BETWEEN '2017-02-20' AND '2017-03-01'
GROUP BY t.customer_id, t.created_at
) t
GROUP BY created_at
ORDER BY created_at DESC;

Count and Group by Month name in railes

This is the code I have
MediaDownload.select("COUNT(*) AS count_all,MONTHNAME(created_at) AS created_at ").group('created_at').count
but instead of getting a sql(mysql) code like this :
SELECT COUNT(*) AS count_all, MONTHNAME(created_at) AS created_at FROM `media_downloads` GROUP BY created_at
I get this :
SELECT COUNT(*) AS count_all, created_at AS created_at FROM `media_downloads` GROUP BY created_at
What I am doing wrong. Is there any solutions to get what I want.
There is an excess count in your ActiveRecord query syntax, so the following one
MediaDownload.select("COUNT(*) AS count_all,MONTHNAME(created_at) AS created_at ").group('created_at')
generates sql query you want

Sql Query to count same date entries

All I want to count entries based on date.(i.e entries with same date.)
My table is
You can see 5th and 6th entry have same date.
Now, the real problem as i think is the same date entry have different time so i am not getting what I want.
I am using this sql
SELECT COUNT( created_at ) AS entries, created_at
FROM wp_frm_items
WHERE user_id =1
GROUP BY created_at
LIMIT 0 , 30
What I am getting is this.
I want entries as 2 for date 2012-02-22
The reason you get what you get is because you also compare the time, down to a second apart. So any entries created the same second will be grouped together.
To achieve what you actually want, you need to apply a date function to the created_at column:
SELECT COUNT(1) AS entries, DATE(created_at) as date
FROM wp_frm_items
WHERE user_id =1
GROUP BY DATE(created_at)
LIMIT 0 , 30
This would remove the time part from the column field, and so group together any entries created on the same day. You could take this further by removing the day part to group entries created on the same month of the same year etc.
To restrict the query to entries created in the current month, you add a WHERE-clause to the query to only select entries that satisfy that condition. Here's an example:
SELECT COUNT(1) AS entries, DATE(created_at) as date
FROM wp_frm_items
WHERE user_id = 1
AND created_at >= DATE_FORMAT(CURDATE(),'%Y-%m-01')
GROUP BY DATE(created_at)
Note: The COUNT(1)-part of the query simply means Count each row, and you could just as well have written COUNT(*), COUNT(id) or any other field. Historically, the most efficient approach was to count the primary key, since that is always available in whatever index the query engine could utilize. COUNT(*) used to have to leave the index and retrieve the corresponding row in the table, which was sometimes inefficient. In more modern query planners this is probably no longer the case. COUNT(1) is another variant of this that didn't force the query planner to retrieve the rows from the table.
Edit: The query to group by month can be created in a number of different ways. Here is an example:
SELECT COUNT(1) AS entries, DATE_FORMAT(created_at,'%Y-%c') as month
FROM wp_frm_items
WHERE user_id =1
GROUP BY DATE_FORMAT(created_at,'%Y-%c')
You must eliminate the time with GROUP BY
SELECT COUNT(*) AS entries, created_at
FROM wp_frm_items
WHERE user_id =1
GROUP BY DATE(created_at)
LIMIT 0 , 30
Oops, misread it.
Use GROUP BY DATE(created_at)
Try:
SELECT COUNT( created_at ) AS entries, created_at
FROM wp_frm_items
WHERE user_id =1
GROUP BY DATE(created_at)
LIMIT 0 , 30