STDEVP SQL function


1. Usage of STDEV to see standard deviation of orders

SQL Server Query 1

            
SELECT  
c.Country,
AVG(od.UnitPrice * od.Quantity) AS AverageOrderAmount,
STDEVP(od.UnitPrice * od.Quantity) AS OrderAmountPopulationStandardDeviation
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 OrderAmountPopulationStandardDeviation 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("od.Quantity")), "AverageOrderAmount")
.Column(new STDEVP(new ColumnArithmatic("od.UnitPrice").MULTIPLY("od.Quantity")), "OrderAmountPopulationStandardDeviation")
.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(new Column("c.Country")))
.OrderBy(new OrderBy().SetColumnDescending("OrderAmountPopulationStandardDeviation"))
.Build();

Query build by SqlQueryBuilder 1

            
SELECT c.Country AS Country,
       AVG(od.UnitPrice * od.Quantity) AS AverageOrderAmount,
       STDEVP(od.UnitPrice * od.Quantity) AS OrderAmountPopulationStandardDeviation
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 OrderAmountPopulationStandardDeviation DESC;


            
        

Parameters (If used)

Name Value

Query Results 1:

  Country AverageOrderAmount OrderAmountPopulationStandardDeviation OrderAmountPopulationVariance
1 Ireland 1042.1343 1681.55247998094 0
2 Denmark 756.1358 1555.0900226515284 0
3 Austria 1115.9730 1412.5052264456788 0
4 Brazil 566.3471 1289.4316667727498 0
5 Canada 737.7880 1275.479092415604 0
6 Germany 745.8555 1211.0011388356534 0
7 USA 748.7698 1163.77649213156 0
8 Sweden 613.6463 859.1273909789394 0
9 Switzerland 633.0673 718.5541455646442 0
10 Belgium 627.4103 592.337197862249 0
11 Spain 359.8498 548.1323085428228 0
12 France 464.6671 536.8490213704046 0
13 UK 449.0111 517.4320825584914 0
14 Mexico 334.3534 497.0879687773799 0
15 Venezuela 515.3804 490.1238969986493 0
16 Norway 358.4468 478.31444758807396 0
17 Portugal 415.6216 338.5483893732508 0
18 Finland 366.2675 326.82384457830676 0
19 Italy 315.1915 291.496452248039 0
20 Argentina 238.7970 227.20766801125836 0
21 Poland 220.7468 157.89734745708802 0


2. Usage of STDEV to see price variation with products

SQL Server Query 2

            
SELECT  
p.SupplierID,
s.CompanyName AS SupplierName,
AVG(p.UnitPrice) AS AveragePrice,
STDEVP(p.UnitPrice) AS PricePopulationStandardDeviation
FROM Products AS p
JOIN Suppliers AS s ON p.SupplierID = s.SupplierID
GROUP BY p.SupplierID, s.CompanyName
ORDER BY PricePopulationStandardDeviation 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 STDEVP(new Column("p.UnitPrice")), "PricePopulationStandardDeviation")
.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("PricePopulationStandardDeviation"))
.Build();

Query build by SqlQueryBuilder 2

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


            
        

Parameters (If used)

Name Value

Query Results 2:

  SupplierID SupplierName AveragePrice PricePopulationStandardDeviation PricePopulationVariance
1 18 Aux joyeux ecclésiastiques 140.7500 122.75 0
2 12 Plutzer Lebensmittelgroßmärkte AG 44.6780 41.85052133486511 0
3 4 Tokyo Traders 46.0000 37.067505985701274 0
4 8 Specialty Biscuits, Ltd. 28.1750 30.522809094183977 0
5 24 G'day, Mate 30.9333 18.825750685932526 0
6 7 Pavlova, Ltd. 35.5700 17.651277574158765 0
7 20 Leka Trading 26.4833 13.978574875700149 0
8 15 Norske Meierier 20.0000 13.717385562368168 0
9 11 Heli Süßwaren GmbH & Co. KG 29.7100 12.253851095335976 0
10 28 Gai pâturage 44.5000 10.5 0
11 29 Forêts d'érables 38.9000 10.400000000000004 0
12 14 Formaggi Fortini s.r.l. 26.4333 9.918445218659803 0
13 26 Pasta Buttini s.r.l. 28.7500 9.25 0
14 5 Cooperativa de Quesos 'Las Cabras' 29.5000 8.5 0
15 25 Ma Maison 15.7250 8.275000000000002 0
16 6 Mayumi's 14.9166 7.054352478356102 0
17 3 Grandma Kelly's Homestead 31.6666 6.236095644623231 0
18 9 PB Knäckebröd AB 15.0000 6 0
19 17 Svensk Sjöföda AB 20.0000 4.546060565661952 0
20 19 New England Seafood Cannery 14.0250 4.375 0
21 1 Exotic Liquids 15.6666 4.02768199119819 0
22 2 New Orleans Cajun Delights 20.3500 1.9643701280563013 0
23 16 Bigfoot Breweries 15.3333 1.8856180831641234 0
24 22 Zaanse Snoepfabriek 11.1250 1.625 0
25 23 Karkki Oy 18.0833 1.5320646925708488 0
26 21 Lyngbysild 10.7500 1.25 0
27 27 Escargots Nouveaux 13.2500 0 0
28 13 Nord-Ost-Fisch Handelsgesellschaft mbH 25.8900 0 0
29 10 Refrescos Americanas LTDA 4.5000 0 0


3. Usage of STDEV for relative price variability

SQL Server Query 3

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