php SQL -按当前周的日期对最后一个值求和

fzsnzjdm  于 2023-01-16  发布在  PHP
关注(0)|答案(4)|浏览(170)

我需要对当前一周的每一天的最后一个值(按日期)求和。为了测试,我有一个包含这些值的表:

5 for 2023-01-12 16:53:01
2 for 2023-01-12 08:49:03
5 for 2023-01-11 08:58:19

我期望结果为10。
下面的代码是:

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;

但是结果是5。哪里错了?谢谢

iqxoj9l9

iqxoj9l91#

也许我找到了解决办法

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

ykejflvf2#

如果我真的理解了你想要什么,这个请求可能就是解决方案(如果我理解正确的话,我就是在指定)

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

查找一天中最近日期的技巧是使用dateTime Data上的max()函数按天数分组。

u5i3ibmn

u5i3ibmn3#

可以使用inner join执行此操作,如下所示:

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;

点击这里查看:https://dbfiddle.uk/hadzywwh

6pp0gazn

6pp0gazn4#

我需要对当前周的每一天的最后一个值(按日期)求和。
所以,让我们先把工作集限制为当前周的数据-

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

然后,我们可以使用它来约束用于获取每个DATE(DataOra)的MAX(DataOra)的查询,并联接回原始数据集以获取各个Value s的总和-

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

或者,如果使用MySQL〉= 8.0,则可以使用ROW_NUMBER()窗口函数-

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;

相关问题