SQL SERVER 2012:每人行数的标准差
SQL SERVER 2012: Standard Deviation of rows per person
我有 table ITEMS
:
Itemid,
ItemName,
OwnerID
每一行代表一个项目。一个所有者可以有多个项目。我想计算每个所有者 items/rows 的标准差。是否可以使用 STDEV
内置函数来计算?
我试过了,但似乎不行:
select STDEV(COUNT(*)) from dbo.ITEMS
GROUP BY ITEMS.OwnerID
我觉得
SELECT STDEV(IQ.cnt) as SDofCounts from --'from' was missing
(select ITEMS.OwnerID, COUNT(0) as cnt from dbo.ITEMS
GROUP BY ITEMS.OwnerID
) IQ
++++++++++++++++++
如果你运行这个要看一些数据
select ITEMS.OwnerID, COUNT(0) as cnt, COUNT(0) * COUNT(0) as EXS from dbo.ITEMS
GROUP BY ITEMS.OwnerID
那你可以做
SELECT STDEV(IQ.cnt) as SDofCounts,
SUM(EXS) as SigmaEXsquare,
SUM(1.0 * cnt) / COUNT(0) as MU,
SUM(EXS) - (SUM(1.0 * cnt) / COUNT(0)) * (SUM(1.0 * cnt) / COUNT(0)) as Variance,
SQRT(SUM(EXS) - (SUM(1.0 * cnt) / COUNT(0)) * (SUM(1.0 * cnt) / COUNT(0))) AS SDcalc
from
(select ITEMS.OwnerID, COUNT(0) as cnt, COUNT(0) * COUNT(0) as EXS from dbo.ITEMS
GROUP BY ITEMS.OwnerID
) IQ
我有 table ITEMS
:
Itemid,
ItemName,
OwnerID
每一行代表一个项目。一个所有者可以有多个项目。我想计算每个所有者 items/rows 的标准差。是否可以使用 STDEV
内置函数来计算?
我试过了,但似乎不行:
select STDEV(COUNT(*)) from dbo.ITEMS
GROUP BY ITEMS.OwnerID
我觉得
SELECT STDEV(IQ.cnt) as SDofCounts from --'from' was missing
(select ITEMS.OwnerID, COUNT(0) as cnt from dbo.ITEMS
GROUP BY ITEMS.OwnerID
) IQ
++++++++++++++++++
如果你运行这个要看一些数据
select ITEMS.OwnerID, COUNT(0) as cnt, COUNT(0) * COUNT(0) as EXS from dbo.ITEMS
GROUP BY ITEMS.OwnerID
那你可以做
SELECT STDEV(IQ.cnt) as SDofCounts,
SUM(EXS) as SigmaEXsquare,
SUM(1.0 * cnt) / COUNT(0) as MU,
SUM(EXS) - (SUM(1.0 * cnt) / COUNT(0)) * (SUM(1.0 * cnt) / COUNT(0)) as Variance,
SQRT(SUM(EXS) - (SUM(1.0 * cnt) / COUNT(0)) * (SUM(1.0 * cnt) / COUNT(0))) AS SDcalc
from
(select ITEMS.OwnerID, COUNT(0) as cnt, COUNT(0) * COUNT(0) as EXS from dbo.ITEMS
GROUP BY ITEMS.OwnerID
) IQ