i have been seeking a good way to calculate excel like trendlines for my desktop app's dashboard graps.
most common trend types must be like
Type = 1 Linear Y = a + b*X
Type = 2 EXponential Y = a*e^(b*X)
Type = 3 Logarithmic Y = a + b*ln(X)
Type = 4 Power Y = a*X^b
so here is how i solved my issue.
2条答案
按热度按时间hm2xizp91#
As I mentioned in my other comment, this could be written as a inline TVF, instead of a multiline TVF. I've taken the OP's solution somewhat literally, however, a couple of CTEs allows us to do this:
This returns exactly the same results as the OP's answer.
2wnc66cl2#
we need an input type to make it an SQL function
X can be a datetime if you want to work with time series. i try to make this function as simple as possible
here is an example
x15 and x16 are there for extrapolations. you simply can copy results and paste it into an excel sheet to check with graphs or builtin excel functions.
important note: type 3,4 can't work with negative X values, 2,4 can't work with negative Y values. preparing the data is important before you calculate the certain types.