a subquery is provided to create a view and the view should cater to dynamic month variable.
the subquery like this:
select col1,col2,col3,col4, sum(money) as KPI_single_month from table1 group by col1,col2,col3,col4 where month='202104'
-- month is not fixed, maybe 202103,202106
the view should be called like this:
select col1,col2,col3,col4,KPI from view1 where month='202104'
--month is not fixed, maybe 202103,maybe 202106
the key output of this view is KPI, which is the sum of KPI_single_month from 202101 to 202104. if month is 202106, then sum(KPI_single_month) from January to June(total 6 months).
so the difficulty of creating view is that the month variable is unknown, how many subquery should I sum in the SQL statement of creating view? Thanks!