【问题标题】:How to create a new SQL table with Mean, Median, and Mode?如何使用均值、中位数和众数创建新的 SQL 表?
【发布时间】:2018-10-22 13:20:30
【问题描述】:

好的,所以我是 SQL 新手,这就是我问这个问题的原因。

我有一个名为:kpi_notification_metrics_per_month 的表 此表有 2 列:

  • 日期
  • 通知计数

我想创建一个全新的表格来显示

  • 平均
  • 中位数
  • 模式

对于 NotificationCount 列。

示例表:

Date    NotificationCount
01/04/2018 00:00    0
31/03/2018 00:00    0
25/03/2018 00:00    0
24/03/2018 00:00    0
22/03/2018 00:00    0
18/03/2018 00:00    0
17/03/2018 00:00    0
14/03/2018 00:00    0
11/03/2018 00:00    0
07/04/2018 00:00    1
26/03/2018 00:00    1
21/03/2018 00:00    1
15/03/2018 00:00    1
13/03/2018 00:00    1
12/03/2018 00:00    1
10/03/2018 00:00    1
08/04/2018 00:00    2
30/03/2018 00:00    2
09/03/2018 00:00    2
08/03/2018 00:00    2
20/03/2018 00:00    3
19/03/2018 00:00    4
02/04/2018 00:00    9
23/03/2018 00:00    11
27/03/2018 00:00    22
03/04/2018 00:00    28
28/03/2018 00:00    34
04/04/2018 00:00    39
05/04/2018 00:00    43
29/03/2018 00:00    47
06/04/2018 00:00    50
16/03/2018 00:00    140

预期结果:

Mean    Median  Mode
13.90625    1   0

【问题讨论】:

  • 添加一些示例表数据和预期结果 - 全部作为格式化文本,而不是图像。并创建一个视图而不是另一个表。 (以节省磁盘并确保数据一致性。)
  • @jarlh 好的,已添加表格和预期结果
  • 你用的是什么关系型数据库?
  • 你如何得到模式值 0?
  • @jarlh:MODE 是最常出现的值。此处为 0(出现 9 次)。 en.wikipedia.org/wiki/Mode_(statistics)

标签: sql mean median mode


【解决方案1】:

这是在 Oracle 中执行此操作的方法:

select
  avg(notificationcount) as statistic_mean,
  median(notificationcount) as statistic_median,
  stats_mode(notificationcount) as statistic_mode
from mytable;

不需要另一张桌子。您可以(并且应该)始终查询临时数据。为方便起见,您可以按照 jarlh 在请求 cmets 中的建议创建视图。

【讨论】:

    【解决方案2】:

    平均:使用Avg()

    Select Avg(NotificationCount)
    From   kpi_notification_metrics_per_month
    

    中位数:TOP 50 Percent 的数据按 ASC 和 DESC 排序,找到中间那个。

    Select ((
            Select Top 1 NotificationCount
            From   (
                    Select  Top 50 Percent NotificationCount
                    From    kpi_notification_metrics_per_month
                    Where   NotificationCount Is NOT NULL
                    Order By NotificationCount
                    ) As A
            Order By NotificationCountDESC) + 
            (
            Select Top 1 NotificationCount
            From   (
                    Select  Top 50 Percent NotificationCount
                    From    kpi_notification_metrics_per_month
                    Where   NotificationCount Is NOT NULL
                    Order By NotificationCount DESC
                    ) As A
            Order By NotificationCount Asc)) / 2
    

    模式:获取每个值集的计数,并按 DESC 顺序获取前 1 行。

    SELECT TOP 1 with ties NotificationCount
    FROM   kpi_notification_metrics_per_month
    WHERE  NotificationCount IS Not NULL
    GROUP  BY NotificationCount
    ORDER  BY COUNT(*) DESC
    

    所有工作都在 Sql Server 2014 中运行。

    参考:http://blogs.lessthandot.com/index.php/datamgmt/datadesign/calculating-mean-median-and-mode-with-sq/

    【讨论】:

      猜你喜欢
      • 2021-04-14
      • 1970-01-01
      • 2010-12-28
      • 1970-01-01
      • 2017-01-11
      • 2017-12-16
      • 2019-03-07
      • 2021-11-10
      • 2017-02-20
      相关资源
      最近更新 更多