![](/img/trans.png)
[英]Can I get the sum of the day, the week and the month in 1 MS SQL query?
[英]Get the sum of values at the end of a day/month/week
我每小時將不同用戶錢包的余額存儲在數據庫中:
Table: snapshots
id wall_id balance created_at
--------------------------------------------------
...
1300 30 1461.18721408 2019-07-05 18:00:05
1301 30 2922.37442816 2019-07-05 19:00:05
1302 30 228.31050220 2019-07-05 20:00:05
1303 30 2283.10502200 2019-07-05 21:00:05
1304 30 285.38812775 2019-07-05 22:00:05
1305 30 57.07762555 2019-07-05 23:00:05 <--
1306 25 2511.41552420 2019-07-05 18:00:05
1307 25 251.14155242 2019-07-05 19:00:05
1308 25 21735.61642968 2019-07-05 20:00:05
1309 25 22523.28766227 2019-07-05 21:00:05
1310 25 79.90867577 2019-07-05 22:00:05
1311 25 285.38812775 2019-07-05 23:00:05 <--
1312 100 2511.41552420 2019-07-05 18:00:05
1313 100 251.14155242 2019-07-05 19:00:05
1314 100 21735.61642968 2019-07-05 20:00:05
1315 100 22523.28766227 2019-07-05 21:00:05
1316 100 79.90867577 2019-07-05 22:00:05
1317 100 285.38812775 2019-07-05 23:00:05
1318 30 1461.18721408 2019-07-06 18:00:05
1319 30 2922.37442816 2019-07-06 19:00:05
1320 30 228.31050220 2019-07-06 20:00:05
1321 30 2283.10502200 2019-07-06 21:00:05
1322 30 285.38812775 2019-07-06 22:00:05
1323 30 79.90867577 2019-07-06 23:00:05 <--
1324 25 2511.41552420 2019-07-06 18:00:05
1325 25 251.14155242 2019-07-06 19:00:05
1326 25 21735.61642968 2019-07-06 20:00:05
1327 25 22523.28766227 2019-07-06 21:00:05
1328 25 79.90867577 2019-07-06 22:00:05
1329 25 21735.61642968 2019-07-06 23:00:05 <--
1330 100 2511.41552420 2019-07-06 18:00:05
1331 100 251.14155242 2019-07-06 19:00:05
1332 100 21735.61642968 2019-07-06 20:00:05
1333 100 22523.28766227 2019-07-06 21:00:05
1334 100 79.90867577 2019-07-06 22:00:05
1335 100 285.38812775 2019-07-06 23:00:05
...
現在,我想獲取在wall.id = 30 && 25的每一天所做的最后快照的總和。
為了理解算法,我標記了應該添加的值。
鐵
Example result for wall_id 30 & 25 on EOD
day sumEOD
--------------------------------------------------
2019-07-05 00:00:00 342.4657533
2019-07-06 00:00:00 21815.52510545
另外,應該可以對其進行修改,並獲取在wall.id = 30 && 25的情況下每周進行的最后快照的總和。
實現這一目標的最簡單方法是什么? 我正在運行postgresql。
SELECT
created_date,
SUM(balance) AS sum_balance
FROM (
SELECT DISTINCT ON (1, 2)
wall_id,
created_at::date AS created_date,
balance
FROM
snapshots
WHERE wall_id IN (25, 30)
ORDER BY 1, 2, created_at DESC
) s
GROUP BY created_date
DISTINCT ON
為您提供有序組的第一條記錄。 在這種情況下,組為(wall_id, created_at::date)
。 created_at::date
給出了時間戳的一部分。 當然是按組排序,並且在其中,記錄按其時間戳記DESC
排序,該時間戳記每天將最近的記錄排序到組的頂部。 這是DISTINCT ON
采取的。
之后,您可以簡單地將結果分組。
首先找到每天用group by wall_id, date(created_at)
的最新快照,然后加入表並按天獲取總和:
select
date(s.created_at) "day",
sum(s.balance) sumEOD
from snapshots s inner join (
select wall_id, max(created_at) maxdate
from snapshots
where wall_id in ('25', '30')
group by wall_id, date(created_at)
) g on g.wall_id = s.wall_id and g.maxdate = s.created_at
group by "day"
order by "day"
參見演示 。
結果:
| day | sumeod |
| ------------------------ | -------------- |
| 2019-07-05T00:00:00.000Z | 342.4657533 |
| 2019-07-06T00:00:00.000Z | 21815.52510545 |
如果您想獲得每周的總和,則可以更改:
group by wall_id, date(created_at)
至:
group by wall_id, date_trunc('week', created_at)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.