I have a table to store website visiter count bellow
id date ip
-----------------------------
1 1-1-2012 195.165.1.2
2 1-1-2012 195.165.1.3
3 12-2-2012 195.165.1.8
and i want a mysql query for get count of each month of current year
seeing your DATE column, i assumed that you save dates as VARCHAR
SELECT MONTHNAME(STR_TO_DATE(date, '%c-%e-%Y')) `monthName`, COUNT(*) totalCount
FROM tableName
WHERE YEAR(STR_TO_DATE(date, '%c-%e-%Y')) = YEAR(CURDATE())
GROUP BY MONTHNAME(STR_TO_DATE(date, '%c-%e-%Y'))
SQLFiddle Demo
OTHER SOURCEs
STR_TO_DATE()
YEAR()
CURDATE()
MONTHNAME()
"select month(date),count(distinct ip) from visitors where date>='$y-01-01' and date<'$y-12-31' group by 1"
SELECT YEAR(date) YEAR,MONTHNAME(date) MONTH,count(id) TOTAL
FROM table_name
GROUP BY YEAR(date),MONTH(date)
THE QUERY IS LIKE THIS BECAUSE DIFFERENT PERSONS FROM SAME IP MAY VISIT
Related
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.
Hi I have this VISITS table
What I want to achieve:
**affiliate_id** **unique visits count**
167 4
121 1
137 1
Special Condition is one IP can only be counted once per day for single affiliate_id.
So for visit_id 553 and 554, it can be only counted as one visits because both have same ip, same date and same affiliate_id.
From what I understand I need to group by ip, date and affiliate_id and count it, but not sure how to write the query.
Can you guys point me to some reference or insight to solve this problem?
Thanks in advance!
--
Update with link sample SQL:
https://dl.dropboxusercontent.com/u/3765168/tb_visits.sql
Based on your requirement i think you need the distinct ip per date and affiliate_id
select DATE(date), affiliate_id, count(distinct( ip))
from your_table
group by DATE(date), affiliate_id
If I understood correctly,
SELECT affiliate_id, count(*)
FROM (SELECT DISTINCT affiliate_id, ip, DAY(date)
FROM visits) AS q
GROUP BY affiliate_id;
What you are trying to do is group the number of unique or distinct ip's for a given affiliate_id so the only group by you need is the affiliate_id. The Unique hits are calculated using a count and to make then unique you add the DISTINCT key word
SELECT
affiliate_id, COUNT(DISTINCT ip) AS unique_visit_counts,
FROM tablename
GROUP BY affiliate_id
However since you want it by the day as well you might want to include a date clause such as:
DATE_FORMAT(date, "%y-%m-%d") AS `date`
Which will turn your date and time stamp into a day in the YY-MM-DD format.
If you group by that you can get a full list by day by affiliate_id using something like
SELECT
affiliate_id,
COUNT(DISTINCT ip) AS unique_visit_counts,
DATE_FORMAT(date, "%y-%m-%d") AS `date`
FROM tablename
GROUP BY `date`, affiliate_id
Or pick a specific date using something like
SELECT
affiliate_id,
COUNT(DISTINCT ip) AS unique_visit_counts,
FROM tablename
WHERE DATE_FORMAT(date, "%y-%m-%d") = '17-02-08'
GROUP BY affiliate_id
I want to get a list of mysql results for each week beginning at July 2015, showing the SUM or new users from my table user GROUPED BY Week. Is this possible?
So as result:
CW25/15: 100
CW26/15: 70
CW27/15: 180
....
How to do?
Try this:
SELECT CONCAT('CW',WEEK(date_col),'/',YEAR(date_col)) as week,
COUNT(*) as count
FROM table_name
GROUP BY YEAR(date_col),WEEK(date_col)
ORDER BY date_col
You can do it like this(You didn't post your table structures so you will have to adjust it) :
SELECT concat('CW',week(DateColumn),'/',year(DateColumn)) as weekDate,
count(*) as cnt
FROM YourTable
GROUP BY concat('CW',week(DateColumn),'/',year(DateColumn))
ORDER BY year(DateColumn),week(DateColumn)
Hi i am trying to get the count of records per day which i can do, but i also want the date to be show, for example,
Result
Date | Count
26/01/2015 20
25/01/2015 | 413
Here is an example of my data.
I would think this would work. Replace 'yourTable' with your table name
SELECT Date, COUNT(*) FROM yourTable GROUP BY Date;
Get the total count and group them by date.
SELECT `date`, COUNT(*) as Total
FROM `table`
GROUP BY `date`
ORDER BY `date`;
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