MYSQL 按每个时间戳的同一周比较前一年

问题描述 投票:0回答:1

我有来自不同销售点的大量销售。我想创建一个 MySql 查询,该查询将按日期和商店对销售交易进行分组,同时有一列显示上一年在一周中最近一天的销售额。

这是我的数据集:

|id     |folderCounter_id|incasso |scontrini|timestamp          |
|-------|----------------|--------|---------|-------------------|
|189.712|131             |783,61  |4        |2024-01-08 06:00:00|
|189.713|131             |531,41  |36       |2024-01-08 08:00:00|
|189.714|131             |2.650,45|74       |2024-01-08 09:00:00|
|189.715|131             |4.140,04|141      |2024-01-08 10:00:00|
|189.716|131             |5.236,63|142      |2024-01-08 11:00:00|
|189.717|131             |7.043,62|202      |2024-01-08 12:00:00|
|189.718|131             |3.796,72|118      |2024-01-08 13:00:00|
|189.719|131             |4.375,61|118      |2024-01-08 14:00:00|
|189.720|131             |4.125,11|137      |2024-01-08 15:00:00|
|189.721|131             |4.346,73|134      |2024-01-08 16:00:00|
|189.722|131             |4.848,3 |144      |2024-01-08 17:00:00|
|189.723|131             |6.408,3 |209      |2024-01-08 18:00:00|
|189.724|131             |6.270,29|176      |2024-01-08 19:00:00|
|189.725|131             |3.482,77|113      |2024-01-08 20:00:00|
|189.726|131             |115,24  |2        |2024-01-08 21:00:00|
|189.727|53              |0       |0        |2024-01-08 07:00:00|
|189.728|53              |671,49  |50       |2024-01-08 08:00:00|
|189.729|53              |1.519,28|73       |2024-01-08 09:00:00|
|189.730|53              |2.918,88|122      |2024-01-08 10:00:00|
|189.731|53              |5.628,6 |184      |2024-01-08 11:00:00|
|189.732|53              |4.880,91|165      |2024-01-08 12:00:00|
|189.733|53              |3.288,26|116      |2024-01-08 13:00:00|
|189.734|53              |1.908,28|85       |2024-01-08 14:00:00|
|189.735|53              |3.786,79|113      |2024-01-08 15:00:00|
.
.
.
|189.736|131             |3.672,45|115      |2023-01-09 07:00:00|
|189.737|131             |5.241,46|164      |2023-01-09 09:00:00|
|189.738|131             |5.776,72|162      |2023-01-09 10:00:00|
|189.739|131             |5.830,12|190      |2023-01-09 12:00:00|
|189.740|131             |2.726,45|85       |2023-01-09 13:00:00|
|189.741|131             |0       |0        |2023-01-09 14:00:00|
|189.742|131             |0       |0        |2023-01-09 15:00:00|
|189.743|131             |1.152,64|42       |2023-01-09 16:00:00|

我创建了这个查询:

select t1.date as 'timestamp', t1.folderCounter_id as 'folderCounter_id', t1.incasso as 
'incasso', t2.incasso as 'last_yr_incasso', t1.scontrini as 'scontrini', t2.scontrini 
as 'last_yr_scontrini'
from(
SELECT cr.`timestamp` as date, cr.folderCounter_id, cr.incasso as incasso, cr.scontrini 
as scontrini
from CashRegister cr
group by cr.`timestamp`, cr.folderCounter_id 
) t1
left join(
SELECT cr.`timestamp` as date, cr.incasso as incasso, cr.scontrini as scontrini
from CashRegister cr
group by cr.`timestamp`, cr.folderCounter_id 
) t2
on DATE_SUB(t1.date, INTERVAL 52 WEEK) = t2.date
order by t1.date asc

但是,它不会返回 t1 上不存在的第一个表 t2 的值。

|timestamp          |folderCounter_id|incasso |last_yr_incasso|scontrini|last_yr_contrini|
|-------------------|----------------|--------|---------------|---------|----------------|
|2024-01-08 07:00:00|131             |NULL    |3.672,45       |NULL     |115             | 

如何修改查询以便它也返回不存在的值? 所有时间戳,包括上一年的时间戳,都必须包含在第一列中。

我尝试进行完整的外连接,但无法创建带有时间戳的单个列(在这种情况下,我必须创建 2 个列,否则第一个列具有 NULL 值)

sql mysql group-by pivot
1个回答
0
投票

您必须收集所有日期,然后将两个表左连接到其中。

示意图:

SELECT *
-- collect all existing datetimes, with according correction
FROM (SELECT timestamp FROM table WHERE YEAR(timestamp) = 2024
      UNION DISTINCT
      SELECT timestamp + INTERVAL 52 WEEK FROM table WHERE YEAR(timestamp) = 2023) AS dates
-- join existing data for 2024
LEFT JOIN table t1 ON dates.timestamp = t1.timestamp AND YEAR(timestamp) = 2024
-- join existing data for 2023
LEFT JOIN table t2 ON dates.timestamp - INTERVAL 52 WEEK = t2.timestamp AND YEAR(timestamp) = 2023
© www.soinside.com 2019 - 2024. All rights reserved.