I want to count the users in the table who's subscriptions are going to expire within a month (30 days). Here is my code:
user_db
id name exp_date
1 John 2013-03-01
2 Alice 2013-02-25
3 Ken 2013-01-10
4 Elise 2013-04-11
5 Bruce 2013-03-14
According to the DB above. There should be 3 persons whom their subscription is about to be expired - John, Alice and Bruce. I don't want Ken to be counted because he doesn't want to subscribe for more.
Here's my MySQL code:
SELECT count(id) AS exp_pax,
datediff(exp_date,now()) AS day_left
FROM labour_db
WHERE day_left<=30
Well, the code does selects only a row in which the sum of day less than 30 but it doesn't count. So please you guy suggest me.
Regards,
If you want to count all records where (1) the expiration date is within 30 days of now and (2) the expiration date is not before now, then use
SELECT count(*) AS exp_pax
FROM user_db
WHERE exp_date<=timestampadd(day, 30, now())
AND exp_date >= now();
If that's the case then you need to add condition wherein it checks if the exp_date is less than today.
SELECT COUNT(*) totalCount
FROM user_db
where exp_date <= timestampadd(day, 30, now()) AND
exp_date > NOW()
SQLFiddle Demo
Remove the group by id to get your count.
Count will roll up rows, as you expect, but when combined with a group by clause, will count each "group". You could use this usefully, for instance, to group by expiration date.
Related
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)
I have a MySQL Table. I put the schema to this fiddle.
In my table when a user logs in I record him/her to mytable. In one day same user can log in several times. So I need to know how many times a user logged in each day.
I need an output like this:
2013-01-30
-- Michael - 2 times
-- John - 4 times
2013-01-29
-- Michael - 1 time
-- John - 1 time
-- Mary - 1 time
-- Dean - 1 time
2013-01-28
-- Michael - 3 times
-- Mary - 1 times
I tried this:
SELECT COUNT(*) AS "times", username, date
FROM mytable GROUP BY date ORDER BY date DESC
But it gave me this:
- 2013-01-30 - Michael - 6 times
- 2013-01-29 - John- 4 times
- 2013-01-28 - Michael - 6 times
Can you recommend a query for this?
Just add username to your GROUP BY clause.
SELECT COUNT(*) AS "times", username, date
FROM mytable
GROUP BY date, username ORDER BY date DESC
http://sqlfiddle.com/#!2/b5701/11
SELECT date, username, COUNT(1) AS "times"
FROM mytable GROUP BY date, username ORDER BY date DESC
you should Group by username also.
try this
SELECT COUNT(*) AS times, username, date
FROM mytable group by date,username ORDER BY date DESC
SQL FIDDLE DEMO
In mySQL what I am trying to do is to query my table and find out how many people registered each day. In other words, I want to be able to produce the following output for one month:
1 January: 10 registrations
2 January: 150 registrations
3 January: 50 registrations
select created, regID
from registrations
Dates are in the following format in the DB: 2012-11-01 00:00:00
To get registration counts for each day of January, use this select:
select daymonth(registration_date), count(*)
from registrations
where registration_date >= '01/01/2012' and registration_date <= '01/31/2012'
group by daymonth(registration_date)
You usually use a grouping operator:
SELECT COUNT(*) AS registrations, DATE(created) AS created_date GROUP BY created_date
If created is already a DATE column, then the conversion isn't required.
Try this:
SELECT created, count(regID) FROM registrations GROUP BY created ORDER BY created ASC
SELECT DATE(DATE_REGISTERED) DATE, COUNT(*) totalRegistered
FROM tableName
GROUP BY DATE
I'm trying to write a query that returns a list of dates and the DISTINCT COUNT of User IDs for the 7 days preceding each date. The table I'm working with is simple, and looks like this:
Started UserId
"2012-09-25 00:01:04" 164382
"2012-09-25 00:01:39" 164382
"2012-09-25 00:02:37" 166121
"2012-09-25 00:03:35" 155682
"2012-09-25 00:04:18" 160947
"2012-09-25 00:08:19" 165806
I can write the query for output of an individual COUNT as follows:
SELECT COUNT(DISTINCT UserId)
FROM Session
WHERE Started BETWEEN '2012-09-18 00:00' AND '2012-09-25 00:00';
But what I'm trying to do is output this COUNT for every day in the table AND the 7 days preceding it. To clarify, the value for September 25th would be the count of DISTINCT User IDs between the 18th and 25th, the 24th the count between 17th and 24th, etc.
I tried the following query but it provides just the COUNT for each day:
SELECT
DATE(A.Started),
Count(DISTINCT A.UserId)
FROM Session AS A
WHERE DATE(A.Started) BETWEEN DATE(DATE_SUB(DATE(DATE(A.Started)),INTERVAL 7 DAY)) AND DATE(DATE(A.Started))
GROUP BY DATE(A.Started)
ORDER BY DATE(A.Started);
And the output looks like this:
DATE(A.Started) "Count(DISTINCT A.UserId)"
2012-09-18 709
2012-09-19 677
2012-09-20 658
2012-09-21 556
2012-09-22 530
2012-09-23 479
2012-09-24 528
2012-09-25 480
...
But as I said, those are just the daily counts. Initially I thought I could just sum the 7 day values, but that will invalidate the DISTINCT clause. I need the DISTINCT UserId counts for each 7 day period preceding a given date.
This query should work for you:
SELECT
DATE_FORMAT(d1.Started, '%Y-%m-%d') AS Started,
COUNT(DISTINCT d2.UserID) Users
FROM
(
SELECT
DATE(Started) AS Started
FROM
Session
GROUP BY
DATE(Started)
) d1
INNER JOIN
(
SELECT DISTINCT
DATE(Started) AS Started,
UserID
FROM
Session
) d2
ON d2.Started BETWEEN d1.Started - INTERVAL 7 DAY AND d1.Started
GROUP BY
d1.Started
ORDER BY
d1.Started DESC
Visit http://sqlfiddle.com/#!2/9339c/5 to see this query in action.
try:
Select Distinct Date(A.Started), Count(B.UserId)
From Session a
Join Session b
On b.Start Between AddDate(A.Start, day, -7) And A.Start
I'm not a MySQL guy, so the syntax might not be correct, but the pattern will work....
I have the following (MySQL) table called "tweets":
tweet_id created_at
---------------------
1 1298027046
2 1298027100
5 1298477008
I want MySQL returning the number of tweets per day of the week; taking the above data it should return:
Wednesday 1
Friday 2
I now have the following query (which should return the day of the week index, not the full name):
SELECT
COUNT(`tweet_id`),
WEEKDAY(FROM_UNIXTIME(`created_at`))
FROM tweets2
ORDER BY WEEKDAY(FROM_UNIXTIME(`created_at`))
This however returns:
COUNT(`tweet_id`) WEEKDAY(FROM_UNIXTIME(`created_at`))
7377 4
(There are a total of 7377 tweets in the database). What am I doing wrong?
SELECT
COUNT(`tweet_id`),
DAYNAME(FROM_UNIXTIME(created_at)) AS Day_Name1
FROM tweets2
GROUP BY Day_Name1
ORDER BY Day_Name1;
You have a count, but you don't have a group by. You should include a
GROUP BY WEEKDAY(FROM_UNIXTIME(`created_at`))
You are not grouping by week day so you only get one grand total. Try this:
SELECT
COUNT(`tweet_id`),
WEEKDAY(FROM_UNIXTIME(`created_at`))
FROM tweets2
GROUP BY WEEKDAY(FROM_UNIXTIME(`created_at`))
ORDER BY WEEKDAY(FROM_UNIXTIME(`created_at`));