Skip to content
Advertisement

Round timestamp to 30 minutes interval to group by

Problem

I want to group my data by currency, timestamp and show all type value like table of comparison with interval of 30 minutes, for now my created time is 1 minute or less different so if i group with created time it will still showing 4 rows cause of different timestamp, is there a way to round the timestamp ?

Data Source

Type Currency Rate Created
Bank A USD 3.4 2020-01-01 12:29:15
Bank B USD 3.34 2020-01-01 12:30:11
Bank A EUR 4.92 2020-01-01 12:31:01
Bank B EUR 5.03 2020-01-01 12:31:14

Expected Result

Timestamp Currency Bank A Rate Bank B Rate
2020-01-01 12:30:00 USD 3.4 3.34
2020-01-01 12:30:00 EUR 4.92 5.03

Advertisement

Answer

Truncate/round created to 30 minutes (the ts expression) and group by it. Your query with this amendment:

SQL Fiddle

‘Inherit’ previous rate

SQL Fiddle

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement