postgres中的时间戳比较
Timestamps comparison in postgres
我有两个具有相似数据的查询(一个与上一时期有关,另一个与当前时期有关)。我需要在 kendo 折线图中显示它。我该怎么做**
select date_trunc('hour' , "CreateDateTime"::TIMESTAMP) as c,
count(1) as d
from "ROBOT"
where "CreateDateTime"::TIMESTAMP between '2018-04-01' and '2018-04-22'
group by date_trunc('hour', "CreateDateTime"::TIMESTAMP)
UNION ALL
select date_trunc('hour' , "CreateDateTime"::TIMESTAMP) as a,
count(1) as b
from "ROBOT"
where "CreateDateTime"::TIMESTAMP between '2018-05-01' and '2018-05-22'
group by date_trunc('hour', "CreateDateTime"::TIMESTAMP);
您似乎想要并排计算两个月的每小时计数。假设这是正确的,你想要这样的东西:
select date_part('hour', "CreateDateTime"::TIMESTAMP) as hour,
sum(case when "CreateDateTime"::TIMESTAMP between '2018-04-01' and '2018-04-22' then 1 else 0 end) as apr,
sum(case when "CreateDateTime"::TIMESTAMP between '2018-05-01' and '2018-05-22' then 1 else 0 end) as may
from "ROBOT"
where "CreateDateTime"::TIMESTAMP between '2018-04-01' and '2018-04-22'
or "CreateDateTime"::TIMESTAMP between '2018-05-01' and '2018-05-22'
group by 1;
当您想从具有不同条件的相同数据生成多个总和时,sum(case...) 模式是一个很好的选择。
我有两个具有相似数据的查询(一个与上一时期有关,另一个与当前时期有关)。我需要在 kendo 折线图中显示它。我该怎么做**
select date_trunc('hour' , "CreateDateTime"::TIMESTAMP) as c,
count(1) as d
from "ROBOT"
where "CreateDateTime"::TIMESTAMP between '2018-04-01' and '2018-04-22'
group by date_trunc('hour', "CreateDateTime"::TIMESTAMP)
UNION ALL
select date_trunc('hour' , "CreateDateTime"::TIMESTAMP) as a,
count(1) as b
from "ROBOT"
where "CreateDateTime"::TIMESTAMP between '2018-05-01' and '2018-05-22'
group by date_trunc('hour', "CreateDateTime"::TIMESTAMP);
您似乎想要并排计算两个月的每小时计数。假设这是正确的,你想要这样的东西:
select date_part('hour', "CreateDateTime"::TIMESTAMP) as hour,
sum(case when "CreateDateTime"::TIMESTAMP between '2018-04-01' and '2018-04-22' then 1 else 0 end) as apr,
sum(case when "CreateDateTime"::TIMESTAMP between '2018-05-01' and '2018-05-22' then 1 else 0 end) as may
from "ROBOT"
where "CreateDateTime"::TIMESTAMP between '2018-04-01' and '2018-04-22'
or "CreateDateTime"::TIMESTAMP between '2018-05-01' and '2018-05-22'
group by 1;
当您想从具有不同条件的相同数据生成多个总和时,sum(case...) 模式是一个很好的选择。