VAR SQL function


1. Usage of VAR to show order amount variance by customers

SQL Server Query 1

            
SELECT  
o.CustomerID,
c.CompanyName,
AVG(od.UnitPrice * od.Quantity) AS AverageOrderAmount,
VAR(od.UnitPrice * od.Quantity) AS OrderAmountVariance
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 o.CustomerID, c.CompanyName
ORDER BY OrderAmountVariance DESC;

Create SQL query with SqlQueryBuilder 1

            
var (sql1, parameters1) = new SqlQueryBuilder()  
.Select()
.Column("o.CustomerID", "CustomerID")
.Column("c.CompanyName", "CompanyName")
.Column(new AVG(new ColumnArithmatic("od.UnitPrice").MULTIPLY("od.Quantity")), "AverageOrderAmount")
.Column(new VAR(new ColumnArithmatic("od.UnitPrice").MULTIPLY("od.Quantity")), "OrderAmountVariance")
.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("o.CustomerID", "c.CompanyName"))
.OrderBy(new OrderBy().SetColumnDescending("OrderAmountVariance"))
.Build();

Query build by SqlQueryBuilder 1

            
SELECT o.CustomerID AS CustomerID,
       c.CompanyName AS CompanyName,
       AVG(od.UnitPrice * od.Quantity) AS AverageOrderAmount,
       VAR(od.UnitPrice * od.Quantity) AS OrderAmountVariance
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 o.CustomerID, c.CompanyName
ORDER BY OrderAmountVariance DESC;


            
        

Parameters (If used)

Name Value

Query Results 1:

  CustomerID CompanyName AverageOrderAmount OrderAmountVariance
1 HANAR Hanari Carnes 1065.6609 7484388.737215222
2 SIMOB Simons bistro 1209.23 6868286.792071429
3 PICCO Piccolo und mehr 1141.7369 4471199.432094863
4 QUICK QUICK-Stop 1366.0859 3882522.7001773594
5 MEREP Mère Paillarde 1006.3718 3360846.6788608874
6 GREAL Great Lakes Food Market 895.9604 3174283.7535474026
7 HUNGO Hungry Owl All-Night Grocers 1042.1343 2879982.05298431
8 RATTC Rattlesnake Canyon Grocery 735.8577 2318099.5891177063
9 QUEEN Queen Cozinha 755.6525 1834720.4830705135
10 KOENE Königlich Essen 813.9935 1729187.356107828
11 ERNSH Ernst Handel 1110.1635 1495161.1868092022
12 SAVEA Save-a-lot Markets 997.1843 1282107.1338422415
13 WHITC White Clover Markets 726.8362 1159985.7421778846
14 FOLKO Folk och fä HB 723.4566 1048765.028590909
15 SPLIR Split Rail Beer & Ale 624.485 930273.6181842104
16 THEBI The Big Cheese 480.1428 904726.1961904763
17 BOLID Bólido Comidas preparadas 882.9666 860125.3506666666
18 RICSU Richter Supermarkt 667.7733 762319.0306850574
19 FOLIG Folies gourmandes 729.1812 721843.1122916668
20 BLONP Blondesddsl père et fils 734.1538 539106.1137846154
21 AROUT Around the Horn 460.2166 517654.7331609196
22 TORTU Tortuga Restaurante 372.8327 502382.26701354684
23 BERGS Berglunds snabbköp 518.6182 479173.0447087104
24 MAISD Maison Dewey 613.5635 384361.30786176474
25 FRANR France restauration 528.6933 363716.82586666674
26 SUPRD Suprêmes délices 633.4461 355102.28202429164
27 VAFFE Vaffeljernet 536.8967 350552.01165591396
28 BOTTM Bottom-Dollar Markets 645.9342 341008.662907563
29 GODOS Godos Cocina Típica 455.0038 338080.5611846154
30 EASTC Eastern Connection 715.8885 318748.1925928573
31 OLDWO Old World Delicatessen 680.2145 310000.1094519928
32 HILAA HILARION-Abastos 524.7017 309195.5933922222
33 HUNGC Hungry Coyote Import Store 340.3555 287320.9577777778
34 RICAR Ricardo Adocicados 478.6814 261731.01618233617
35 FRANK Frankenversand 598.3897 258549.52474676404
36 SANTG Santé Gourmet 358.4468 244037.02482291666
37 SEVES Seven Seas Imports 660.4634 243735.9443115388
38 TOMSP Toms Spezialitäten 353.8571 239330.97340659337
39 DRACD Drachenblut Delikatessen 376.321 238408.13218777778
40 LAMAI La maison d'Asie 331.3661 234950.30823118277
41 LILAS LILA-Supermercado 524.2664 234775.24816292332
42 LEHMS Lehmanns Marktstand 545.6928 232028.1658786775
43 CHOPS Chop-suey Chinese 585.7409 221713.36443722952
44 SPECD Spécialités du monde 403.8916 217174.6304166667
45 COMMI Comércio Mineiro 381.075 212123.27847222224
46 GOURL Gourmet Lanchonetes 458.0121 209091.74545087724
47 PRINI Princesa Isabel Vinhos 531.71 195028.78766666664
48 VICTE Victuailles en stock 397.484 189342.0414
49 LINOD LINO-Delicateses 511.13 187181.38414705874
50 GROSR GROSELLA-Restaurante 372.175 180171.58916666664
51 BONAP Bon app' 542.067 164664.25266781176
52 WELLI Wellington Importadora 341.0894 142395.4743274854
53 WARTH Wartian Herkku 449.1108 130477.45890765774
54 MORGK Morgenstern Gesundkost 458.3818 120660.45963636362
55 OCEAN Océano Atlántico Ltda. 314.5636 117213.61454545458
56 ANTON Antonio Moreno Taquería 442.0794 113965.2128308824
57 TRADH Tradição Hipermercados 562.3553 113479.60587692303
58 WANDK Die Wandernde Kuh 409.7634 113120.21111153845
59 REGGC Reggiani Caseifici 343.4363 105556.71861471859
60 LONEP Lonesome Pine Restaurant 304.1857 95778.53208791208
61 BSBEV B's Beverages 276.8136 95662.24409090911
62 ALFKI Alfreds Futterkiste 383.0166 92154.29424242425
63 OTTIK Ottilies Käseladen 453.7068 87174.67941502466
64 MAGAA Magazzini Alimentari Riuniti 362.088 81324.38897619052
65 FURIB Furia Bacalhau e Frutos do Mar 357.5775 77949.6711776316
66 LETSS Let's Stop N Shop 349.002 75112.44348444446
67 PERIC Pericles Comidas clásicas 303.0142 72403.39208791203
68 FAMIA Familia Arquibaldo 233.6263 68642.8009356725
69 ISLAT Island Trading 267.2304 60677.24130434781
70 THECR The Cracker Box 243.405 57680.14362857142
71 QUEDE Que Delícia 290.5679 53396.89287807968
72 BLAUS Blauer See Delikatessen 231.4142 45221.94131868131
73 CONSH Consolidated Holdings 245.5857 39441.64809523811
74 FRANS Franchi S.p.A. 154.57 37612.44455555555
75 DUMON Du monde entier 179.5444 30211.81777777779
76 RANCH Rancho grande 237.0083 27329.368106060607
77 WOLZA Wolski Zajazd 220.7468 26593.677156250007
78 LACOR La corne d'abondance 181.0954 24172.353227272728
79 TRAIH Trail's Head Gourmet Provisioners 174.5777 19035.91194444445
80 ANATR Ana Trujillo Emparedados y helados 140.295 18442.554694444443
81 WILMK Wilman Kala 185.9617 16509.61485294118
82 CACTU Cactus Comidas para llevar 164.9818 15931.353636363638
83 ROMEY Romero y tomillo 104.8064 11003.8313478022
84 VINET Vins et alcools Chevalier 148 6990.373333333332
85 NORTS North/South 108.1666 4497.7666666666655
86 GALED Galería del gastrónomo 104.5875 2758.1898214285693
87 LAZYK Lazy K Kountry Store 178.5 1984.5
88 CENTC Centro comercial Moctezuma 50.4 1752.3200000000006
89 LAUGB Laughing Bacchus Wine Cellars 65.3125 1642.638392857143


2. Usage of VAR to see price variance across product categories

SQL Server Query 2

            
SELECT  
p.CategoryID,
c.CategoryName,
AVG(p.UnitPrice) AS AveragePrice,
VAR(p.UnitPrice) AS PriceVariance
FROM Products AS p
JOIN Categories AS c ON p.CategoryID = c.CategoryID
GROUP BY p.CategoryID, c.CategoryName
ORDER BY PriceVariance DESC;

Create SQL query with SqlQueryBuilder 2

            
var (sql2, parameters2) = new SqlQueryBuilder()  
.Select()
.Column("p.CategoryID", "CategoryID")
.Column("c.CategoryName", "CategoryName")
.Column(new AVG(new Column("p.UnitPrice")), "AveragePrice")
.Column(new VAR(new Column("p.UnitPrice")), "PriceVariance")
.From("Products", "p")
.Join(new List<IJoin>()
{
new INNERJOIN().TableName(new Table("Categories","c"))
.On(new Column("p.CategoryID").Equale(new Column("c.CategoryID")))
})
.GroupBy(new GroupBy("p.CategoryID", "c.CategoryName"))
.OrderBy(new OrderBy()
.SetColumnDescending("PriceVariance"))
.Build();

Query build by SqlQueryBuilder 2

            
SELECT p.CategoryID AS CategoryID,
       c.CategoryName AS CategoryName,
       AVG(p.UnitPrice) AS AveragePrice,
       VAR(p.UnitPrice) AS PriceVariance
FROM Products AS p
     INNER JOIN
     Categories AS c
     ON p.CategoryID = c.CategoryID
GROUP BY p.CategoryID, c.CategoryName
ORDER BY PriceVariance DESC;


            
        

Parameters (If used)

Name Value

Query Results 2:

  CategoryID CategoryName AveragePrice PriceVariance
1 1 Beverages 37.9791 5144.86884469697
2 6 Meat/Poultry 54.0066 2092.197266666666
3 3 Confections 25.1600 451.91229999999996
4 7 Produce 32.3700 297.4595000000004
5 8 Seafood 20.6825 231.2588568181819
6 4 Dairy Products 28.7300 218.62900000000002
7 5 Grains/Cereals 20.2500 137.72916666666666
8 2 Condiments 23.0625 103.7627840909091


3. Usage of VAR for variance of price within categories

SQL Server Query 3

            
SELECT  
p.ProductID,
p.ProductName,
p.CategoryID,
c.CategoryName,
p.UnitPrice,
VAR(p.UnitPrice) OVER (PARTITION BY p.CategoryID) AS CategoryPriceVariance,
(p.UnitPrice - AVG(p.UnitPrice) OVER (PARTITION BY p.CategoryID)) / SQRT(VAR(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 VAR(new Column("p.UnitPrice")).OVER(new OVER().PARTITION_BY(new Column("p.CategoryID"))), "CategoryPriceVariance")
.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 VAR(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,
       VAR(p.UnitPrice) OVER (PARTITION BY p.CategoryID) AS CategoryPriceVariance,
       (p.UnitPrice - AVG(p.UnitPrice) OVER (PARTITION BY p.CategoryID)) / SQRT(VAR(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 CategoryPriceVariance CategoryPricePopulationVariance ZScore
1 24 Guaraná Fantástica 1 Beverages 4.5000 5144.86884469697 0 -0.46675246539281917
2 75 Rhönbräu Klosterbier 1 Beverages 7.7500 5144.86884469697 0 -0.4214422416255535
3 67 Laughing Lumberjack Lager 1 Beverages 14.0000 5144.86884469697 0 -0.3343071959192735
4 34 Sasquatch Ale 1 Beverages 14.0000 5144.86884469697 0 -0.3343071959192735
5 70 Outback Lager 1 Beverages 15.0000 5144.86884469697 0 -0.3203655886062687
6 39 Chartreuse verte 1 Beverages 18.0000 5144.86884469697 0 -0.27854076666725425
7 35 Steeleye Stout 1 Beverages 18.0000 5144.86884469697 0 -0.27854076666725425
8 1 Chai 1 Beverages 18.0000 5144.86884469697 0 -0.27854076666725425
9 76 Lakkalikööri 1 Beverages 18.0000 5144.86884469697 0 -0.27854076666725425
10 2 Chang 1 Beverages 19.0000 5144.86884469697 0 -0.26459915935424944
11 43 Ipoh Coffee 1 Beverages 46.0000 5144.86884469697 0 0.11182423809688022
12 38 Côte de Blaye 1 Beverages 263.5000 5144.86884469697 0 3.144123828675425
13 3 Aniseed Syrup 2 Condiments 10.0000 103.7627840909091 0 -1.2823468091712305
14 77 Original Frankfurter grüne Soße 2 Condiments 13.0000 103.7627840909091 0 -0.9878365372084598
15 15 Genen Shouyu 2 Condiments 15.5000 103.7627840909091 0 -0.7424113105728176
16 66 Louisiana Hot Spiced Okra 2 Condiments 17.0000 103.7627840909091 0 -0.5951561745914322
17 44 Gula Malacca 2 Condiments 19.4500 103.7627840909091 0 -0.3546394524885029
18 65 Louisiana Fiery Hot Pepper Sauce 2 Condiments 21.0500 103.7627840909091 0 -0.19756730744169196
19 5 Chef Anton's Gumbo Mix 2 Condiments 21.3500 103.7627840909091 0 -0.1681162802454149
20 4 Chef Anton's Cajun Seasoning 2 Condiments 22.0000 103.7627840909091 0 -0.10430572132014793
21 6 Grandma's Boysenberry Spread 2 Condiments 25.0000 103.7627840909091 0 0.19020455064262268
22 61 Sirop d'érable 2 Condiments 28.5000 103.7627840909091 0 0.5337998679325218
23 8 Northwoods Cranberry Sauce 2 Condiments 40.0000 103.7627840909091 0 1.6627559104564757
24 63 Vegie-spread 2 Condiments 43.9000 103.7627840909091 0 2.0456192640080775
25 19 Teatime Chocolate Biscuits 3 Confections 9.2000 451.91229999999996 0 -0.7507680909741813
26 47 Zaanse koeken 3 Confections 9.5000 451.91229999999996 0 -0.7366559088130125
27 21 Sir Rodney's Scones 3 Confections 10.0000 451.91229999999996 0 -0.7131356052110644
28 68 Scottish Longbreads 3 Confections 12.5000 451.91229999999996 0 -0.5955340872013243
29 48 Chocolade 3 Confections 12.7500 451.91229999999996 0 -0.5837739354003503
30 25 NuNuCa Nuß-Nougat-Creme 3 Confections 14.0000 451.91229999999996 0 -0.5249731763954801
31 50 Valkoinen suklaa 3 Confections 16.2500 451.91229999999996 0 -0.41913181018671397
32 16 Pavlova 3 Confections 17.4500 451.91229999999996 0 -0.3626830815420387
33 49 Maxilaku 3 Confections 20.0000 451.91229999999996 0 -0.24272953317210372
34 26 Gumbär Gummibärchen 3 Confections 31.2300 451.91229999999996 0 0.28553648572764917
35 27 Schoggi Schokolade 3 Confections 43.9000 451.91229999999996 0 0.8815409790010122
36 62 Tarte au sucre 3 Confections 49.3000 451.91229999999996 0 1.1355602579020512
37 20 Sir Rodney's Marmalade 3 Confections 81.0000 451.91229999999996 0 2.6267475062655565
38 33 Geitost 4 Dairy Products 2.5000 218.62900000000002 0 -1.773962383298419
39 31 Gorgonzola Telino 4 Dairy Products 12.5000 218.62900000000002 0 -1.0976519054873557
40 11 Queso Cabrales 4 Dairy Products 21.0000 218.62900000000002 0 -0.522787999347952
41 71 Flotemysost 4 Dairy Products 21.5000 218.62900000000002 0 -0.4889724754573988
42 32 Mascarpone Fabioli 4 Dairy Products 32.0000 218.62900000000002 0 0.22115352624421772
43 60 Camembert Pierrot 4 Dairy Products 34.0000 218.62900000000002 0 0.3564156218064303
44 72 Mozzarella di Giovanni 4 Dairy Products 34.8000 218.62900000000002 0 0.4105204600313155
45 69 Gudbrandsdalsost 4 Dairy Products 36.0000 218.62900000000002 0 0.491677717368643
46 12 Queso Manchego La Pastora 4 Dairy Products 38.0000 218.62900000000002 0 0.6269398129308557
47 59 Raclette Courdavault 4 Dairy Products 55.0000 218.62900000000002 0 1.7766676252096634
48 52 Filo Mix 5 Grains/Cereals 7.0000 137.72916666666666 0 -1.1290233356007147
49 23 Tunnbröd 5 Grains/Cereals 9.0000 137.72916666666666 0 -0.9586047189062671
50 42 Singaporean Hokkien Fried Mee 5 Grains/Cereals 14.0000 137.72916666666666 0 -0.5325581771701484
51 57 Ravioli Angelo 5 Grains/Cereals 19.5000 137.72916666666666 0 -0.0639069812604178
52 22 Gustaf's Knäckebröd 5 Grains/Cereals 21.0000 137.72916666666666 0 0.0639069812604178
53 64 Wimmers gute Semmelknödel 5 Grains/Cereals 33.2500 137.72916666666666 0 1.1077210085139086
54 56 Gnocchi di nonna Alice 5 Grains/Cereals 38.0000 137.72916666666666 0 1.5124652231632214
55 54 Tourtière 6 Meat/Poultry 7.4500 2092.197266666666 0 -1.0178410011631822
56 55 Pâté chinois 6 Meat/Poultry 24.0000 2092.197266666666 0 -0.6560175739960208
57 53 Perth Pasties 6 Meat/Poultry 32.8000 2092.197266666666 0 -0.463628077979645
58 17 Alice Mutton 6 Meat/Poultry 39.0000 2092.197266666666 0 -0.3280809330590166
59 9 Mishi Kobe Niku 6 Meat/Poultry 97.0000 2092.197266666666 0 0.9399407452307333
60 29 Thüringer Rostbratwurst 6 Meat/Poultry 123.7900 2092.197266666666 0 1.525635585944223
61 74 Longlife Tofu 7 Produce 10.0000 297.4595000000004 0 -1.2970360954096023
62 14 Tofu 7 Produce 23.2500 297.4595000000004 0 -0.5287871788169679
63 7 Uncle Bob's Organic Dried Pears 7 Produce 30.0000 297.4595000000004 0 -0.13741508923204102
64 28 Rössle Sauerkraut 7 Produce 45.6000 297.4595000000004 0 0.7670892955864568
65 51 Manjimup Dried Apples 7 Produce 53.0000 297.4595000000004 0 1.1961490678721545
66 13 Konbu 8 Seafood 6.0000 231.2588568181818 0 -0.9654967634769832
67 45 Rogede sild 8 Seafood 9.5000 231.2588568181818 0 -0.7353425886314569
68 41 Jack's New England Clam Chowder 8 Seafood 9.6500 231.2588568181818 0 -0.7254788382809343
69 46 Spegesild 8 Seafood 12.0000 231.2588568181818 0 -0.5709467494560808
70 58 Escargots de Bourgogne 8 Seafood 13.2500 231.2588568181818 0 -0.48874882986839285
71 73 Röd Kaviar 8 Seafood 15.0000 231.2588568181818 0 -0.3736717424456297
72 40 Boston Crab Meat 8 Seafood 18.4000 231.2588568181818 0 -0.1500934011671183
73 36 Inlagd Sill 8 Seafood 19.0000 231.2588568181818 0 -0.11063839976502805
74 30 Nord-Ost Matjeshering 8 Seafood 25.8900 231.2588568181818 0 0.3424365330023082
75 37 Gravad lax 8 Seafood 26.0000 231.2588568181818 0 0.34966994992602474
76 10 Ikura 8 Seafood 31.0000 231.2588568181818 0 0.6784616282767768
77 18 Carnarvon Tigers 8 Seafood 62.5000 231.2588568181818 0 2.7498492018865144