[英]group by date and two different times in the same date in Sql
i want to select from my database the data that is我想从我的数据库中选择数据
BETWEEN to date and two different times in the same date在日期和同一日期的两个不同时间之间
but when I use the Sum in one of the variants it give me a wrong result但是当我在其中一个变体中使用 Sum 时,它给了我错误的结果
i thnik that the problem is in the group by我认为问题出在组中
this is my exemple这是我的例子
date_0 = str(self.dateEdit_49.text())
date_1 = str(self.dateEdit_47.text())
selected_day = self.dateEdit_49.date()
last_day = selected_day.addDays(-1)
last_day_str = last_day.toString(self.dateEdit_49.displayFormat())
'''SELECT date_d, SUM(montant) FROM transactions
WHERE (date_d, time_d) > (%s::date, '16:00:00'::time)
AND (date_d, time_d) < (%s::date, '16:00:00'::time)
GROUP BY (date_d)''',(last_day_str, date_1))
how can i groupe the result with date but date is like this image我如何将结果与日期分组,但日期就像这张图片
You seem to want:你似乎想要:
SELECT date_d, SUM(montant)
FROM transactions
WHERE date_d + time_d > %s::date + '16:00:00'::time
AND date_d + time_d < %s::date + '16:00:00'::time
GROUP BY date_d
If you want to generate groups of rows that start at 4 PM until the next day at 4 PM, then we can offset the dates.如果您想生成从下午 4 点开始到第二天下午 4 点的行组,那么我们可以偏移日期。 A subquery comes handy for this:
一个子查询很方便:
SELECT new_dt, SUM(montant)
FROM (
SELECT t.*, date_d + time_d - interval '16 hour' new_dt
FROM transactions t
) t
WHERE new_dt > %s::date
AND new_dt < %s::date
GROUP BY new_dt
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.