Context:FIT3003_MOC Β· fit the least-squares line inside Oracle over two numeric columns Β· formula and interpretation in Linear and Polynomial Regression Β· technique map in Data Analytics for Data Warehousing Β· Lab 10 script β 30_Projects/FIT3003_Labs/Lab10_regression_in_SQL.sqlProblem it solves: slope, intercept and a predicted value y^β for every x, with no tool outside SQL.
Quick Revision
π― Trigger: βtrend lineβ, βbest fitβ, βpredict the measure from timeβ β three nested layers: means β slope β intercept; then cross-join the one-row result back onto the data for y^β.
β οΈ Key Constraint:xΛ,yΛβ must reach every row β a one-row inline view cross-joined to dataset (lecture) or avg(x) over () (lab); then carry them up as max(x_bar) β any aggregate of a constant returns the constant.
π§ Minimal Working Example
DATASET(xβ,y) β Table 1.5, x= Time, y= Value: (1,11),(2,27),(3,34),(4,38),(5,45),(6,61),(7,63).
select slope, y_bar_max - (slope * x_bar_max) as intercept -- 3. b0 = Θ³ β b1Β·xΜfrom ( select sum((x - x_bar) * (y - y_bar)) / sum((x - x_bar) * (x - x_bar)) as slope, -- 2. b1 max(x_bar) as x_bar_max, max(y_bar) as y_bar_max from (select avg(x) as x_bar, avg(y) as y_bar from dataset) av, dataset -- 1. one-row means Γ every row);
Expected output: one row β slope=8.392857, intercept=6.285714.
Hand shortcut(arithmetic aid) β β(xiββxΛ)(yiββyΛβ)=β(xiββxΛ)yiβ since β(xiββxΛ)=0 β the line above uses it.
π Variations
Prediction table β CTAS with y^β
create table linear_regression asselect x, y, (intercept + (slope * x)) as y_predfrom dataset, (select slope, y_bar_max - (slope * x_bar_max) as intercept from (select sum((x - x_bar) * (y - y_bar)) / sum((x - x_bar) * (x - x_bar)) as slope, max(x_bar) as x_bar_max, max(y_bar) as y_bar_max from (select avg(x) as x_bar, avg(y) as y_bar from dataset) av, dataset))order by x;
Expected output: 7 rows, y_pred=14.68,23.07,31.46,39.86,48.25,56.64,65.04 β the slideβs βregression as discrete valuesβ; residual at x=1 is 11β14.68=β3.68.
Same query, new shell β the slope/intercept query drops into the from as a one-row view, cross-joined to dataset so each row sees the single (b1β,b0β) pair.
Means by window β avg(x) over () (Lab 10)
GLUCOSEALBUMIN(Glucose,Albumin) β the 17 patients of Table 1.6, both number(18,6).
select slope, y_bar_max - x_bar_max * slope as interceptfrom ( select sum((Glucose - x_bar) * (Albumin - y_bar)) / sum((Glucose - x_bar) * (Glucose - x_bar)) as slope, max(x_bar) as x_bar_max, max(y_bar) as y_bar_max from (select Glucose, avg(Glucose) over () as x_bar, Albumin, avg(Albumin) over () as y_bar from GlucoseAlbumin) s);
Expected output:slope=0.0077, intercept=2.4959 β Albumin rises 0.0077 per unit of Glucose.
Empty over () β the window is the whole result set, so the grand mean repeats on every row β no second copy of the table, no cross join.
Lecture vs lab form β same three layers, same answer; the window form reads one source table, the cross-join form works without window functions.
βοΈ Practice
Practice 1: by hand, fit the line to (1,2),(2,4),(3,5),(4,4),(5,5) and predict y at x=6.
Key move: intercept after slope β b0β needs b1β.
Practice 2: build dataset from SalesFact as MEL's monthly total sales, ready for the regression query.
Reference solution
create table dataset asselect row_number() over (order by S.TimeID) as x, sum(Total_Sales) as yfrom SalesFact Swhere S.LocationID = 'MEL'group by S.TimeID;
Key move:group by first (one row per month), then index the months 1,2,3,β¦ β TimeID codes are not evenly spaced numbers (201812β201901 jumps 89).
Practice 3 (Lab 10 Task 4β5): load the 49 (Age,WBC) points into Age_WBC with Age numeric(3,0) as the lab does, fit the line, and explain why Oracle returns intercept 20.1574, slope β0.0919 when the same points fit in Python give 20.1455, β0.0918.
Reference solution
select slope, y_bar_max - x_bar_max * slope as interceptfrom ( select sum((Age - x_bar) * (WBC - y_bar)) / sum((Age - x_bar) * (Age - x_bar)) as slope, max(x_bar) as x_bar_max, max(y_bar) as y_bar_max from (select Age, avg(Age) over () as x_bar, WBC, avg(WBC) over () as y_bar from Age_WBC) s);
Key move:numeric(3,0) has scale 0, so Oracle rounds56.3β56 on insert β no error, a different x β the labβs check values belong to integer ages; number(4,1) keeps the decimal and gives 20.1455, β0.0918 (verified by recomputation).
Reading: negative slope β white-blood-cell count falls about 0.09 per year of age.
β οΈ Common Mistakes
π‘ group by x, y inside the means view β the lab solution does this; it collapses repeated (x,y) points to one, silently reweighting the fit β harmless on Lab 10βs all-distinct pairs, wrong on real fact rows β drop it.
π‘ Bare x_bar beside sum(...) β a non-aggregated column with no group by raises ORA-00937; max(x_bar) is the legal carrier.
π‘ Adding a join condition to av, dataset β there is no shared key; the comma-list is the intended Cartesian product of a one-row view.
π‘ Slope alias in its own layer β slope is invisible beside its definition; the intercept needs the outer select.