Other SQUARE SQL function
1. Usage of SQUARE to show Price Variance Analyzing
SQL Server Query 1
WITH CategoryAvgPrices AS (
SELECT
CategoryID,
AVG(UnitPrice) AS AvgCategoryPrice
FROM Products
GROUP BY CategoryID
)
SELECT
p.ProductID,
p.ProductName,
p.CategoryID,
c.CategoryName,
p.UnitPrice,
cap.AvgCategoryPrice,
SQUARE(p.UnitPrice - cap.AvgCategoryPrice) AS SquaredPriceDifference
FROM Products p
JOIN CategoryAvgPrices cap ON p.CategoryID = cap.CategoryID
JOIN Categories c ON p.CategoryID = c.CategoryID
ORDER BY p.CategoryID, p.ProductID;
Create SQL query with SqlQueryBuilder 1
var (sql1, parameters1) = new SqlQueryBuilder()
.WithCTETable(new Table("CategoryAvgPrices"), new SqlQueryBuilder()
.Select().Column("CategoryID", "CategoryID")
.Column(new AVG(new Column("UnitPrice")), "AvgCategoryPrice")
.From("Products")
.GroupBy(new GroupBy(new Column("CategoryID")))
)
.Select()
.Columns("p.ProductID", "p.ProductName", "p.CategoryID", "c.CategoryName", "p.UnitPrice", "cap.AvgCategoryPrice")
.Column(new SQUARE(new ColumnArithmatic("p.UnitPrice").SUBTRACT("cap.AvgCategoryPrice")), "SquaredPriceDifference")
.From("Products", "p")
.Join(new List<IJoin>()
{
new INNERJOIN().TableName(new Table("CategoryAvgPrices","cap"))
.On(new Column("p.CategoryID").Equale(new Column("cap.CategoryID"))),
new INNERJOIN().TableName(new Table("Categories","c"))
.On(new Column("p.CategoryID").Equale(new Column("c.CategoryID")))
})
.OrderBy(new OrderBy().SetColumnAscending("p.CategoryID")
.SetColumnAscending("p.ProductID"))
.Build();
Query build by SqlQueryBuilder 1
WITH CategoryAvgPrices
AS (SELECT CategoryID AS CategoryID,
AVG(UnitPrice) AS AvgCategoryPrice
FROM Products
GROUP BY CategoryID)
SELECT p.ProductID,
p.ProductName,
p.CategoryID,
c.CategoryName,
p.UnitPrice,
cap.AvgCategoryPrice,
SQUARE(p.UnitPrice - cap.AvgCategoryPrice) AS SquaredPriceDifference
FROM Products AS p
INNER JOIN
CategoryAvgPrices AS cap
ON p.CategoryID = cap.CategoryID
INNER JOIN
Categories AS c
ON p.CategoryID = c.CategoryID
ORDER BY p.CategoryID ASC, p.ProductID ASC;
Parameters (If used)
Query Results 1:
|
ProductID |
ProductName |
CategoryID |
CategoryName |
UnitPrice |
AvgCategoryPrice |
SquaredPriceDifference |
1 |
1
|
Chai
|
1
|
Beverages
|
18.0000
|
37.9791
|
399.16443681
|
2 |
2
|
Chang
|
1
|
Beverages
|
19.0000
|
37.9791
|
360.20623680999995
|
3 |
24
|
Guaraná Fantástica
|
1
|
Beverages
|
4.5000
|
37.9791
|
1120.85013681
|
4 |
34
|
Sasquatch Ale
|
1
|
Beverages
|
14.0000
|
37.9791
|
574.99723681
|
5 |
35
|
Steeleye Stout
|
1
|
Beverages
|
18.0000
|
37.9791
|
399.16443681
|
6 |
38
|
Côte de Blaye
|
1
|
Beverages
|
263.5000
|
37.9791
|
50859.67633681001
|
7 |
39
|
Chartreuse verte
|
1
|
Beverages
|
18.0000
|
37.9791
|
399.16443681
|
8 |
43
|
Ipoh Coffee
|
1
|
Beverages
|
46.0000
|
37.9791
|
64.33483680999998
|
9 |
67
|
Laughing Lumberjack Lager
|
1
|
Beverages
|
14.0000
|
37.9791
|
574.99723681
|
10 |
70
|
Outback Lager
|
1
|
Beverages
|
15.0000
|
37.9791
|
528.03903681
|
11 |
75
|
Rhönbräu Klosterbier
|
1
|
Beverages
|
7.7500
|
37.9791
|
913.79848681
|
12 |
76
|
Lakkalikööri
|
1
|
Beverages
|
18.0000
|
37.9791
|
399.16443681
|
13 |
3
|
Aniseed Syrup
|
2
|
Condiments
|
10.0000
|
23.0625
|
170.62890625
|
14 |
4
|
Chef Anton's Cajun Seasoning
|
2
|
Condiments
|
22.0000
|
23.0625
|
1.12890625
|
15 |
5
|
Chef Anton's Gumbo Mix
|
2
|
Condiments
|
21.3500
|
23.0625
|
2.9326562499999995
|
16 |
6
|
Grandma's Boysenberry Spread
|
2
|
Condiments
|
25.0000
|
23.0625
|
3.75390625
|
17 |
8
|
Northwoods Cranberry Sauce
|
2
|
Condiments
|
40.0000
|
23.0625
|
286.87890625
|
18 |
15
|
Genen Shouyu
|
2
|
Condiments
|
15.5000
|
23.0625
|
57.19140625
|
19 |
44
|
Gula Malacca
|
2
|
Condiments
|
19.4500
|
23.0625
|
13.050156249999999
|
20 |
61
|
Sirop d'érable
|
2
|
Condiments
|
28.5000
|
23.0625
|
29.56640625
|
21 |
63
|
Vegie-spread
|
2
|
Condiments
|
43.9000
|
23.0625
|
434.20140624999993
|
22 |
65
|
Louisiana Fiery Hot Pepper Sauce
|
2
|
Condiments
|
21.0500
|
23.0625
|
4.050156250000001
|
23 |
66
|
Louisiana Hot Spiced Okra
|
2
|
Condiments
|
17.0000
|
23.0625
|
36.75390625
|
24 |
77
|
Original Frankfurter grüne Soße
|
2
|
Condiments
|
13.0000
|
23.0625
|
101.25390625
|
25 |
16
|
Pavlova
|
3
|
Confections
|
17.4500
|
25.1600
|
59.4441
|
26 |
19
|
Teatime Chocolate Biscuits
|
3
|
Confections
|
9.2000
|
25.1600
|
254.72160000000002
|
27 |
20
|
Sir Rodney's Marmalade
|
3
|
Confections
|
81.0000
|
25.1600
|
3118.1056000000003
|
28 |
21
|
Sir Rodney's Scones
|
3
|
Confections
|
10.0000
|
25.1600
|
229.8256
|
29 |
25
|
NuNuCa Nuß-Nougat-Creme
|
3
|
Confections
|
14.0000
|
25.1600
|
124.54560000000001
|
30 |
26
|
Gumbär Gummibärchen
|
3
|
Confections
|
31.2300
|
25.1600
|
36.8449
|
31 |
27
|
Schoggi Schokolade
|
3
|
Confections
|
43.9000
|
25.1600
|
351.1875999999999
|
32 |
47
|
Zaanse koeken
|
3
|
Confections
|
9.5000
|
25.1600
|
245.2356
|
33 |
48
|
Chocolade
|
3
|
Confections
|
12.7500
|
25.1600
|
154.0081
|
34 |
49
|
Maxilaku
|
3
|
Confections
|
20.0000
|
25.1600
|
26.625600000000002
|
35 |
50
|
Valkoinen suklaa
|
3
|
Confections
|
16.2500
|
25.1600
|
79.38810000000001
|
36 |
62
|
Tarte au sucre
|
3
|
Confections
|
49.3000
|
25.1600
|
582.7396
|
37 |
68
|
Scottish Longbreads
|
3
|
Confections
|
12.5000
|
25.1600
|
160.2756
|
38 |
11
|
Queso Cabrales
|
4
|
Dairy Products
|
21.0000
|
28.7300
|
59.752900000000004
|
39 |
12
|
Queso Manchego La Pastora
|
4
|
Dairy Products
|
38.0000
|
28.7300
|
85.93289999999999
|
40 |
31
|
Gorgonzola Telino
|
4
|
Dairy Products
|
12.5000
|
28.7300
|
263.41290000000004
|
41 |
32
|
Mascarpone Fabioli
|
4
|
Dairy Products
|
32.0000
|
28.7300
|
10.6929
|
42 |
33
|
Geitost
|
4
|
Dairy Products
|
2.5000
|
28.7300
|
688.0129000000001
|
43 |
59
|
Raclette Courdavault
|
4
|
Dairy Products
|
55.0000
|
28.7300
|
690.1129
|
44 |
60
|
Camembert Pierrot
|
4
|
Dairy Products
|
34.0000
|
28.7300
|
27.772899999999996
|
45 |
69
|
Gudbrandsdalsost
|
4
|
Dairy Products
|
36.0000
|
28.7300
|
52.85289999999999
|
46 |
71
|
Flotemysost
|
4
|
Dairy Products
|
21.5000
|
28.7300
|
52.27290000000001
|
47 |
72
|
Mozzarella di Giovanni
|
4
|
Dairy Products
|
34.8000
|
28.7300
|
36.8449
|
48 |
22
|
Gustaf's Knäckebröd
|
5
|
Grains/Cereals
|
21.0000
|
20.2500
|
0.5625
|
49 |
23
|
Tunnbröd
|
5
|
Grains/Cereals
|
9.0000
|
20.2500
|
126.5625
|
50 |
42
|
Singaporean Hokkien Fried Mee
|
5
|
Grains/Cereals
|
14.0000
|
20.2500
|
39.0625
|
51 |
52
|
Filo Mix
|
5
|
Grains/Cereals
|
7.0000
|
20.2500
|
175.5625
|
52 |
56
|
Gnocchi di nonna Alice
|
5
|
Grains/Cereals
|
38.0000
|
20.2500
|
315.0625
|
53 |
57
|
Ravioli Angelo
|
5
|
Grains/Cereals
|
19.5000
|
20.2500
|
0.5625
|
54 |
64
|
Wimmers gute Semmelknödel
|
5
|
Grains/Cereals
|
33.2500
|
20.2500
|
169
|
55 |
9
|
Mishi Kobe Niku
|
6
|
Meat/Poultry
|
97.0000
|
54.0066
|
1848.4324435600001
|
56 |
17
|
Alice Mutton
|
6
|
Meat/Poultry
|
39.0000
|
54.0066
|
225.19804356000003
|
57 |
29
|
Thüringer Rostbratwurst
|
6
|
Meat/Poultry
|
123.7900
|
54.0066
|
4869.72291556
|
58 |
53
|
Perth Pasties
|
6
|
Meat/Poultry
|
32.8000
|
54.0066
|
449.7198835600001
|
59 |
54
|
Tourtière
|
6
|
Meat/Poultry
|
7.4500
|
54.0066
|
2167.5170035600004
|
60 |
55
|
Pâté chinois
|
6
|
Meat/Poultry
|
24.0000
|
54.0066
|
900.39604356
|
61 |
7
|
Uncle Bob's Organic Dried Pears
|
7
|
Produce
|
30.0000
|
32.3700
|
5.6169
|
62 |
14
|
Tofu
|
7
|
Produce
|
23.2500
|
32.3700
|
83.17439999999999
|
63 |
28
|
Rössle Sauerkraut
|
7
|
Produce
|
45.6000
|
32.3700
|
175.0329
|
64 |
51
|
Manjimup Dried Apples
|
7
|
Produce
|
53.0000
|
32.3700
|
425.59689999999995
|
65 |
74
|
Longlife Tofu
|
7
|
Produce
|
10.0000
|
32.3700
|
500.41690000000006
|
66 |
10
|
Ikura
|
8
|
Seafood
|
31.0000
|
20.6825
|
106.45080625000001
|
67 |
13
|
Konbu
|
8
|
Seafood
|
6.0000
|
20.6825
|
215.57580624999997
|
68 |
18
|
Carnarvon Tigers
|
8
|
Seafood
|
62.5000
|
20.6825
|
1748.7033062500002
|
69 |
30
|
Nord-Ost Matjeshering
|
8
|
Seafood
|
25.8900
|
20.6825
|
27.118056249999995
|
70 |
36
|
Inlagd Sill
|
8
|
Seafood
|
19.0000
|
20.6825
|
2.83080625
|
71 |
37
|
Gravad lax
|
8
|
Seafood
|
26.0000
|
20.6825
|
28.27580625
|
72 |
40
|
Boston Crab Meat
|
8
|
Seafood
|
18.4000
|
20.6825
|
5.209806250000001
|
73 |
41
|
Jack's New England Clam Chowder
|
8
|
Seafood
|
9.6500
|
20.6825
|
121.71605625000001
|
74 |
45
|
Rogede sild
|
8
|
Seafood
|
9.5000
|
20.6825
|
125.04830624999998
|
75 |
46
|
Spegesild
|
8
|
Seafood
|
12.0000
|
20.6825
|
75.38580624999999
|
76 |
58
|
Escargots de Bourgogne
|
8
|
Seafood
|
13.2500
|
20.6825
|
55.242056250000005
|
77 |
73
|
Röd Kaviar
|
8
|
Seafood
|
15.0000
|
20.6825
|
32.29080625
|