任何人都可以纠正此查询吗?
select distinct
v.DriverName,t.KM_W1,t.KM_W2,t.KM_W3,t.KM_W4,t.KM_W5,(t.KM_W1+t.KM_W2+t.KM_W3+t.KM_W4+t.KM_W5) Total
from
(select
DriverName,
SUM( case when TodayDate >= '01-07-2018' and TodayDate <= '07-07-2018' then PaidKM else 0 end) KM_W1,
SUM( case when TodayDate >= '07-07-2018' and TodayDate <= '14-07-2018' then PaidKM else 0 end) KM_W2,
SUM( case when TodayDate >= '14-07-2018' and TodayDate <= '21-07-2018' then PaidKM else 0 end) KM_W3,
SUM( case when TodayDate >= '21-07-2018' and TodayDate <= '28-07-2018' then PaidKM else 0 end) KM_W4,
SUM( case when TodayDate >= '28-07-2018' and TodayDate <= '31-07-2018' then PaidKM else 0 end) KM_W5
from
Traveling
group by
DriverName) t
inner join
Traveling v on t.DriverName = v.DriverName
where
TodayDate >= '01-07-2018' and TodayDate <= '31-07-2018'
以上查询说明::我正在获取驱动程序在一个月中获得的所有星期的数据PaidKM
。例如,月份的开始日期为01-07-2018
,结束日期为31-07-2018
,涵盖整个月份#07
。但是当我更改月份时,它总是给我相同的结果。
在GIF的查询实现之上:我已经在Java中实现了此查询,并且当我从组合框更改月份时,后端查询始终给出所有记录的结果,而不是不同的月份。 >
上图是数据库中的Traveling
表。
更改其他月份后如何获得结果?以及如何更正此查询以使其工作。
任何帮助将不胜感激!预先谢谢你!
答案 0 :(得分:2)
您的问题是比较是基于字符串的,而不是基于日期的。 天的优先级高于月份,而月份的优先级高于年份。
例如 31-01-1970 被认为大于 01-01-2018 ,因为 31 大于 01 。
您应该考虑使用公认的日期格式,该格式可以直接排序/比较。 YYYY-MM-DD (每次使用/存储日期)。
您不妨考虑看看SQL As Understood By SQLite - Date And Time Functions - Time Strings。
您可以使用
将日期转换为可排序/易于比较的日期substr(datevalue,7,4)||substr(datevalue,3,4)||substr(datevalue,1,2)
例如代替:-
SUM( case when TodayDate >= '01-07-2018' and TodayDate <= '07-07-2018' then PaidKM else 0 end) KM_W1, ....
您可以使用:-
SUM( case when substr(TodayDate,7,4)||substr(Todaydate,3,4)||substr(Todaydate,1,2) >= '2018-07-01' and substr(TodayDate,7,4)||substr(Todaydate,3,4)||substr(Todaydate,1,2) <= '2018-07-07' then PaidKM else 0 end) KM_W1, ....
整个查询可能是:-
select distinct
v.DriverName,t.KM_W1,t.KM_W2,t.KM_W3,t.KM_W4,t.KM_W5,(t.KM_W1+t.KM_W2+t.KM_W3+t.KM_W4+t.KM_W5) Total
from
(select
DriverName,
SUM( case when substr(TodayDate,7,4)||substr(Todaydate,3,4)||substr(Todaydate,1,2) >= '2018-07-01' and substr(TodayDate,7,4)||substr(Todaydate,3,4)||substr(Todaydate,1,2) <= '2018-07-07' then PaidKM else 0 end) KM_W1,
SUM( case when substr(TodayDate,7,4)||substr(Todaydate,3,4)||substr(Todaydate,1,2) >= '2018-07-08' and substr(TodayDate,7,4)||substr(Todaydate,3,4)||substr(Todaydate,1,2) <= '2018-07-14' then PaidKM else 0 end) KM_W2,
SUM( case when substr(TodayDate,7,4)||substr(Todaydate,3,4)||substr(Todaydate,1,2) >= '2018-07-15' and substr(TodayDate,7,4)||substr(Todaydate,3,4)||substr(Todaydate,1,2) <= '2018-07-21' then PaidKM else 0 end) KM_W3,
SUM( case when substr(TodayDate,7,4)||substr(Todaydate,3,4)||substr(Todaydate,1,2) >= '2018-07-22' and substr(TodayDate,7,4)||substr(Todaydate,3,4)||substr(Todaydate,1,2) <= '2018-07-28' then PaidKM else 0 end) KM_W4,
SUM( case when substr(TodayDate,7,4)||substr(Todaydate,3,4)||substr(Todaydate,1,2) >= '2018-07-29' and substr(TodayDate,7,4)||substr(Todaydate,3,4)||substr(Todaydate,1,2) <= '2018-07-31' then PaidKM else 0 end) KM_W5
from
Traveling
group by
DriverName) t
inner join
Traveling v on t.DriverName = v.DriverName
where
substr(TodayDate,7,4)||substr(Todaydate,3,4)||substr(Todaydate,1,2) >= '2018-07-01' and substr(TodayDate,7,4)||substr(Todaydate,3,4)||substr(Todaydate,1,2) <= '2018-07-31'
答案 1 :(得分:1)
首先,这里有两个潜在的问题。您的查询中没有输入变量,因此我们必须先询问这一点。
您是否在语句末尾对WHERE
子句的参数传递进行了硬编码?
您是否在子查询中为SUM
语句的参数传递进行了硬编码?那些将月份分成几周的部分。
这些都是导致结果不变的原因。
此查询的一个肯定的问题是,您将需要重复计算表示一周开始/结束的那几天的行驶时间。
...SUM( case when TodayDate >= '01-07-2018' and TodayDate <= '07-07-2018' then PaidKM else 0 end) KM_W1,
SUM( case when TodayDate >= '08-07-2018' and TodayDate <= '14-07-2018' then PaidKM else 0 end) KM_W2,...
您需要增加下周开始前的日期
我在这里通过将07-07-2018
更新为08-07-2018