Solved

STDEVP in SQL

Posted on 2016-10-24
2
38 Views
Last Modified: 2016-10-27
I have data that is arranged this way (see below) and need to calculate the Dispersion = Standard Deviation/Average of Total_NR rolled up by the MGR_NAME. How can I achieve this in T-SQL? Thank you.

The way it's supposed to work is: Sum (Total_NR) divided by the average of the sum of NR which we should then so a STDEVP on that average.

MGR_NAME      EMPL      HC      Total_NR           Avrg             Dispersion?
JOSE PH        1027         1           $3,974.00          0.000107      
JOSE PH      1233      1           $3,324.00          0.000074      
JOSE PH      1354      1         $51.87                  0.000012      
JOSE PH      1375      1      $2,302.67          0.000049      
JOSE PH      1486      1      $1,230.68          0.000045
0
Comment
Question by:saved4use
  • 2
2 Comments
 
LVL 18

Expert Comment

by:Pawan Kumar Khowal
ID: 41857935
can you pls post the expected output for Dispersion column?
0
 
LVL 18

Accepted Solution

by:
Pawan Kumar Khowal earned 500 total points
ID: 41857941
Try..

SELECT stdddv.*,AVG(Avrg) OVER (PARTITION BY MGR_NAME) AvgByManagerName , ny StandardDeviation, ny1  StandardDeviation2 ,
ny/AVG(Avrg) OVER (PARTITION BY MGR_NAME) Dispersion1, ny1/AVG(Avrg) OVER (PARTITION BY MGR_NAME) Dispersion2 FROM stdddv
CROSS APPLY
(
	SELECT STDEV(Total_NR) ny , STDEVP(Total_NR) ny1
	FROM stdddv 
)b

Open in new window


OR....

SELECT stdddv.*,AVG(Avrg) OVER (PARTITION BY MGR_NAME) AvgByManagerName , STDEVP(Total_NR) OVER(PARTITION BY MGR_NAME) StandardDeviation, STDEV(Total_NR) OVER(PARTITION BY MGR_NAME)  StandardDeviation2 ,
STDEVP(Total_NR) OVER(PARTITION BY MGR_NAME)/AVG(Avrg) OVER (PARTITION BY MGR_NAME) Dispersion1, STDEV(Total_NR) OVER(PARTITION BY MGR_NAME) /AVG(Avrg) OVER (PARTITION BY MGR_NAME) Dispersion2 
FROM stdddv

Open in new window

0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Problem with SqlConnection 5 124
MS SQL export CSV & schedule It 9 44
Usage Scenarios for Extended Events? 1 12
Caste datetime 2 25
In this article I will describe the Detach & Attach method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
This article describes how to use the timestamp of existing data in a database to allow Tableau to calculate the prior work day instead of relying on case statements or if statements to calculate the days of the week.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now