SQL - Sum last value by date of current week - php

I need to do a SUM of the last value (by date) of each day of the current week. To test I have a table with the values:
5 for 2023-01-12 16:53:01
2 for 2023-01-12 08:49:03
5 for 2023-01-11 08:58:19
I expect a result of 10.
I'm trying with the following code:
SELECT SUM(value) AS weeklyValue
FROM (
SELECT value
FROM table
WHERE WEEK(DATE(DataOra)) = WEEK(NOW())
AND WEEKDAY(DATE(DataOra)) >= 1
AND WEEKDAY(DATE(DataOra)) <= 7
AND DataOra = (SELECT MAX(DataOra) FROM table WHERE WEEKDAY(DataOra) = WEEKDAY(DATE(DataOra)) )
GROUP BY WEEKDAY(DATE(DataOra))
) AS subquery;
but the result is 5. Where is the mistake? Thanks

Maybe I found a solution:
SELECT SUM(Value) AS energiaSettimanale
FROM (
SELECT MAX(Value) as value, WEEKDAY(DataOra) as d
FROM table
WHERE WEEK(DATE(DataOra)) = WEEK(NOW())
AND WEEKDAY(DATE(DataOra)) >= 0
AND WEEKDAY(DATE(DataOra)) <= 6
GROUP BY d
) AS subquery;

If I really understood what you want, this request may be (if I understood correctly, I am specifying) the solution
select sum(value) from `table`
inner join (
select max(DataOra) as maxDataOra
from `table`
where WEEK(DATE(DataOra))=WEEK(NOW())
group by WEEKDAY(DataOra)
)
as tmp on DataOra=tmp.maxDataOra
The trick to find the most recent date of the day being to group by day number with the max() function on dateTime Data.

You can do it using inner join as follows :
select weekNumber, sum(value) from (
select t.value, week(s.maxDataOra) as weekNumber
from _table t
inner join (
select MAX(DataOra) as maxDataOra
from _table
group by DATE(DataOra)
) as s on s.maxDataOra = t.DataOra
) as b
group by weekNumber;
Check it here : https://dbfiddle.uk/hadzywwh

I need to do a SUM of the last value (by date) of each day of the current week.
So, let's start by constraining our working set to just the current week's data -
WHERE `DataOra` >= (CURRENT_DATE - INTERVAL(WEEKDAY(CURRENT_DATE)) DAY) /* first day of current week (Mon - Sun) */
AND `DataOra` < (CURRENT_DATE - INTERVAL(WEEKDAY(CURRENT_DATE)) DAY + INTERVAL 1 WEEK) /* first day of next week (Mon - Sun) */
Then we can use that to constrain the query used to get the MAX(DataOra) per DATE(DataOra) and join back to the original dataset to get the sum of the respective Values -
SELECT SUM(`Value`) `energiaSettimanale`
FROM `table` `t1`
INNER JOIN (
SELECT MAX(`DataOra`) AS `maxDataOra`
FROM `table`
WHERE `DataOra` >= (CURRENT_DATE - INTERVAL(WEEKDAY(CURRENT_DATE)) DAY) /* first day of current week (Mon - Sun) */
AND `DataOra` < (CURRENT_DATE - INTERVAL(WEEKDAY(CURRENT_DATE)) DAY + INTERVAL 1 WEEK) /* first day of next week (Mon - Sun) */
GROUP BY DATE(`DataOra`)
) `t2` ON `t2`.`maxDataOra` = `t1`.`DataOra`;
Or if using MySQL >= 8.0 you could use the ROW_NUMBER() window function -
SELECT SUM(`Value`) `energiaSettimanale`
FROM (
SELECT `Value`, ROW_NUMBER() OVER (PARTITION BY DATE(`DataOra`) ORDER BY `DataOra` DESC) `rn`
FROM `table`
WHERE `DataOra` >= (CURRENT_DATE - INTERVAL(WEEKDAY(CURRENT_DATE)) DAY) /* first day of current week (Mon - Sun) */
AND `DataOra` < (CURRENT_DATE - INTERVAL(WEEKDAY(CURRENT_DATE)) DAY + INTERVAL 1 WEEK) /* first day of next week (Mon - Sun) */
) `t`
WHERE `rn` = 1;

Related

How to edit this query to get last 7 days visits even if there are no visits on specific days? [duplicate]

This question already has answers here:
MySQL how to fill missing dates in range?
(6 answers)
MySQL: Select All Dates In a Range Even If No Records Present
(6 answers)
Closed 2 years ago.
I have the following query that returns the dates first_visit starting from today and 7 days back, as well as the visitors hash per day:
SET time_zone= '{$company_timezone}';
SELECT DATE( first_visit ) AS day , COUNT( DISTINCT hash ) AS total
FROM table
WHERE company = 1 and first_visit > SUBDATE( CURDATE( ) , 7 )
GROUP BY day
The flaw with this is that if company = 1 have visitors only today and three days ago, I will get this:
day --------- total
2020-03-08 ----- 30
2020-03-05 ----- 40
leaving out all other dates inbetween.
What I want is to get all the past 7 days, even there are no visitors at all. If there are no visitors, then it should just show 0.
How to edit my query in order to achieve this?
Thank you
Perform an outer join with a derived table that contains desired dates:
select b.date as day, count(distinct hash) as total
from table
right join (select #now := #now - interval 1 day as date from (select #now := curdate()) a, table limit 7) b
on b.date = date(first_visit) and company = 1
group by b.date
This assumes that table has at least 7 rows.
Note: there are two occurrences of table.
If you have data for each day -- but not for that company -- then conditional aggregation is a pretty simply approach:
SELECT DATE( first_visit ) AS day ,
COUNT( DISTINCT CASE WHEN company = 1 THEN hash END ) AS total
FROM table
WHERE first_visit > SUBDATE( CURDATE( ) , 7 )
GROUP BY day;
This only works if all days are represented in your table for some company.
Some solutions involve a table of numbers.
Here is one way with a recursive query, available in MySQL 8.0:
with d as (select 0 n union all select n + 1 where n < 6)
select
current_date - interval n day myday,
count(distinct t.hash) total
from d
left join mytable t
on t.company = 1
and t.first_visit >= current_date - inteval n day
and t.first_visit < current_date - interval (n - 1) day
group by d.n
In earlier version, you can enumerate the numbers as a derived table:
select
current_date - interval n day myday,
count(distinct t.hash) total
from (
select 0 n union all select 1 union all select 2 union all select 3
union all select 4 union all select 5 union all select 6 union all select 7
) d
left join mytable t
on t.compatny = 1
and t.first_visit >= current_date - inteval n day
and t.first_visit < current_date - interval (n - 1) day
group by d.n

how to take the count based on first week and second and third week and fourth week

I have one table name (task), here i want take the count like how many registration happend in first and how many registration happend in second how how can do this , i am the new persion of mysql and PHP .
id name t_created_on
1 Kani 2017-03-03 12:45:18
2 yuvi 2017-03-04 12:45:18
3 Mahesh 2017-03-11 12:45:18
Here i am using this format date("Y-m-d h:i:s")
Bassed on my database first week 2 registration and second week is 1.
Expected results:
First Week count : 2
Second week count : 1
1.
SELECT count(*) as records FROM tbl
WHERE t_created_on >= curdate() - INTERVAL DAYOFWEEK(curdate())+6 DAY
AND t_created_on < curdate() - INTERVAL DAYOFWEEK(curdate())-1 DAY
2.
SELECT count(*) as weekrecord FROM tb1
WHERE
WEEK (date) = WEEK( current_date ) -1 AND YEAR( date) = YEAR( current_date );
note: WEEK( current_date ) -1: number indicates number of week.
SELECT WEEK(t_created_on) as week, count(*) as count
FROM my_table
GROUP BY WEEK(t_created_on)
ORDER BY WEEK(t_created_on);
SELECT count(*) as weekrecord, MONTH(current_date) as monthwise
FROM tb1
WHERE
WEEK (date) = WEEK( current_date ) -1
AND YEAR( date) = YEAR( current_date );

MySQL Query results based on month

I really need some help. Not MySQL friendly, muddled through this last few days but now stuck...
Need to take the below query and modify it to pull out only records closed in month of "January" for instance. Is this possible from the below? Cant figure it...
<?php
$recentlyClosedDays = 7;
?>
$query1 = "
SELECT HD_TICKET.ID as ID,
HD_TICKET.TITLE as Title,
HD_STATUS.NAME AS Status,
HD_PRIORITY.NAME AS Priority,
HD_TICKET.CREATED as Created,
HD_TICKET.MODIFIED as Modified,
S.FULL_NAME as Submitter,
O.FULL_NAME as Owner,
HD_TICKET.RESOLUTION as Resolution,
(SELECT COMMENT FROM HD_TICKET_CHANGE WHERE HD_TICKET_ID=HD_TICKET.ID ORDER BY TIMESTAMP DESC LIMIT 1) as Comment,
HD_TICKET.CUSTOM_FIELD_VALUE0 as Type
FROM HD_TICKET
JOIN HD_STATUS ON (HD_STATUS.ID = HD_TICKET.HD_STATUS_ID)
JOIN HD_PRIORITY ON (HD_PRIORITY.ID = HD_TICKET.HD_PRIORITY_ID)
LEFT JOIN USER S ON (S.ID = HD_TICKET.SUBMITTER_ID)
LEFT JOIN USER O ON (O.ID = HD_TICKET.OWNER_ID)
WHERE (HD_TICKET.HD_QUEUE_ID = $mainQueueID)
AND (HD_STATUS.STATE like '%Closed%')
AND (HD_TICKET.TIME_CLOSED >= DATE_SUB(NOW(), INTERVAL $recentlyClosedDays DAY))
ORDER BY HD_TICKET.TIME_CLOSED DESC
";
Any help would be greatly apprecaited and beer will be owed :)
To select DATE, DATETIME, or TIMESTAMP values in the current month, you do this.
WHERE timestampval >= DATE(DATE_FORMAT(NOW(), '%Y-%m-01'))
AND timestampval < DATE(DATE_FORMAT(NOW(), '%Y-%m-01')) + INTERVAL 1 MONTH
For the previous month you can do this:
WHERE timestampval >= DATE(DATE_FORMAT(NOW(), '%Y-%m-01')) - INTERVAL 1 MONTH
AND timestampval < DATE(DATE_FORMAT(NOW(), '%Y-%m-01'))
For the previous year you could do this:
WHERE timestampval >= DATE(DATE_FORMAT(NOW(), '%Y-01-01')) - INTERVAL 1 YEAR
AND timestampval < DATE(DATE_FORMAT(NOW(), '%Y-01-01'))
You can summarize (aggregate) tables by month like this:
SELECT DATE(DATE_FORMAT(timestampval , '%Y-%m-01')) AS month_starting,
SUM(whatever) AS total,
COUNT(whatever) AS transactions
FROM table
GROUP BY DATE(DATE_FORMAT(timestampval , '%Y-%m-01'))
This all works because this expression:
DATE(DATE_FORMAT(sometime, '%Y-%m-01'))
takes an arbitrary sometime value and returns the first day of the month in which the timestamp occurs. Similarly,
DATE(DATE_FORMAT(sometime, '%Y-01-01'))
returns the first day of the year. You can then use date arithmetic like + INTERVAL 1 MONTH to manipulate those first days of months or years.
Here's a more complete writeup on this topic. http://www.plumislandmedia.net/mysql/sql-reporting-time-intervals/

Group rows by 7 days interval starting from a certain date

Is there a way to group rows by a 7 days intervals(datetime) starting from a certain date in Mysql?
SELECT
1 + DATEDIFF(columnDate, #start_date) DIV 7 AS weekNumber
, #start_date + INTERVAL (DATEDIFF(columnDate, #start_date) DIV 7) WEEK
AS week_start_date
, MIN(columnDate) AS actual_first_date
, MAX(columnDate) AS actual_last_date
, SUM(otherColumn)
, AVG(otherColumn)
---
FROM
tableX
WHERE
columnDate >= #start_date
GROUP BY
DATEDIFF(columnDate, #start_date) DIV 7 ;
SELECT *
FROM `table`
GROUP BY WEEK( ADDDATE( `date_column` , WEEKDAY(NOW()) ) )
SELECT users.* from users
WHERE created_at >= '2011-12-01'
AND created_at <= date_add('2011-12-01', INTERVAL 7 DAY)
This selects the users created between 201-12-01 and 7 days after that.
Make changes, to query based on your need.

HOw to SELECT * (all) which date start from the last day of last month to the first day of next month

use PHP and MySQL and want to use SELECT statement which date_post(datetime variable) start at the last date of last month and to date the first day of next month, help me please.
Thank you in advance.
my database: 'id', 'content', 'image', 'date_post',
etc. and I try to use
$today = getdate();
$thisyear=$today['year'];
$thismon=$today['mon'];
$date_start=$thisyear.'-'.$thismon.'-01';
$date_end=$thisyear.'-'.($thismon+1).'-01';
$sql="SELECT *, DATE_FORMAT(date_post, '%d-%m-%Y') AS datepost
FROM my_table
WHERE date_post BETWEEN date('$date_start')
AND date('$date_end')
ORDER BY date_post DESC";
It makes with one query in MySQL, without any PHP:
SELECT * FROM `table_name`
WHERE DATE(`date_post`) >= DATE_FORMAT(CURDATE() - INTERVAL 1 MONTH, CONCAT('%Y-%m-', DAY(LAST_DAY(CURDATE() - INTERVAL 1 MONTH))))
AND DATE(`date_post`) <= DATE_FORMAT(CURDATE() + INTERVAL 1 MONTH, '%Y-%m-01');
Ensuring that the query will not scan the full table but will use the index of date_post (if there is one!):
SELECT *
FROM myTable
WHERE date_post < LAST_DAY(CURDATE())
+ INTERVAL 2 DAY
AND date_Post >= LAST_DAY( LAST_DAY( CURDATE()) - INTERVAL 1 MONTH )
If it is run today ( 2011-07-01 ), it will give all datetimes between 2011-06-31 00:00:00 and 2011-08-01 23:59:59.

Categories