Get Weekly SUM from MySQL - mysql

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)

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.

Mysql counting records per day with the date next to the count

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`;

Top-10 mysql query

I'm in need of a better way of retrieving top 10 distinct UID from some tables I have.
The setup:
Table user_view_tracker
Contains pairs of {user id (uid), timestamp (ts)}
Is growing every day (today it's 41k entries)
My goal:
To produce a top 10 of most viewed user id's in the table user_view_tracker
My current code is working, but killing the database slowly:
select
distinct uvt.uid as UID,
(select count(*) from user_view_tracker temp where temp.uid=uvt.uid and temp.ts>date_sub(now(),interval 1 month)) as CLICK
from user_view_tracker uvt
order by CLICK
limit 10
It's quite obvious that a different data structure would help. But I can't do that as of now.
First of all, delete that subquery, this should be enough ;)
select
uvt.uid as UID
,count(*) as CLICK
from
user_view_tracker uvt
where
uvt.ts > date_sub(now(),interval 1 month)
group by
uvt.uid
order by CLICK DESC
limit 10
Try:
select uid, count(*) as num_stamps
from user_view_tracker
where ts > date_sub(now(), interval 1 month)
group by uid
order by 2 desc limit 10
I kept your criteria as far as getting the count for just the past month. You can remove that line if you want to count all.
The removal of DISTINCT should improve performance. It is not necessary if you aggregate in your outer query and group by uid, as that will aggregate the data to one row per uid with the count.
You should use Aggregate functions in MySQL
SELECT UID, COUNT(ts) as Number_Of_Views FROM user_view_tracker
GROUP BY UID
ORDER BY Number_Of_Views DESC
LIMIT 10
A simple demo which selects the top 10 UID viewed
http://sqlfiddle.com/#!2/907c10/3

mysql query for website visiter count

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

total count each day sorted, result grouped by week

My query looks like this right now pretty straightforward:
select
count(*),
date(visit_date),
DATE_FORMAT(visit_date,"%a")
from visits
group by date(visit_date)
Here is the result:
http://d.pr/FmMg
what I want to happen is:
for each week the count is sorted
Can you modify my query so it satisfies the criteria?
Assuming, you no longer need the count by day and are ONLY looking for count by week:
SELECT
count(*),
yearweek(visit_date)
FROM visits
GROUP BY yearweek(visit_date)
ORDER BY yearweek(visit_date) ASC;
Are you trying to do like this?
- you can use Datepart to get week number and sort by that.
select
count(*),
date(visit_date),
DATE_FORMAT(visit_date,"%a")
from
visits
group by
date(visit_date)
order by
datepart(yyyy,visit_date),
datepart(wk,visit_date),
count(*)