Hello,
I'm trying to write a select where I can get part numbers sold in the last hour. The thing is, I'm having difficulties in getting the results right. Here's the info...
select c.partnumber,o.timeplaced,count(o.orders_id) AS count
from orders o
inner join orderitems oi on (o.orders_id = oi.orders_id)
...
and o.timeplaced > current_timestamp - interval '1' hour
group by c.partnumber,o.timeplaced;
This all works fine. The issue is that since timeplaced is a timestamp, it goes down to the millisecond. Therefore, I'm not able to get the grouping I'm looking for. I really only want orders placed in the last hour. I really don't care about doing any ordering, although being able to order by the count would be nice.
How can I run my query but only get results in the last hour so the grouping will be better? For example, in my current query, the results look something like this...
PARTNUMBER TIMEPLACED COUNT
123456 10/16/2018 7:22:23.325000 AM 1
123456 10/16/2018 7:34:40.448000 AM 1
123456 10/16/2018 7:54:51.566000 AM 1
123457 ...
When I'd like it to be:
PARTNUMBER TIMEPLACED COUNT
123456 10/16/2018 07 AM 3
123457 ...
I did try using TO_CHAR(o.timeplaced,'yyyy-mm-dd hh24') in my select, but the results are basically the same. I'm wondering if this is because of the "and o.timeplaced > current_timestamp - interval '1' hour" while the "TO_CHAR(o.timeplaced,'yyyy-mm-dd hh24')" is just for display.:
PARTNUMBER TIMEPLACED COUNT
123456 10/16/2018 07 AM 1
123456 10/16/2018 07 AM 1
123456 10/16/2018 07 AM 1
Thanks,
Larry
and trunc(o.timeplaced,'HH') > trunc(current_timestamp,'H
if there is an index on timeplaced, this will ignore it. If you need the index used, we can tweak it.