STDEV SQL function


1. Usage of STDEV to see standard deviation of orders

SQL Server Query 1

            
SELECT  
o.CustomerID,
c.CompanyName,
AVG(od.UnitPrice * od.Quantity) AS AverageOrderAmount,
STDEV(od.UnitPrice * od.Quantity) AS OrderAmountStandardDeviation
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 OrderAmountStandardDeviation 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 STDEV(new ColumnArithmatic("od.UnitPrice").MULTIPLY("od.Quantity")), "OrderAmountStandardDeviation")
.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("OrderAmountStandardDeviation"))
.Build();

Query build by SqlQueryBuilder 1

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


            
        

Parameters (If used)

Name Value

Query Results 1:

  CustomerID CompanyName AverageOrderAmount OrderAmountStandardDeviation
1 HANAR Hanari Carnes 1065.6609 2735.7610892062967
2 SIMOB Simons bistro 1209.2300 2620.7416492419525
3 PICCO Piccolo und mehr 1141.7369 2114.5210881177945
4 QUICK QUICK-Stop 1366.0859 1970.4118097944297
5 MEREP Mère Paillarde 1006.3718 1833.2612140284011
6 GREAL Great Lakes Food Market 895.9604 1781.6519731831474
7 HUNGO Hungry Owl All-Night Grocers 1042.1343 1697.0509871492695
8 RATTC Rattlesnake Canyon Grocery 735.8577 1522.530652932054
9 QUEEN Queen Cozinha 755.6525 1354.5185429039034
10 KOENE Königlich Essen 813.9935 1314.9856866551163
11 ERNSH Ernst Handel 1110.1635 1222.7678384751548
12 SAVEA Save-a-lot Markets 997.1843 1132.3016973590748
13 WHITC White Clover Markets 726.8362 1077.0263423788133
14 FOLKO Folk och fä HB 723.4566 1024.0922949572998
15 SPLIR Split Rail Beer & Ale 624.4850 964.5069300861505
16 THEBI The Big Cheese 480.1428 951.1709605483529
17 BOLID Bólido Comidas preparadas 882.9666 927.4294316370742
18 RICSU Richter Supermarkt 667.7733 873.1088309512494
19 FOLIG Folies gourmandes 729.1812 849.613507597229
20 BLONP Blondesddsl père et fils 734.1538 734.2384583938759
21 AROUT Around the Horn 460.2166 719.4822674402195
22 TORTU Tortuga Restaurante 372.8327 708.7892966273876
23 BERGS Berglunds snabbköp 518.6182 692.2232621840373
24 MAISD Maison Dewey 613.5635 619.9687958774738
25 FRANR France restauration 528.6933 603.0894012222954
26 SUPRD Suprêmes délices 633.4461 595.9045913770858
27 VAFFE Vaffeljernet 536.8967 592.0743295025667
28 BOTTM Bottom-Dollar Markets 645.9342 583.9594702610473
29 GODOS Godos Cocina Típica 455.0038 581.4469547470478
30 EASTC Eastern Connection 715.8885 564.5778888628719
31 OLDWO Old World Delicatessen 680.2145 556.7765345737846
32 HILAA HILARION-Abastos 524.7017 556.0535885975579
33 HUNGC Hungry Coyote Import Store 340.3555 536.0232810035193
34 RICAR Ricardo Adocicados 478.6814 511.59653652300676
35 FRANK Frankenversand 598.3897 508.47765412726255
36 SANTG Santé Gourmet 358.4468 494.00103726906957
37 SEVES Seven Seas Imports 660.4634 493.696206499036
38 TOMSP Toms Spezialitäten 353.8571 489.2146496238572
39 DRACD Drachenblut Delikatessen 376.3210 488.27055224309584
40 LAMAI La maison d'Asie 331.3661 484.71672988579917
41 LILAS LILA-Supermercado 524.2664 484.536116469065
42 LEHMS Lehmanns Marktstand 545.6928 481.69302037571344
43 CHOPS Chop-suey Chinese 585.7409 470.8644862773466
44 SPECD Spécialités du monde 403.8916 466.0199892887286
45 COMMI Comércio Mineiro 381.0750 460.5684297389718
46 GOURL Gourmet Lanchonetes 458.0121 457.2655087046007
47 PRINI Princesa Isabel Vinhos 531.7100 441.6206377272994
48 VICTE Victuailles en stock 397.4840 435.1345095484843
49 LINOD LINO-Delicateses 511.1300 432.64463956815496
50 GROSR GROSELLA-Restaurante 372.1750 424.46624031442906
51 BONAP Bon app' 542.0670 405.7884333834711
52 WELLI Wellington Importadora 341.0894 377.3532487305302
53 WARTH Wartian Herkku 449.1108 361.2166370859152
54 MORGK Morgenstern Gesundkost 458.3818 347.36214479468487
55 OCEAN Océano Atlántico Ltda. 314.5636 342.3647390509931
56 ANTON Antonio Moreno Taquería 442.0794 337.58734104062967
57 TRADH Tradição Hipermercados 562.3553 336.8673416597742
58 WANDK Die Wandernde Kuh 409.7634 336.33348199597737
59 REGGC Reggiani Caseifici 343.4363 324.89493473232017
60 LONEP Lonesome Pine Restaurant 304.1857 309.4810690299361
61 BSBEV B's Beverages 276.8136 309.29313618460577
62 ALFKI Alfreds Futterkiste 383.0166 303.569257736063
63 OTTIK Ottilies Käseladen 453.7068 295.2535849317069
64 MAGAA Magazzini Alimentari Riuniti 362.0880 285.1743133176453
65 FURIB Furia Bacalhau e Frutos do Mar 357.5775 279.19468329040865
66 LETSS Let's Stop N Shop 349.0020 274.0664946403417
67 PERIC Pericles Comidas clásicas 303.0142 269.07878416536676
68 FAMIA Familia Arquibaldo 233.6263 261.9977116993057
69 ISLAT Island Trading 267.2304 246.32750821690178
70 THECR The Cracker Box 243.4050 240.16690785487376
71 QUEDE Que Delícia 290.5679 231.07767715225043
72 BLAUS Blauer See Delikatessen 231.4142 212.6545116349082
73 CONSH Consolidated Holdings 245.5857 198.59921473973182
74 FRANS Franchi S.p.A. 154.5700 193.9392805894555
75 DUMON Du monde entier 179.5444 173.8154704788322
76 RANCH Rancho grande 237.0083 165.31596446217952
77 WOLZA Wolski Zajazd 220.7468 163.07567922976745
78 LACOR La corne d'abondance 181.0954 155.47460637439391
79 TRAIH Trail's Head Gourmet Provisioners 174.5777 137.97069233878784
80 ANATR Ana Trujillo Emparedados y helados 140.2950 135.80336775810989
81 WILMK Wilman Kala 185.9617 128.48974610038414
82 CACTU Cactus Comidas para llevar 164.9818 126.21946615464525
83 ROMEY Romero y tomillo 104.8064 104.89914846080592
84 VINET Vins et alcools Chevalier 148.0000 83.60845252325468
85 NORTS North/South 108.1666 67.06539097527626
86 GALED Galería del gastrónomo 104.5875 52.51847124039855
87 LAZYK Lazy K Kountry Store 178.5000 44.54772721475249
88 CENTC Centro comercial Moctezuma 50.4000 41.86072144624362
89 LAUGB Laughing Bacchus Wine Cellars 65.3125 40.52947560550399


2. Usage of STDEV to find standard deviation across product categories

SQL Server Query 2

            
SELECT  
p.CategoryID,
c.CategoryName,
AVG(p.UnitPrice) AS AveragePrice,
STDEV(p.UnitPrice) AS PriceStandardDeviation
FROM Products AS p
JOIN Categories AS c ON p.CategoryID = c.CategoryID
GROUP BY p.CategoryID, c.CategoryName
ORDER BY PriceStandardDeviation 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 STDEV(new Column("p.UnitPrice")), "PriceStandardDeviation")
.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("PriceStandardDeviation"))
.Build();

Query build by SqlQueryBuilder 2

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


            
        

Parameters (If used)

Name Value

Query Results 2:

  CategoryID CategoryName AveragePrice PriceStandardDeviation
1 1 Beverages 37.9791 71.72774110967785
2 6 Meat/Poultry 54.0066 45.740542920549885
3 3 Confections 25.1600 21.258228994909242
4 7 Produce 32.3700 17.247014234353735
5 8 Seafood 20.6825 15.207197533345251
6 4 Dairy Products 28.7300 14.786108345335496
7 5 Grains/Cereals 20.2500 11.735807030906168
8 2 Condiments 23.0625 10.186401920742627


3. Usage of STDEV to compare the price of each products

SQL Server Query 3

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