I'm building a ruby on rails application that uses raw SQL to query my database because I heard that it performs better than using ActiveRecord and I will be handling millions of records.
Lets say for simplicity’s sake, I have the following records in a Table1:
<id: 1, price: 20, quantity: 2, date: "2020-01-01T10:02:32">
<id: 2, price: 5, quantity: 1, date: "2020-01-01T10:32:12">
<id: 3, price: 10, quantity: 3, date: "2020-01-01T12:01:10">
What I want to do is get the total price * quantity per each hour as a hash or anything that makes sense. So in this case, the results would look like this:
{“2020-01-01 10:00:00”: 45, “2020-01-01 12:00:00”: 30}
As you can see, the value at 2020-01-01 10:00:00 is 45 and we got this form doing (20*2)+(5*1) since these records both have a date within the same hour.
Now originally, I had a simple loop in ruby that looped through this table and returned the desired results however I later learned that raw sql performs much better with larger data. I’m wondering how I can get this results using raw sql. I'm using postgresql. Any type of help is greatly appreciated. Sorry if it’s a noob question.
EDIT I changed the timestamps to be type string since that is how I'm getting the data.