I'm new to writing reports in PostegrSQL. Any help will be greatly appreciated.
All our inventory is entered into the database with a date_in and cost fields. I'm trying to pull this data out sorting it by category then summing it by date group (i.e., 0-90 days) within each category.
The report runs fine with just the totals by category (before I try to break it out by date). I can't seem to figure out the right way to break the totals out into the date groups I want.
This is my code so far.
I've got the feeling I'm missing something very simple in how I'm writing this. Can anyone point me in the right direction.
Thanks
All our inventory is entered into the database with a date_in and cost fields. I'm trying to pull this data out sorting it by category then summing it by date group (i.e., 0-90 days) within each category.
The report runs fine with just the totals by category (before I try to break it out by date). I can't seem to figure out the right way to break the totals out into the date groups I want.
This is my code so far.
Code:
select store.short_name || ' (' || store.store_id || ')' as \"Store\",
inventory.description as \"Description\",
category_number as \"Category\",
sum (cost) as \"Total\",
(select inventory.cost,
CASE
when inventory.date_in between today and today - ('90 days') then sum end as \"0-90 Days\"
when inventory.date_in between today - ('91 days') and today - ('120 days') then sum end as \"91-120 Days\"
when inventory.date_in between today - ('121 days') and today - ('180 days') then sum end as \"121-180 Days\"
when inventory.date_in between today - ('181 days') and today - ('240 days') then sum end as \"181-240 Days\"
else inventory.date_in between today - ('241 days') and today -('infinity') then sum end as \"240+ Days\"
)
from store
join inventory on store.store_id = inventory.store_id
where store.store_id in $stores
group by store.short_name, store.store_id, inventory.description, store.sort_id, inventory.category_number
order by sort_id,category_number
I've got the feeling I'm missing something very simple in how I'm writing this. Can anyone point me in the right direction.
Thanks