AVG SQL function


1. Usage of AVG along to find product price difference

SQL Server Query 1

            
SELECT  
ProductID,
ProductName,
UnitPrice,
(SELECT AVG(UnitPrice) FROM Products) AS AveragePrice,
ABS(UnitPrice - (SELECT AVG(UnitPrice) FROM Products)) AS PriceDifference
FROM Products;

Create SQL query with SqlQueryBuilder 1

            
var (sql2, parameters2) = new SqlQueryBuilder()  
.Select()
.Columns("ProductID", "ProductName", "UnitPrice")
.Column(new SqlQueryBuilder().Select().Column(new AVG(new Column("UnitPrice")), "AveragePrice").From("Products"), "AveragePrice")
.Column(new ABS(
new ColumnArithmatic().Column("UnitPrice").SUBTRACT().Column(
new SqlQueryBuilder().Select().Column(new AVG(new Column("UnitPrice")), "PriceDifference").From("Products")
)), "PriceDifference")
.From("Products")
.Build();

Query build by SqlQueryBuilder 1

            
SELECT ProductID,
       ProductName,
       UnitPrice,
       (SELECT AVG(UnitPrice) AS AveragePrice
        FROM Products) AS AveragePrice,
       ABS(UnitPrice - (SELECT AVG(UnitPrice) AS PriceDifference
                        FROM Products)) AS PriceDifference
FROM Products;


            
        

Parameters (If used)

Name Value

Query Results 1:

  ProductID ProductName UnitPrice AveragePrice PriceDifference
1 1 Chai 18.0000 28.8663 10.8663
2 2 Chang 19.0000 28.8663 9.8663
3 3 Aniseed Syrup 10.0000 28.8663 18.8663
4 4 Chef Anton's Cajun Seasoning 22.0000 28.8663 6.8663
5 5 Chef Anton's Gumbo Mix 21.3500 28.8663 7.5163
6 6 Grandma's Boysenberry Spread 25.0000 28.8663 3.8663
7 7 Uncle Bob's Organic Dried Pears 30.0000 28.8663 1.1337
8 8 Northwoods Cranberry Sauce 40.0000 28.8663 11.1337
9 9 Mishi Kobe Niku 97.0000 28.8663 68.1337
10 10 Ikura 31.0000 28.8663 2.1337
11 11 Queso Cabrales 21.0000 28.8663 7.8663
12 12 Queso Manchego La Pastora 38.0000 28.8663 9.1337
13 13 Konbu 6.0000 28.8663 22.8663
14 14 Tofu 23.2500 28.8663 5.6163
15 15 Genen Shouyu 15.5000 28.8663 13.3663
16 16 Pavlova 17.4500 28.8663 11.4163
17 17 Alice Mutton 39.0000 28.8663 10.1337
18 18 Carnarvon Tigers 62.5000 28.8663 33.6337
19 19 Teatime Chocolate Biscuits 9.2000 28.8663 19.6663
20 20 Sir Rodney's Marmalade 81.0000 28.8663 52.1337
21 21 Sir Rodney's Scones 10.0000 28.8663 18.8663
22 22 Gustaf's Knäckebröd 21.0000 28.8663 7.8663
23 23 Tunnbröd 9.0000 28.8663 19.8663
24 24 Guaraná Fantástica 4.5000 28.8663 24.3663
25 25 NuNuCa Nuß-Nougat-Creme 14.0000 28.8663 14.8663
26 26 Gumbär Gummibärchen 31.2300 28.8663 2.3637
27 27 Schoggi Schokolade 43.9000 28.8663 15.0337
28 28 Rössle Sauerkraut 45.6000 28.8663 16.7337
29 29 Thüringer Rostbratwurst 123.7900 28.8663 94.9237
30 30 Nord-Ost Matjeshering 25.8900 28.8663 2.9763
31 31 Gorgonzola Telino 12.5000 28.8663 16.3663
32 32 Mascarpone Fabioli 32.0000 28.8663 3.1337
33 33 Geitost 2.5000 28.8663 26.3663
34 34 Sasquatch Ale 14.0000 28.8663 14.8663
35 35 Steeleye Stout 18.0000 28.8663 10.8663
36 36 Inlagd Sill 19.0000 28.8663 9.8663
37 37 Gravad lax 26.0000 28.8663 2.8663
38 38 Côte de Blaye 263.5000 28.8663 234.6337
39 39 Chartreuse verte 18.0000 28.8663 10.8663
40 40 Boston Crab Meat 18.4000 28.8663 10.4663
41 41 Jack's New England Clam Chowder 9.6500 28.8663 19.2163
42 42 Singaporean Hokkien Fried Mee 14.0000 28.8663 14.8663
43 43 Ipoh Coffee 46.0000 28.8663 17.1337
44 44 Gula Malacca 19.4500 28.8663 9.4163
45 45 Rogede sild 9.5000 28.8663 19.3663
46 46 Spegesild 12.0000 28.8663 16.8663
47 47 Zaanse koeken 9.5000 28.8663 19.3663
48 48 Chocolade 12.7500 28.8663 16.1163
49 49 Maxilaku 20.0000 28.8663 8.8663
50 50 Valkoinen suklaa 16.2500 28.8663 12.6163
51 51 Manjimup Dried Apples 53.0000 28.8663 24.1337
52 52 Filo Mix 7.0000 28.8663 21.8663
53 53 Perth Pasties 32.8000 28.8663 3.9337
54 54 Tourtière 7.4500 28.8663 21.4163
55 55 Pâté chinois 24.0000 28.8663 4.8663
56 56 Gnocchi di nonna Alice 38.0000 28.8663 9.1337
57 57 Ravioli Angelo 19.5000 28.8663 9.3663
58 58 Escargots de Bourgogne 13.2500 28.8663 15.6163
59 59 Raclette Courdavault 55.0000 28.8663 26.1337
60 60 Camembert Pierrot 34.0000 28.8663 5.1337
61 61 Sirop d'érable 28.5000 28.8663 0.3663
62 62 Tarte au sucre 49.3000 28.8663 20.4337
63 63 Vegie-spread 43.9000 28.8663 15.0337
64 64 Wimmers gute Semmelknödel 33.2500 28.8663 4.3837
65 65 Louisiana Fiery Hot Pepper Sauce 21.0500 28.8663 7.8163
66 66 Louisiana Hot Spiced Okra 17.0000 28.8663 11.8663
67 67 Laughing Lumberjack Lager 14.0000 28.8663 14.8663
68 68 Scottish Longbreads 12.5000 28.8663 16.3663
69 69 Gudbrandsdalsost 36.0000 28.8663 7.1337
70 70 Outback Lager 15.0000 28.8663 13.8663
71 71 Flotemysost 21.5000 28.8663 7.3663
72 72 Mozzarella di Giovanni 34.8000 28.8663 5.9337
73 73 Röd Kaviar 15.0000 28.8663 13.8663
74 74 Longlife Tofu 10.0000 28.8663 18.8663
75 75 Rhönbräu Klosterbier 7.7500 28.8663 21.1163
76 76 Lakkalikööri 18.0000 28.8663 10.8663
77 77 Original Frankfurter grüne Soße 13.0000 28.8663 15.8663