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.
Related
So we log when we send our clients promotional emails and sometimes clients are in our database for a while before they receive their first email so we want to know how many clients received their first ever email by month for the past 12 months.
So far I can only think to get the information month by month but there has to be a way to query all 12 months in a single query.
SELECT DISTINCT
`id`
FROM
`table1`
WHERE
`sendtime` BETWEEN '2019-08-01' AND '2019-09-01'
AND `id` NOT IN (SELECT
`id`
FROM
`table1`
WHERE
`sendtime` < '2019-08-01');
Check for the minimum sendtime for each user:
SELECT id
FROM table1
GROUP BY id
HAVING MIN(sendtime) BETWEEN '2019-08-01' AND '2019-09-01'
If you want the number of these ids:
SELECT COUNT(*) counter
FROM (
SELECT id
FROM table1
GROUP BY id
HAVING MIN(sendtime) BETWEEN '2019-08-01' AND '2019-09-01'
) t
You can use two levels of aggregation:
select date_format(min_sendtime, '%Y-%m') yyyy_mm, count(*) no_clients
from (
select id, min(sendtime) min_sendtime
from table1
group by id
) t
where min_sendtime >= date_format(current_date, '%Y-%m-01') - interval 1 year
group by yyyy_mm
order by yyyy_mm
This gives you one row for each of the last twelve months (that has a least one customer that received their first email), with the count of "new" email over the month.
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');
I have a number of posts saved into a InnoDB table on MySQL. The table has the columns "id", "date", "user", "content". I wanted to make some statistic graphs, so I ended up using the following query to get the amount of posts per hour of yesterday:
SELECT HOUR(FROM_UNIXTIME(`date`)) AS `hour`, COUNT(date) from fb_posts
WHERE DATE(FROM_UNIXTIME(`date`)) = CURDATE() - INTERVAL 1 DAY GROUP BY hour
This outputs the following data:
I can edit this query to get any day I want. But what I want now is the AVERAGE of each hour of every day, so that if on Day 1 at 00 hours I have 20 posts and on Day 2 at 00 hours I have 40, I want the output to be "30". I'd like to be able to pick date periods as well if it's possible.
Thanks in advance!
You can use a sub-query to group the data by day/hour, then take the average by hour across the sub-query.
Here's an example to give you the average count by hour for the past 7 days:
select the_hour,avg(the_count)
from
(
select date(from_unixtime(`date`)) as the_day,
hour(from_unixtime(`date`)) as the_hour,
count(*) as the_count
from fb_posts
where `date` >= unix_timestamp(current_date() - interval 7 day)
and created_on < unix_timestamp(current_date())
group by the_day,the_hour
) s
group by the_hour
Aggregate the information by date and hour, and then take the average by hour:
select hour, avg(numposts)
from (SELECT date(`date`) as day, HOUR(FROM_UNIXTIME(`date`)) AS `hour`,
count(*) as numposts
from fb_posts
WHERE DATE(FROM_UNIXTIME(`date`)) between <date1> and <date2>
GROUP BY date(`date`), hour
) d
group by hour
order by 1
By the way, I prefer including the explicit order by, since most databases do not order the results of a group by. Mysql happens to be one database that does.
SELECT
HOUR(FROM_UNIXTIME(`date`)) AS `hour`
, COUNT(`id`) \ COUNT(DISTINCT TO_DAYS(`date`)) AS avgHourlyPostCount
FROM fb_posts
WHERE `date` > '2012-01-01' -- your optional date criteria
GROUP BY hour
This gives you a count of all the posts, divided by the number of days, by hour.
I have a CHANGES table with fields VALUE(integer) and CREATED_AT(timestamp). I want to know the total of the VALUE column grouped by each of the past 30 days (without making 30 queries).
So if yesterday there were records created with VALUEs of 10, -7, and 12; I would want a record returned with CREATED_AT = yesterday and TOTAL = 15.
Any help?
SELECT date(created_at) as CREATED_AT, sum(value) as TOTAL
FROM changes
WHERE created_at >= curdate() - interval 30 day
GROUP BY date(created_at);
Well, it slightly depends on what kind the timestamp is formatted in (SQL/ Unix/ etc). But this type of query might help you along:
SELECT
DATE_FORMAT(CREATED_AT, '%Y-%m-%d') ym,
COUNT(VALUE)
FROM foo
GROUP BY ym
I have a table with columns: NAME, CHANGE_ID, and CHANGE_DATE, where each row constitutes a single change, the columns indicated who made the change(name), when it was made(timestamp), and an id for the change(integer).
I can retrieve a list of names sorted by those that have made the most changes(in the last month) with the following query:
SELECT
NAME AS name,
COUNT(DISTINCT CHANGE_ID) AS changes
FROM
CHANGE_TABLE
WHERE
DATE(CHANGE_DATE) > DATE(now() - INTERVAL 1 MONTH)
GROUP BY
name
ORDER BY
changes DESC
And I can retrieve a list of changes made per month in the last 10 months with the following query:
SELECT
DATE_FORMAT(CHANGE_DATE, '%Y-%m') AS date,
COUNT(DISTINCT CHANGE_ID) AS change_count
FROM
CHANGE_TABLE
WHERE
CHANGE_DATE > curdate() - INTERVAL 10 MONTH
GROUP BY
date
What I want is a query that will return the combined information of these queries: I want the names of the top change-makers and how many changes they have made each month for the last 10 months. I don't particularly care how the resulting table looks as long as the data is there. I have wracked my brain, but my SQL understanding is not great enough to solve the problem. Any help would be appreciated.
Have you tried grouping on date and name, something like:
SELECT
DATE_FORMAT(CHANGE_DATE, '%Y-%m') AS date,
COUNT(DISTINCT CHANGE_ID) AS change_count,
NAME
FROM
CHANGE_TABLE, (SELECT
NAME AS name,
COUNT(DISTINCT CHANGE_ID) AS changes
FROM CHANGE_TABLE
WHERE DATE(CHANGE_DATE) > DATE(now() - INTERVAL 1 MONTH)
GROUP BY name
ORDER BY changes DESC
) subq
WHERE CHANGE_DATE > curdate() - INTERVAL 10 MONTH AND change_table.name = subq.name
GROUP BY date, name