Get COUNT of id when GROUP BY Id and DATE - mysql

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;

Related

related to query using SQL

In oracle sql, how to get the count of newly added customers only for the month of april and may and make sure they werent there in the previous months
SELECT CUSTOMER ID , COUNT(*)
FROM TABLE
WHERE DATE BETWEEN '1-APR-2018' AND '31-MAY-2018' AND ...
If we give max (date) and min(date), we can compare the greater date to check if this customer is new , correct?
expected output is month count
april ---
may ---
should show the exact count how many new customers joined in these two months
One approach is to use aggregation:
select customer_id, min(date) as min_date
from t
group by customer_id
having min(date) >= date '2018-04-01 and
min(date) < date '2018-06-01';
This gets the list of customers (which your query seems to be doing). To get the count, just use count(*) and make this a subquery.

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)

Select between dates with count condition

I have table user with column login_time.
I want to select all the users that have logged in more than 10 times in a month.
I tried something like this:
SELECT login_time, count(id) as loginCount FROM user
WHERE login_time between DATE_SUB(login_time INTERVAL 1 month) AND login_time
GROUP BY id, MONTH(login_time) HAVING loginCount > 10;
Im not sure about my selection between dates.
How can I select with a month intervals avoiding double records.
For example if I have this values for login_time:
1. '2015-02-01 14:05:19'
2. '2015-01-21 14:05:19'
3. '2015-01-11 14:05:19'
Both 3 and 2 are within month range of 1.
So will I get double records for that values?
To find the users who have logged in more than ten times in the month ending right now, do this.
SELECT COUNT(*) times_logged_in,
userid
FROM user
WHERE login_time >= NOW() - INTERVAL 1 MONTH
GROUP BY user_id
HAVING COUNT(*)> 10
To find the users who have logged in more than ten times in any calendar month in your table, do this.
SELECT COUNT(*) times_logged_in,
DATE(DATE_FORMAT(login_time, '%Y-%m-01')) month_beginning,
userid
FROM user
GROUP BY user_id, DATE(DATE_FORMAT(login_time, '%Y-%m-01'))
HAVING COUNT(*)> 10
The trick here is the expression DATE(DATE_FORMAT(login_time, '%Y-%m-01')), which converts any timestamp to the first day of the month in which it occurs.
Your question mentioned this WHERE condition:
WHERE login_time between DATE_SUB(login_time INTERVAL 1 month) AND login_time
This doesn't do anything interesting because it always comes back true. Each given login_time always falls in the interval you specified.
Edit: You can GROUP BY MONTH(dt) if you want. But the way I have shown it automatically accounts for years as well as months, so in my opinion it's much better for accurate reporting.
Another edit: This formula yields the preceding Sunday for any given date or timestamp item.
FROM_DAYS(TO_DAYS(login_time) -MOD(TO_DAYS(login_time) -1, 7))
If Monday is the first day of the week in your jurisdiction, change the -1 to -2. Grouping by this function is superior to doing GROUP BY WEEK(login_time) because WEEK() does odd things at the beginnings and ends of calendar years.
This is all written up in more detail here: http://www.plumislandmedia.net/mysql/sql-reporting-time-intervals/

Previous day Record count in my sql

Hi I am not perfect in mysql queries i tried this code to get previous date record count
code snippet
SELECT id, date, COUNT(IF(date<= date-INTERVAL 1 DAY, id, NULL))
FROM table_name
GROUP BY date
this query gives me prev day value is 0.
help me out to get previous day count of id
this is what i need
date count
-------------------
2014-01-01 0
2014-01-02 13
2014-01-03 55
I suspect you rather look for something like this, to count id's from yesterday:
select
date(dt),
count(id)
from
table_name
where
date(dt) < date(now())
group by
date(dt)

SQL Query :: parameters inside a Count?

Let's say I have a table of movie renters with columns:
UserID
MovieID
Rent_Start_date
Rent_Due_Date
I am trying to achieve an output table that looks like:
[UserID, Count of Movies Due in 1 Day, Count of Movies Due in 1 Week]
Is this possible to do in one single query? I currently have a php script that runs 1 query on movies due in 1 day and another query that runs movies due in 1 week. These two queries are then looped for every user ID, filling in the table essentially slot by slot. This is kind of slow.
By attempting to create this output with only 1 query, I tried something like:
SELECT UserID, count(movieID)
FROM MovieTable
GROUP BY movieID
But this doens't create columns of counts of expiration dates.
Is it possible to create a count column that has an arguement such as count( //all those satisfy where Rent_Due_Date - CURDATE() < ONE_WEEK)?
You need to group your results by user, not by movie:
SELECT UserID,
SUM(Rent_Due_Date BETWEEN CURRENT_DATE AND CURRENT_DATE + INTERVAL 1 DAY),
COUNT(*)
FROM MovieTable
WHERE Rent_Due_Date BETWEEN CURRENT_DATE AND CURRENT_DATE + INTERVAL 1 WEEK
GROUP BY UserID