Let’s say this is the sample date coming from a join of 2 tables. Database is Postgres 9.6
id product_id invoice_id amount date 1 PROD1 INV01 2 01-01-2018 2 PROD2 INV02 3 01-01-2018 3 PROD1 INV01 2 05-01-2018 4 PROD1 INV03 1 05-01-2018 5 PROD2 INV02 3 08-01-2018 6 PROD2 INV04 4 08-01-2018
I want to know if it’s possible in a optimized way to:
- Get all the PRODx with their respective INVx which have the latest date, but per product_id. Please note that records unused from a day may be reported to a new one. This means:
id product_id invoice_id amount date 3 PROD1 INV01 2 05-01-2018 4 PROD1 INV03 1 05-01-2018 5 PROD2 INV02 3 08-01-2018 6 PROD2 INV04 4 08-01-2018
- Get daily summed amounts for each PRODx but fill the gaps with the previous ones if day does not exist.
product_id amount date PROD1 2 01-01-2018 PROD2 3 01-01-2018 PROD1 2 02-01-2018 PROD2 3 02-01-2018 PROD1 2 03-01-2018 PROD2 3 03-01-2018 PROD1 2 04-01-2018 PROD2 3 04-01-2018 PROD1 3 05-01-2018 PROD2 3 05-01-2018 PROD1 3 06-01-2018 PROD2 3 06-01-2018 PROD1 3 07-01-2018 PROD2 3 07-01-2018 PROD1 3 08-01-2018 PROD2 7 08-01-2018
A few thoughts:
For first question I could obtain the
max(date)for each PRODx and the pick for each PRODx the rows that have the
date=with max(date)but I was wondering if there’s faster way to obtain this given a large number of recors in the database
For the second question, I could generate a series of dates for the interval needed and then use
WITH rows Asand do the query grouping by
sumby amount and then select for each date the previous values from
limit 1but that does not sound that optimized either.
Looking forward for any input. Thank you.
✓ Extra quality
ExtraProxies brings the best proxy quality for you with our private and reliable proxies
✓ Extra anonymity
Top level of anonymity and 100% safe proxies – this is what you get with every proxy package
✓ Extra speed
1,ooo mb/s proxy servers speed – we are way better than others – just enjoy our proxies!
USA proxy location
We offer premium quality USA private proxies – the most essential proxies you can ever want from USA
Our proxies have TOP level of anonymity + Elite quality, so you are always safe and secure with your proxies
Use your proxies as much as you want – we have no limits for data transfer and bandwidth, unlimited usage!
Superb fast proxy servers with 1,000 mb/s speed – sit back and enjoy your lightning fast private proxies!
99,9% servers uptime
Alive and working proxies all the time – we are taking care of our servers so you can use them without any problems
No usage restrictions
You have freedom to use your proxies with every software, browser or website you want without restrictions
Perfect for SEO
We are 100% friendly with all SEO tasks as well as internet marketing – feel the power with our proxies
Buy more proxies and get better price – we offer various proxy packages with great deals and discounts
We are working 24/7 to bring the best proxy experience for you – we are glad to help and assist you!