0

I writing a query and getting the value

I have tried this on SQL Server 2012

;With CTE_ColorIndicator
AS
(
    select Name,Value,Report_Date,
    Dense_Rank() Over(Order by Report_Date DESC)Dno,
    (AVG(value)+(3*STDEV(value))) UpperLimit,
    (AVG(value)+(3*STDEV(value)))*0.85 Limit 
    from #Temp1 
    group by Name,value,Report_date
)

Select * 
from CTE_ColorIndicator 
where Dno <=90 and Dno > 7

"I expected the aggregated values in the place of Upper-limit and limit columns" "But the output is showing 'NULL' values in the upper-limit and limit columns"

mkRabbani
  • 16,295
  • 2
  • 15
  • 24
venugopal
  • 79
  • 10

1 Answers1

0

You are grouping by value. Hence, the standard deviation is not defined. I think you intend for the subquery to be:

select Name, Report_Date,
       Dense_Rank() Over (Order by Report_Date DESC) as Dno,
       (AVG(value) + 3 * STDEV(value)) as UpperLimit,
       (AVG(value) + 3 * STDEV(value)) * 0.85 as Limit 
from #Temp1 
group by Name, Report_date
Gordon Linoff
  • 1,242,037
  • 58
  • 646
  • 786