I do the statistic function for my project. I have the order table with some fields as:
prouct_id,amount,order_date.
The customer ask me to do the auto scale statistic as: I get the MIN(order_date) of a produc_id and the current date to calculate the days:
-
If the days about ~ 1 month ==> show the statistic of the product by weeks
-
If the days about ~ 1 year ==> show the statistic of the product by months
-
If the days >= 2 year ==> show the statistic of the product by year
I hope that can receive the ideas, examples from all of you about making the statistic as above.
You didn’t specify what RDBMS is this. So let me assume that it is SQL Server, and you can do this:You didn’t specify what statistics do you want to compute. But you should be able to do whatever statistics you want to do using the last cte:
DatesWithIntervalsdepending on the fieldIntervaland I will leave it as a practice for you.Edit: For MySQL, just replace all these
CTEs with subqueries like so: