Count and Group by Month name in railes - mysql

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

Related

Mysql: How to query records created by all users every day?

My table has following data:
As you can see above table has user_id and created_at columns. I want to query data as shown below.
I used below query but it only data for 1 user at any date.
e user_id is not null
GROUP BY DATE(created_at);
[![enter image description here][3]][3]
As you can see, it shows data only for one user a time.
Please help.
Include user_id in group by clause to get data for each date and each user
SELECT count(*) AS 'Requests',
DATE(created_at) AS 'Date',
user_id
FROM ibiza_production.line_write_revenues where user_id is not null
GROUP BY DATE(created_at),user_id;
SELECT count(*) AS 'Requests',
DATE(created_at) AS 'Date',user_id
FROM ibiza_production.line_write_revenues where user_id is not null
GROUP BY DATE(created_at), user_id;
Try to group with concat
SELECT `date`,`user_id`,COUNT(*) FROM ibiza_production.line_write_revenues GROUP BY CONCAT(`date`,'-',`user_id`) ORDER BY `date`,`user_id` LIMIT 50
Hi Can you try this and let me know if it helped you.
SELECT count(*) AS 'Requests',
DATE(created_at) AS 'Date',user_id
FROM ibiza_production.line_write_revenues where user_id is not null
GROUP BY DATE(created_at),user_id order by DATE(created_at);

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;

MySQL count results after MAX timestamp

I have two tables
mts
id
customer_id
created_at
mos
id
customer_id
created_at
I want to get the last time of an entry in mts and count the results of mos after MAX(mts.created_at). All should be GROUP BY customer_id.
I had the idea of a simple query like this, but this wont work.
SELECT id, created_at, COUNT(id)
FROM mos
WHERE created_at > (SELECT MAX(created_at) FROM mts)
GROUP BY customer_id
LIMIT 10
This SQL-Statement will just give you 1 single line in the result set (not matter what). Everything that is not part of an aggregate-function should by contained in GROUP BY.
Your SQL-Statement should look like this:
SELECT id, created_at, COUNT(id)
FROM mos
WHERE created_at > (SELECT MAX(created_at) FROM mts)
GROUP BY customer_id, id, created_at
LIMIT 10

List of records per date

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)

mysql - want to group by two columns including by each day

I have the following mysql 5.5 query:
select count(original_url), original_url from page_views group by original_url order by count(original_url) desc;
but would also like to group by each day (like 2014-06-23) so that I can see which day has the most page_views per url. It's a rails app so there is a created_at. I was thinking something like:
select count(original_url), original_url, created_at from page_views group by original_url, created_at order by count(original_url) desc;
but that selects and groups by hour:min:sec and I just want it to be by day like YEAR-MONTH-DAY. How would I do that?
Try the Date function:
select count(original_url), original_url, created_at from page_views group by original_url, DATE(created_at) order by count(original_url)