total count each day sorted, result grouped by week - mysql

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(*)

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.

Get average of values of rows of every month and grouped by user

I have these tables:
Fiddle of tables: http://sqlfiddle.com/#!9/18c65
With this query:
SELECT user_id AS user, round(AVG(cost_freight), 0) AS average FROM `freights` GROUP BY user ORDER BY average ASC LIMIT 10
i get average of freights(cost_freight) grouped by user (the values in bold is the authenticated user):
But now I need to get average of freights(cost_freight) of every month grouped by user, the result should be like this:
and this is where I have tried several ways but I can't get the desired result.
I would very much appreciate your help.
Thanks.
Use one column(MONTH(date)) into group by. Try this one,
SELECT user_id AS user,MONTH(date), round(AVG(cost_freight), 0) AS average FROM `freights` GROUP BY user,MONTH(date) ORDER BY average ASC LIMIT 10

Query SELECT DISTINCT count()

Hello there I have the following doubt I want to count how many times in a month I enter data.
My database is:
Date:
10/2010
10/2010
09/2010
08/2010
I have the following query.
SELECT DISTINCT (date)
FROM employee
WHERE date
IN (SELECT date
FROM employee
GROUP BY date
HAVING count( date ) >0)
ORDER BY date DESC;
This query gives me:
Date:
10/2017
8/2017
9/2017
But I want you to give me something like that.
Count | Date
2 | 10/2017
1 | 9/2017
1 | 10/2017
I hope I have explained my regards.
You're overcomplicating it; no subquery, or DISTINCT, needed.
SELECT `date`, count(*)
FROM `employee`
GROUP BY `date`
HAVING count(*) > 0
ORDER BY `date` DESC;
I am a little confused as to what reason you would have for the HAVING count() > 0 though; the only way something could have a zero count would mean it wasn't in the table (and therefore wouldn't show up anyway).
Other observations:
DISTINCT is not a function; enclosing the date in parenthesis in the SELECT clause has absolutely no effect. (Also, DISTINCT is almost never appropriate for a GROUPing query.)
COUNT(somefield) is the same as COUNT(1), COUNT(*). If you want the count of unique values you can do COUNT(DISTINCT somefield); but it wouldn't make sense to COUNT(DISTINCT groupingfield) as that would always result in 1.
The query you wrote is a bit complicated. Distinct and group by are doing the same thing for you here. When you do a group by count will automatically give you the count of grouped rows. Also you will have unique dates as well. Try this.
SELECT count(date), date
FROM employee
GROUP BY date
HAVING count( date ) >0
ORDER BY date DESC;

Get Weekly SUM from 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)

MySQL Aggregate function in other aggregate function

I'm having a table with posts. Like (id int, date datetime).
How can I select average posts per day count for each month with one sql request?
Thank you!
This should do it for you:
select month, avg(posts_per_day)
from (select day(date), month(date) as month, count(*) as posts_per_day
from posts group by 1,2) x
group by 1
Explanation: Because you are doing an aggregate on an aggregate, there is no getting around doing a query on a query:
The inner query calculates the number per day and captures the month.
The outer query averages this count , grouping by month.
You can get the number of posts per month like this:
SELECT COUNT(*) AS num_posts_per_month FROM table GROUP BY MONTH(date);
Now we need the number of days in a month:
SELECT COUNT(*) / DATEDIFF(MAKEDATE(YEAR(date), MONTH(date)) + INTERVAL 1 MONTH, MAKEDATE(YEAR(date), MONTH(date))) AS avg_over_month
FROM table GROUP BY MONTH(date);
This will get the average number of posts per day during the calendar month of the post. That is, averages during the current month will continue to rise until the end of the month. If you want real averages during the current month, you have to put in a conditional to get the true number of elapsed days.