Hi,
I am attempting to take data from a table and summarise it in part in a view. The reason for this is to denormalise the weekly rows over columns, replacing twelve weekly rows with one row with twelve weekly columns.
The relevant section of code looks like this:
This excerpt focuses on the numeric fields, but as you can see, the fields starting with inv should not accumulate over weeks. Rather, I just want to take the latest value and populate these columns.
Using the above code, if I group by non aggregated columns, I end up inflating the total inv values.
e.g.
What should happen is that the sum non-summarised fields of the summarised table should reveal the same value as the original table.
Is there a simple way to achieve this? Because I'm missing what it is.
Thanks
I am attempting to take data from a table and summarise it in part in a view. The reason for this is to denormalise the weekly rows over columns, replacing twelve weekly rows with one row with twelve weekly columns.
The relevant section of code looks like this:
Code:
...
sum(Case When replace(right(fweek,2),'.','') in (13,27,40,53) Then ts_adjd_qty End) as week13a,sum(bill_qty) as bill_qty,
sum(bill_amt_$) as bill_amt_$,
sum(ts_adjd_qty) as ts_adjd_qty,
sum(inv_rptd_qty) as inv_rptd_qty,
inv_demo_qty as inv_demo_qty,
inv_boh_qty as inv_boh_qty,
inv_coh_qty as inv_coh_qty,
inv_eoh_qty as inv_eoh_qty
FROM tbl
Using the above code, if I group by non aggregated columns, I end up inflating the total inv values.
e.g.
Code:
select sum(inv_boh_qty) from xxx where fiscal_week = 52: (167765)
select sum(inv_boh_qty) from summarised_xxx: (345203)
What should happen is that the sum non-summarised fields of the summarised table should reveal the same value as the original table.
Is there a simple way to achieve this? Because I'm missing what it is.
Thanks