VARP SQL function


1. Usage of VARP to show standard deviation of population of orders

SQL Server Query 1

            
SELECT  
c.Country,
AVG(od.UnitPrice * od.Quantity) AS AverageOrderAmount,
VARP(od.UnitPrice * od.Quantity) AS OrderAmountPopulationVariance
FROM Orders AS o
JOIN Customers AS c ON o.CustomerID = c.CustomerID
JOIN [Order Details] AS od ON o.OrderID = od.OrderID
GROUP BY c.Country
ORDER BY OrderAmountPopulationVariance DESC;

Create SQL query with SqlQueryBuilder 1

            
var (sql1, parameters1) = new SqlQueryBuilder()  
.Select()
.Column("c.Country", "Country")
.Column(new AVG(new ColumnArithmatic("od.UnitPrice").MULTIPLY(new Column("od.Quantity"))), "AverageOrderAmount")
.Column(new VARP(new ColumnArithmatic("od.UnitPrice").MULTIPLY(new Column("od.Quantity"))), "OrderAmountPopulationVariance")
.From("Orders", "o")
.Join(new List<IJoin>()
{
new INNERJOIN().TableName(new Table("Customers","c"))
.On(new Column("o.CustomerID").Equale(new Column("c.CustomerID"))),
new INNERJOIN().TableName(new Table("[Order Details]", "od"))
.On(new Column("o.OrderID").Equale(new Column("od.OrderID")))
})
.GroupBy(new GroupBy("c.Country"))
.OrderBy(new OrderBy().SetColumnDescending("OrderAmountPopulationVariance"))
.Build();

Query build by SqlQueryBuilder 1

            
SELECT c.Country AS Country,
       AVG(od.UnitPrice * od.Quantity) AS AverageOrderAmount,
       VARP(od.UnitPrice * od.Quantity) AS OrderAmountPopulationVariance
FROM Orders AS o
     INNER JOIN
     Customers AS c
     ON o.CustomerID = c.CustomerID
     INNER JOIN
     [Order Details] AS od
     ON o.OrderID = od.OrderID
GROUP BY c.Country
ORDER BY OrderAmountPopulationVariance DESC;


            
        

Parameters (If used)

Name Value

Query Results 1:

  Country AverageOrderAmount OrderAmountPopulationVariance
1 Ireland 1042.1343 2827618.74293005
2 Denmark 756.1358 2418304.978550331
3 Austria 1115.9730 1995171.0147363583
4 Brazil 566.3471 1662634.0232763519
5 Canada 737.7880 1626846.9151893333
6 Germany 745.8555 1466523.7582612492
7 USA 748.7698 1354375.723638039
8 Sweden 613.6463 738099.8739302794
9 Switzerland 633.0673 516320.06010813586
10 Belgium 627.4103 350863.3559713011
11 Spain 359.8498 300449.0276684843
12 France 464.6671 288206.8717463611
13 UK 449.0111 267735.9600608175
14 Mexico 334.3534 247096.4487032214
15 Venezuela 515.3804 240221.4344091426
16 Norway 358.4468 228784.71077148436
17 Portugal 415.6216 114615.01194722224
18 Finland 366.2675 106813.82538494523
19 Italy 315.1915 84970.18167319329
20 Argentina 238.7970 51623.3244031142
21 Poland 220.7468 24931.572333984383


2. Usage of VARP to see price variance population across product categories

SQL Server Query 2

            
SELECT  
p.SupplierID,
s.CompanyName AS SupplierName,
AVG(p.UnitPrice) AS AveragePrice,
VARP(p.UnitPrice) AS PricePopulationVariance
FROM Products AS p
JOIN Suppliers AS s ON p.SupplierID = s.SupplierID
GROUP BY p.SupplierID, s.CompanyName
ORDER BY PricePopulationVariance DESC;

Create SQL query with SqlQueryBuilder 2

            
var (sql2, parameters2) = new SqlQueryBuilder()  
.Select()
.Column("p.SupplierID", "SupplierID")
.Column("s.CompanyName", "SupplierName")
.Column(new AVG(new Column("p.UnitPrice")), "AveragePrice")
.Column(new VARP(new Column("p.UnitPrice")), "PricePopulationVariance")
.From("Products", "p")
.Join(new List<IJoin>()
{
new INNERJOIN().TableName(new Table("Suppliers","s"))
.On(new Column("p.SupplierID").Equale(new Column("s.SupplierID")))
})
.GroupBy(new GroupBy("p.SupplierID", "s.CompanyName"))
.OrderBy(new OrderBy()
.SetColumnDescending("PricePopulationVariance"))
.Build();

Query build by SqlQueryBuilder 2

            
SELECT p.SupplierID AS SupplierID,
       s.CompanyName AS SupplierName,
       AVG(p.UnitPrice) AS AveragePrice,
       VARP(p.UnitPrice) AS PricePopulationVariance
FROM Products AS p
     INNER JOIN
     Suppliers AS s
     ON p.SupplierID = s.SupplierID
GROUP BY p.SupplierID, s.CompanyName
ORDER BY PricePopulationVariance DESC;


            
        

Parameters (If used)

Name Value

Query Results 2:

  SupplierID SupplierName AveragePrice PricePopulationVariance
1 18 Aux joyeux ecclésiastiques 140.7500 15067.5625
2 12 Plutzer Lebensmittelgroßmärkte AG 44.6780 1751.4661359999998
3 4 Tokyo Traders 46.0000 1374
4 8 Specialty Biscuits, Ltd. 28.1750 931.641875
5 24 G'day, Mate 30.9333 354.40888888888895
6 7 Pavlova, Ltd. 35.5700 311.56760000000014
7 20 Leka Trading 26.4833 195.40055555555546
8 15 Norske Meierier 20.0000 188.16666666666666
9 11 Heli Süßwaren GmbH & Co. KG 29.7100 150.1568666666667
10 28 Gai pâturage 44.5000 110.25
11 29 Forêts d'érables 38.9000 108.16000000000008
12 14 Formaggi Fortini s.r.l. 26.4333 98.37555555555552
13 26 Pasta Buttini s.r.l. 28.7500 85.5625
14 5 Cooperativa de Quesos 'Las Cabras' 29.5000 72.25
15 25 Ma Maison 15.7250 68.47562500000004
16 6 Mayumi's 14.9166 49.76388888888888
17 3 Grandma Kelly's Homestead 31.6666 38.888888888888836
18 9 PB Knäckebröd AB 15.0000 36
19 17 Svensk Sjöföda AB 20.0000 20.666666666666668
20 19 New England Seafood Cannery 14.0250 19.140625
21 1 Exotic Liquids 15.6666 16.22222222222221
22 2 New Orleans Cajun Delights 20.3500 3.8587499999999295
23 16 Bigfoot Breweries 15.3333 3.555555555555543
24 22 Zaanse Snoepfabriek 11.1250 2.640625
25 23 Karkki Oy 18.0833 2.3472222222222094
26 21 Lyngbysild 10.7500 1.5625
27 27 Escargots Nouveaux 13.2500 0
28 13 Nord-Ost-Fisch Handelsgesellschaft mbH 25.8900 0
29 10 Refrescos Americanas LTDA 4.5000 0


3. Usage of VAR for variance of price within categories population

SQL Server Query 3

            
SELECT  
p.ProductID,
p.ProductName,
p.CategoryID,
c.CategoryName,
p.UnitPrice,
VARP(p.UnitPrice) OVER (PARTITION BY p.CategoryID) AS CategoryPriceVariance,
(p.UnitPrice - AVG(p.UnitPrice) OVER (PARTITION BY p.CategoryID)) / SQRT(VARP(p.UnitPrice) OVER (PARTITION BY p.CategoryID)) AS ZScore --Approximation
FROM Products AS p
JOIN Categories AS c ON p.CategoryID = c.CategoryID
ORDER BY p.CategoryID, p.UnitPrice;

Create SQL query with SqlQueryBuilder 3

            
var (sql3, parameters3) = new SqlQueryBuilder()  
.Select()
.Columns("p.ProductID", "p.ProductName", "p.CategoryID", "c.CategoryName", "p.UnitPrice")
.Column(new VARP(new Column("p.UnitPrice")).OVER(new OVER().PARTITION_BY(new Column("p.CategoryID"))), "CategoryPricePopulationVariance")
.Column(new ColumnArithmatic().StartBracket("p.UnitPrice").SUBTRACT(
new AVG(new Column("p.UnitPrice")).OVER(new OVER().PARTITION_BY(new Column("p.CategoryID")))).EndBracket()
.DIVIDE(new SQRT(new VARP(new Column("p.UnitPrice")).OVER(new OVER().PARTITION_BY(new Column("p.CategoryID")))))
, "ZScore")
.From("Products", "p")
.Join(new List<IJoin>()
{
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.UnitPrice"))
.Build();

Query build by SqlQueryBuilder 3

            
SELECT p.ProductID,
       p.ProductName,
       p.CategoryID,
       c.CategoryName,
       p.UnitPrice,
       VARP(p.UnitPrice) OVER (PARTITION BY p.CategoryID) AS CategoryPricePopulationVariance,
       (p.UnitPrice - AVG(p.UnitPrice) OVER (PARTITION BY p.CategoryID)) / SQRT(VARP(p.UnitPrice) OVER (PARTITION BY p.CategoryID)) AS ZScore
FROM Products AS p
     INNER JOIN
     Categories AS c
     ON p.CategoryID = c.CategoryID
ORDER BY p.CategoryID ASC, p.UnitPrice ASC;


            
        

Parameters (If used)

Name Value

Query Results 3:

  ProductID ProductName CategoryID CategoryName UnitPrice CategoryPricePopulationVariance ZScore
1 24 Guaraná Fantástica 1 Beverages 4.5000 4716.129774305556 -0.4875070505227496
2 75 Rhönbräu Klosterbier 1 Beverages 7.7500 4716.129774305556 -0.4401820652573471
3 67 Laughing Lumberjack Lager 1 Beverages 14.0000 4716.129774305556 -0.3491724782084962
4 34 Sasquatch Ale 1 Beverages 14.0000 4716.129774305556 -0.3491724782084962
5 70 Outback Lager 1 Beverages 15.0000 4716.129774305556 -0.33461094428068
6 39 Chartreuse verte 1 Beverages 18.0000 4716.129774305556 -0.2909263424972316
7 35 Steeleye Stout 1 Beverages 18.0000 4716.129774305556 -0.2909263424972316
8 1 Chai 1 Beverages 18.0000 4716.129774305556 -0.2909263424972316
9 76 Lakkalikööri 1 Beverages 18.0000 4716.129774305556 -0.2909263424972316
10 2 Chang 1 Beverages 19.0000 4716.129774305556 -0.2763648085694154
11 43 Ipoh Coffee 1 Beverages 46.0000 4716.129774305556 0.11679660748162053
12 38 Côte de Blaye 1 Beverages 263.5000 4716.129774305556 3.2839302367816328
13 3 Aniseed Syrup 2 Condiments 10.0000 95.11588541666667 -1.339367559976778
14 77 Original Frankfurter grüne Soße 2 Condiments 13.0000 95.11588541666667 -1.031761613187853
15 15 Genen Shouyu 2 Condiments 15.5000 95.11588541666667 -0.775423324197082
16 66 Louisiana Hot Spiced Okra 2 Condiments 17.0000 95.11588541666667 -0.6216203508026195
17 44 Gula Malacca 2 Condiments 19.4500 95.11588541666667 -0.370408827591664
18 65 Louisiana Fiery Hot Pepper Sauce 2 Condiments 21.0500 95.11588541666667 -0.2063523226375706
19 5 Chef Anton's Gumbo Mix 2 Condiments 21.3500 95.11588541666667 -0.17559172795867808
20 4 Chef Anton's Cajun Seasoning 2 Condiments 22.0000 95.11588541666667 -0.10894377282107764
21 6 Grandma's Boysenberry Spread 2 Condiments 25.0000 95.11588541666667 0.19866217396784747
22 61 Sirop d'érable 2 Condiments 28.5000 95.11588541666667 0.5575357785549268
23 8 Northwoods Cranberry Sauce 2 Condiments 40.0000 95.11588541666667 1.736691907912473
24 63 Vegie-spread 2 Condiments 43.9000 95.11588541666667 2.1365796387380755
25 19 Teatime Chocolate Biscuits 3 Confections 9.2000 417.14981538461535 -0.7814242042325249
26 47 Zaanse koeken 3 Confections 9.5000 417.14981538461535 -0.7667357793409361
27 21 Sir Rodney's Scones 3 Confections 10.0000 417.14981538461535 -0.742255071188288
28 68 Scottish Longbreads 3 Confections 12.5000 417.14981538461535 -0.6198515304250479
29 48 Chocolade 3 Confections 12.7500 417.14981538461535 -0.6076111763487239
30 25 NuNuCa Nuß-Nougat-Creme 3 Confections 14.0000 417.14981538461535 -0.5464094059671039
31 50 Valkoinen suklaa 3 Confections 16.2500 417.14981538461535 -0.43624621928018775
32 16 Pavlova 3 Confections 17.4500 417.14981538461535 -0.3774925197138325
33 49 Maxilaku 3 Confections 20.0000 417.14981538461535 -0.2526409081353276
34 26 Gumbär Gummibärchen 3 Confections 31.2300 417.14981538461535 0.297195796973147
35 27 Schoggi Schokolade 3 Confections 43.9000 417.14981538461535 0.9175369415612478
36 62 Tarte au sucre 3 Confections 49.3000 417.14981538461535 1.1819285896098466
37 20 Sir Rodney's Marmalade 3 Confections 81.0000 417.14981538461535 2.7340054864877312
38 33 Geitost 4 Dairy Products 2.5000 196.7661 -1.869920538227882
39 31 Gorgonzola Telino 4 Dairy Products 12.5000 196.7661 -1.1570266997879728
40 11 Queso Cabrales 4 Dairy Products 21.0000 196.7661 -0.55106693711405
41 71 Flotemysost 4 Dairy Products 21.5000 196.7661 -0.5154222451920545
42 32 Mascarpone Fabioli 4 Dairy Products 32.0000 196.7661 0.23311628516985033
43 60 Camembert Pierrot 4 Dairy Products 34.0000 196.7661 0.37569505285783217
44 72 Mozzarella di Giovanni 4 Dairy Products 34.8000 196.7661 0.43272655993302495
45 69 Gudbrandsdalsost 4 Dairy Products 36.0000 196.7661 0.5182738205458141
46 12 Queso Manchego La Pastora 4 Dairy Products 38.0000 196.7661 0.6608525882337959
47 59 Raclette Courdavault 4 Dairy Products 55.0000 196.7661 1.8727721135816418
48 52 Filo Mix 5 Grains/Cereals 7.0000 118.05357142857143 -1.2194845800799579
49 23 Tunnbröd 5 Grains/Cereals 9.0000 118.05357142857143 -1.0354114359169453
50 42 Singaporean Hokkien Fried Mee 5 Grains/Cereals 14.0000 118.05357142857143 -0.5752285755094141
51 57 Ravioli Angelo 5 Grains/Cereals 19.5000 118.05357142857143 -0.06902742906112969
52 22 Gustaf's Knäckebröd 5 Grains/Cereals 21.0000 118.05357142857143 0.06902742906112969
53 64 Wimmers gute Semmelknödel 5 Grains/Cereals 33.2500 118.05357142857143 1.1964754370595811
54 56 Gnocchi di nonna Alice 5 Grains/Cereals 38.0000 118.05357142857143 1.6336491544467358
55 54 Tourtière 6 Meat/Poultry 7.4500 1743.4977222222217 -1.1149889525814338
56 55 Pâté chinois 6 Meat/Poultry 24.0000 1743.4977222222217 -0.7186312467948701
57 53 Perth Pasties 6 Meat/Poultry 32.8000 1743.4977222222217 -0.5078791132044315
58 17 Alice Mutton 6 Meat/Poultry 39.0000 1743.4977222222217 -0.3593946554475315
59 9 Mishi Kobe Niku 6 Meat/Poultry 97.0000 1743.4977222222217 1.029653497762178
60 29 Thüringer Rostbratwurst 6 Meat/Poultry 123.7900 1743.4977222222217 1.6712500499085248
61 74 Longlife Tofu 7 Produce 10.0000 237.96760000000032 -1.4501304393033867
62 14 Tofu 7 Produce 23.2500 237.96760000000032 -0.5912020387325385
63 7 Uncle Bob's Organic Dried Pears 7 Produce 30.0000 237.96760000000032 -0.15363474032852154
64 28 Rössle Sauerkraut 7 Produce 45.6000 237.96760000000032 0.8576319048718734
65 51 Manjimup Dried Apples 7 Produce 53.0000 237.96760000000032 1.3373353134925734
66 13 Konbu 8 Seafood 6.0000 211.98728541666665 -1.0084284805133161
67 45 Rogede sild 8 Seafood 9.5000 211.98728541666665 -0.7680402849201539
68 41 Jack's New England Clam Chowder 8 Seafood 9.6500 211.98728541666665 -0.757737933680447
69 46 Spegesild 8 Seafood 12.0000 211.98728541666665 -0.5963344309250378
70 58 Escargots de Bourgogne 8 Seafood 13.2500 211.98728541666665 -0.5104815039274799
71 73 Röd Kaviar 8 Seafood 15.0000 211.98728541666665 -0.3902874061308987
72 40 Boston Crab Meat 8 Seafood 18.4000 211.98728541666665 -0.15676744469754092
73 36 Inlagd Sill 8 Seafood 19.0000 211.98728541666665 -0.11555803973871308
74 30 Nord-Ost Matjeshering 8 Seafood 25.8900 211.98728541666665 0.3576632938718266
75 37 Gravad lax 8 Seafood 26.0000 211.98728541666665 0.3652183514476117
76 10 Ikura 8 Seafood 31.0000 211.98728541666665 0.7086300594378439
77 18 Carnarvon Tigers 8 Seafood 62.5000 211.98728541666665 2.8721238197763057