group by date_format for each day - mysql

My I'm having problems with a query.
Here is what I have
SELECT COUNT(id) as Counted
FROM referral_code_logs
GROUP BY DATE_FORMAT(time_stamp, '%c/%e/%y');
I am trying to see the amount of referrals each day has. The part I'm having problems with is the date_format.
Table layout
Here are how the time_stamps are entered in the Table: (month/day/year)
month is 1-12 day is 0-31 year is YY
Time_stamps

You just need to add in the date to the select part:
SELECT DATE_FORMAT(STR_TO_DATE(time_stamp, '%c/%e/%y'), '%c/%e/%y') as DT
,COUNT(id) as Counted
FROM referral_code_logs
GROUP BY DATE_FORMAT(time_stamp, '%c/%e/%y');

Related

SQL - I need help How to COUNT all sales by month within a year

I can't figure out a way to COUNT al transactions by month each YEAR on a single query. I figured out a way but for one month at a time, there's a lot of data (time consuming)
this is the table
i want to have something like this
thank you
here is how you can do it , using extract to extract month /year from your date column and group by it:
select
extract(year_month from Date) year_month
, count(*) total_sales
from your table
group by extract(year_month from Date)
order by extract(year_month from Date)

Avg function not returning proper value

I expect this query to give me the avg value from daily active users up to date and grouped by month (from Oct to December). But the result is 164K aprox when it should be 128K. Why avg is not working? Avg should be SUM of values / number of current month days up to today.
SELECT sq.month_year AS 'month_year', AVG(number)
FROM
(
SELECT CONCAT(MONTHNAME(date), "-", YEAR(DATE)) AS 'month_year', count(distinct id_user) AS number
FROM table1
WHERE date between '2020-10-01' and '2020-12-31 23:59:59'
GROUP BY EXTRACT(year_month FROM date)
) sq
GROUP BY 1
Ok guys thanks for your help. The problem was that on the subquery I was pulling the info by month and not by day. So I should pull the info by day there and group by month in the outer query. This finally worked:
SELECT sq.day_month, AVG(number)
FROM (SELECT date(date) AS day_month,
count(distinct id_user) AS number
FROM table_1
WHERE date >= '2020-10-01' AND
date < '2021-01-01'
GROUP BY 1
) sq
GROUP BY EXTRACT(year_month FROM day_month)
Do not use single quotes for column aliases!
SELECT sq.month_year, AVG(number)
FROM (SELECT CONCAT(MONTHNAME(date), '-', YEAR(DATE)) AS month_year,
count(distinct id_user) AS number
FROM table1
WHERE date >= '2020-10-01' AND
date < '2021-01-01'
GROUP BY month_year
) sq
GROUP BY 1;
Note the fixes to the query:
The GROUP BY uses the same columns as the SELECT. Your query should return an error (although it works in older versions of MySQL).
The date comparisons have been simplified.
No single quotes on column aliases.
Note that the outer query is not needed. I assume it is there just to illustrate the issue you are having.

MySQL grouping from complex query

I need to create a query that looks like this image with the result:
You can ignore the names of the user, user_id is fine for now. Each user can have several timesheets for one day. So I need to count the hours and place it in its own column for day of the week. Then have a total at the end. Here is a screen shot of the database:
Here is what I have so far that gets me the days of the week totals but not grouped in one record with the day of the week as its own column and a total. Any help would be appreciated. Thanks!
SELECT user_id, WEEKDAY(start_date) AS day, (select time_to_sec(timediff(end_date, start_date )) / 3600) AS hours FROM `timesheet_table` WHERE id > 0 GROUP BY day, user_id
If you need a totat you can use a sum and group by
In Group by you can't use the alias but you should use the expression
SELECT
user_id
, WEEKDAY(start_date) AS day
, sum((select time_to_sec(timediff(end_date, start_date )) / 3600)) AS hours
FROM `timesheet_table`
WHERE id > 0
GROUP BY WEEKDAY(start_date), user_id
E.g.:
SELECT user_id
, DATE(start_date) dt
, SEC_TO_TIME(SUM(TIME_TO_SEC(end_date)-TIME_TO_SEC(start_date))) day_total
-- [or , SUM(TIME_TO_SEC(end_date)-TIME_TO_SEC(start_date))/3600 day_total]
FROM my_table
WHERE start_date BETWEEN '2016-10-01 00:00:00' AND '2016-10-07 23:59:59'
GROUP
BY user_id
, DATE(start_date);
The rest of the problem (missing days, display issues, weekly totals, etc.) would normally be handled in application level code.

get month wise total, week wise total and day wise total in one mysql query

I want to display month wise total, week wise total and then day wise total with days in one mysql query. I can do it using seperate queries but i want it in single query . Is it possible to display this hierarchy?
by using GROUP BY MONTH we found total by month and GROUP BY WEEK we found total by WEEK
Select sum(column) From table GROUP BY MONTH(column)
union
Select sum(column) From table GROUP BY WEEK(column)
and UNION operator combines the result
This is from link you have provided.
use UNION:
SELECT SUM(cost) AS total, MONTHNAME(date) AS month
FROM daily_expense
GROUP BY month
UNION
SELECT SUM(cost) AS total, CONCAT(date, ' - ', date + INTERVAL 6 DAY) AS week
FROM daily_expense
GROUP BY WEEK(date)

One query to get total # records for each day in a month

Scenario:
I have a table of registrations and there is a datetime column.
I want to display a bar graph on a webpage showing number of registrations per day for the current month.
How can I do that with one query? i.e. get back a resultset with a field for each day (of current month) with the total number of registrations that day
select day(Date), count(*)
from table
where month(Date) = month(NOW()) and year(Date) = year(NOW())
group by day(Date)
"Where" clause selects the current month. You can use count(whatever) or count(distinct whatever) instead of count(*)...
Something like this pseudo-code:
SELECT Day, COUNT(*)
FROM XXX_Users
GROUP BY "date extracted from datetime" as Day
To extract the date from datetime you could use the DATE function:
SELECT Day, COUNT(*)
FROM XXX_Users
GROUP BY DATE(created) as Day
Or you can create a Stored Procedure to enclose all the logic inside