2010-01-11 74 views
2

我有這樣的SQL的MS SQL Server 2008中:追加模式這個SQL

WITH CTE AS (
SELECT e_id, 
    scale, 
    ROW_NUMBER() OVER(PARTITION BY e_id ORDER BY scale ASC) AS rn, 
    COUNT(scale) OVER(PARTITION BY e_id) AS cn 
FROM waypoint.dbo.ScoreMaster 
WHERE scale IS NOT NULL 
) 
SELECT e_id, 

cast(AVG (cast(scale as decimal(5,2))) as decimal(5,3)) as [AVG], 

cast (STDEV(cast(scale as decimal(5,1))) as decimal(5,3)) as [STDDEV], 
AVG(CASE WHEN 2 * rn - cn BETWEEN 0 AND 2 THEN 
    scale END) AS FinancialMedian, 

MAX(CASE WHEN 2 * rn - cn BETWEEN 0 AND 2 THEN 
    scale END) AS StatisticalMedian 
from CTE 
GROUP BY e_id 

,我想補充的模式。 我一直在嘗試不同的想法,但沒有任何工程尚未...

+0

你怎麼想的時候剛好有存在雙峯樣品(兩種模式),您的SQL表現在使用MAX(co.sn)?何時有兩種以上的模式? – kurast 2010-01-11 19:45:45

回答

1
WITH s (e_id, scale) AS 
     (
     SELECT 1, 0.0 
     UNION ALL 
     SELECT 1, 0.1 
     UNION ALL 
     SELECT 1, 0.1 
     UNION ALL 
     SELECT 1, 0.2 
     UNION ALL 
     SELECT 1, 0.2 
     UNION ALL 
     SELECT 1, 0.3 
     ), 
     cte AS 
     (
     SELECT e_id, 
       scale, 
       ROW_NUMBER() OVER (PARTITION BY e_id ORDER BY scale ASC) AS rn, 
       COUNT(scale) OVER (PARTITION BY e_id) AS cn, 
       COUNT(*) OVER (PARTITION BY e_id, scale) AS sn 
     FROM s 
     WHERE scale IS NOT NULL 
     ) 
SELECT e_id, 
     AVG(scale), 
     (
     SELECT AVG(scale) 
     FROM cte ci 
     WHERE ci.e_id = co.e_id 
       AND ci.sn = MAX(co.sn) 
     ) AS mode 
FROM cte co 
GROUP BY 
     e_id 

這將選擇所有模態尺度的平均值。將AVG替換爲TOP 1 ... ORDER BY以獲得單一比例。

注意,我們怎麼能WHERE條款:)

0

試試這個:

; WITH Mean AS (
SELECT AVG(scale)/1.0 AS MeanVal 
    FROM waypoint.dbo.ScoreMaster 
WHERE scale IS NOT NULL 
), Median AS (
SELECT ((SELECT TOP 1 scale 
      FROM (SELECT TOP 50 PERCENT scale FROM waypoint.dbo.ScoreMaster WHERE scale IS NOT NULL ORDER BY scale) AS A 
      ORDER BY scale DESC) 
     + (SELECT TOP 1 scale 
      FROM (SELECT TOP 50 PERCENT scale FROM waypoint.dbo.ScoreMaster WHERE scale IS NOT NULL ORDER BY scale DESC) AS A 
      ORDER BY scale ASC))/2.0 AS MedianVal 
), Mode AS (
SELECT scale AS ModeVal 
    FROM waypoint.dbo.ScoreMaster 
WHERE scale IS NOT NULL 
GROUP BY scale 
HAVING COUNT(scale) = (SELECT TOP 1 COUNT(scale) 
          FROM waypoint.dbo.ScoreMaster 
         WHERE scale IS NOT NULL 
         GROUP BY scale 
         ORDER BY COUNT(scale) DESC) 
) 
SELECT MeanVal 
    , MedianVal 
    , ModeVal 
    FROM Mean 
    , Median 
    , Mode 

我知道它的醜陋,但訣竅是交叉加入所有可能的值由給出模式值爲平均值中值

0

或者這樣:

; WITH Mean AS (
SELECT AVG(scale)/1.0 AS MeanVal 
    FROM waypoint.dbo.ScoreMaster 
WHERE scale IS NOT NULL 
), Median AS (
SELECT ((SELECT TOP 1 scale 
      FROM (SELECT TOP 50 PERCENT scale FROM waypoint.dbo.ScoreMaster WHERE scale IS NOT NULL ORDER BY scale) AS A 
      ORDER BY scale DESC) 
     + (SELECT TOP 1 scale 
      FROM (SELECT TOP 50 PERCENT scale FROM waypoint.dbo.ScoreMaster WHERE scale IS NOT NULL ORDER BY scale DESC) AS A 
      ORDER BY scale ASC))/2.0 AS MedianVal 
), Mode AS (
SELECT AVG(scale) AS ModeVal 
    FROM waypoint.dbo.ScoreMaster 
WHERE scale IS NOT NULL 
HAVING COUNT(scale) = (SELECT TOP 1 COUNT(scale) 
          FROM waypoint.dbo.ScoreMaster 
         WHERE scale IS NOT NULL 
         GROUP BY scale 
         ORDER BY COUNT(scale) DESC) 
) 
SELECT MeanVal 
    , MedianVal 
    , ModeVal 
    FROM Mean 
    , Median 
    , Mode 

如果你想模式是具有最高計數秤之間的AVG。