我有一个看起来像
的表<svg xmlns="http://www.w3.org/2000/svg" width="200" height="200">
<style>
text {
font-family: sans-serif;
font-size: 30px;
}
tspan {
alignment-baseline: central;
}
</style>
<circle cx="50%" cy="50%" r="45%" stroke="tomato" stroke-width="8" stroke-dasharray="20" fill="none" />
<text x="50%" y="50%" text-anchor="middle">
<tspan x="50%" dy="-2em">First row</tspan>
<tspan x="50%" dy="1em">Second row</tspan>
<tspan x="50%" dy="1em">Third row</tspan>
<tspan x="50%" dy="1em">Fourth row</tspan>
<tspan x="50%" dy="1em">Fifth row</tspan>
</text>
</svg>
我还有另一个桌子
Order
OrderId
OrderStatus
OrderTime
我正在尝试获取两个日期之间的每小时平均金额,即可以是一天的开始和结束时间,也可以是一周的开始和结束时间。 因此,例如,如果有两个订单分别在上午9点和上午9:30以50英镑的价格下达。然后我试图得到:
OrderId
OrderLineId
OrderLineAmount
OrderLineCost
这是否有意义?
我目前有这个,但还不能完全满足我的需要
20/07/2018 09:00:00 - 20/07/2018 10:00:00
2
100
任何帮助都会很棒,谢谢
答案 0 :(得分:1)
您在这里:
create table ord (
orderid int,
orderstatus int,
ordertime timestamp
);
insert into ord values (1, 1, '2018-07-20 09:10:00');
insert into ord values (2, 1, '2018-07-20 09:40:00');
insert into ord values (3, 1, '2018-07-20 10:15:00');
create table orderline (
orderid int,
orderlineid int,
orderlineamount int,
orderlinecost int
);
insert into orderline values (1, 100, 50, 40);
insert into orderline values (2, 100, 25, 40);
insert into orderline values (2, 100, 25, 40);
insert into orderline values (3, 10, 1234, 40);
select
date_trunc('hour', o.ordertime),
count(distinct o.orderid),
sum(l.orderlineamount)
from ord o
join orderline l on l.orderid = o.orderid
group by date_trunc('hour', o.ordertime)
order by date_trunc('hour', o.ordertime);
结果:
date_trunc count sum
---------------------------------------------------------------------------
2018-07-20 09:00:00.0 2 100
2018-07-20 10:00:00.0 1 1234