使用SQL进行Access访问的中位数计算

时间:2015-10-07 13:22:18

标签: ms-access median

我在下面的Access SQL查询中遇到了一些困难。它返回-1和0. -1告诉我有一个中位数。零是没有中位数(0是该中位数的正确值)。任何帮助改变查询以显示实际中值。这真的会有所帮助。谢谢。

SELECT (((Sum(IIf([CALCAULATION]<=[CALCULATION],1,0)))>=(Count(*)/2)) And ((Sum(IIf([CALCULATION]>=[CALCULATION],1,0)))>=(Count(*)/2))) AS Median, 
Datatable.Measure
FROM Datatable
WHERE (((Datatable.Date) Between #1/1/2014# And #1/31/2014#) AND ((Datatable.NAME)="Test"))
GROUP BY Datatable.Measure
ORDER BY Datatable.Measure;

2 个答案:

答案 0 :(得分:1)

因为[CALCAULATION]=[CALCULATION]永远是真的 因为(SUM(1)总是= COUNT(*)

这个

      SELECT (((SUM( 1)
               )>=(COUNT(*)/2
               )

将评估为真

所以

      SELECT (((SUM( 1)
               )>=(COUNT(*)/2
               )
         )
         AND
         ((SUM( 1
              )
           )>=(COUNT(*)/2)
         )) AS Median       

true and true相同,即true,即1。

这显然不是你想到的。

Here's a google search to help

我建议this link

这是SQL重新格式化的

  SELECT (((SUM( IIF([CALCAULATION]<=[CALCULATION],1,0))
           )>=(COUNT(*)/2
               )
         )
         AND
         ((SUM( IIF([CALCULATION]>=[CALCULATION],1,0)
              )
           )>=(COUNT(*)/2)
         )) AS Median
       , Datatable.Measure
    FROM Datatable
   WHERE (Datatable.Date BETWEEN #1/1/2014# AND #1/31/2014#
          AND 
          Datatable.NAME="Test"
         )
GROUP BY Datatable.Measure
ORDER BY Datatable.Measure;

第2部分

这里有一些用于创建中位数的SQL(请参阅NameNeededHereIThink和我的评论)。您可能还需要添加一个WHERE子句,将日期限制为一个月,如上例所示。注意表必须相同(或包含相同的数据!)

 SELECT CALCULATION AS Median
        -- #HARVEY  You might want to add the next lines
        , a3.MEASURE
        , a3.CALCULATION

  FROM ( SELECT a1.MEASURE
              , a1.CALCULATION
              , COUNT(a1.CALCULATION) Rank
           FROM Datatable_CALCULATION a1
              , Datatable_CALCULATION a2   
                --#HARVEy a1 and a2 MUST be the same table!
          WHERE a1.CALCULATION < a2.CALCULATION
             OR (a1.CALCULATION=a2.CALCULATION
                 AND 
                 a1.MEASURE    <= a2.MEASURE)
       GROUP BY a1.MEASURE
              , a1.CALCULATION
       --#HARVEY REMOVED NEXT LINE
       -- as order by in sub query cannot be relied upon
       -- and was only added here for testing purposes
       -- ORDER BY a1.CALCULATION DESC
       )
       a3
 WHERE Rank =
       -- as 99/2 = 45.5 you may need to round down 
       -- tot the  nearest integer
       (SELECT cint( ((COUNT()) /2) ) AS NameNeededHereIThink
       -- (SELECT ((COUNT()+1) /2) 
          FROM Datatable_CALCULATION
       );

答案 1 :(得分:0)

SELECT CALCULATION AS Median
     , a3.MEASURE
     , a3.CALCULATION
  FROM (  SELECT a1.MEASURE
              , a1.CALCULATION
              , COUNT(a1.CALCULATION) Rank
           FROM Datatable_CALCULATION a1
              , Datatable_0114_CALCULATION a2
          WHERE a1.CALCULATION < a2.CALCULATION
             OR (a1.CALCULATION=a2.CALCULATION
            AND a1.MEASURE    <= a2.MEASURE)
       GROUP BY a1.MEASURE
              , a1.CALCULATION
       )
       a3
 WHERE Rank =(
       (SELECT cint(((COUNT()) DIV 2)) AS Median
                  FROM[Datatable_CALCULATION]
               ));