【问题标题】:How to get top 150 results based on a column data如何根据列数据获得前 150 个结果
【发布时间】:2018-06-19 23:23:30
【问题描述】:

我有一个TEMP_TABLE 有以下数据

SOURCE - Number
DESTINATION- Number
COUNT- Number (Total count of transactions)
SUM- Number (Total sum of transactions)

样本数据:

SOURCE    DESTINATION COUNT SUM
123123123 99009900    65    1000000
123123123 88880303    12    90000
191113111 98980101    277   5000000
191113111 77778585    5     20000
191113111 56789547    740   75000000

我正在尝试根据COUNT 的值在source number with the destination number 上获得Top 150 results。如果 123123123 在表中有 200 行具有多个目标,我会尝试获取 123123123 的前 150 个结果并排除其他 50 行。

【问题讨论】:

    标签: sql oracle select oracle11g greatest-n-per-group


    【解决方案1】:

    您可以使用row_number 窗口函数为每一行(每个源编号)分配一个编号,然后只为每一行取前 150 个:
    (注意:sumcount 是 Oracle SQL 中的保留字。为了避免丑陋的转义,下面的示例将它们分别重命名为 sum_colcount_col

    SELECT source, destination, count_col, sum_col
    FROM   (SELECT source, destination, count_col, sum_col,
                   ROW_NUMBER() OVER (PARTITION BY source
                                      ORDER BY count_col DESC) AS rn
            FROM   temp_table) t
    WHERE  rn <= 150
    

    【讨论】:

      猜你喜欢
      • 2022-09-23
      • 1970-01-01
      • 1970-01-01
      • 2018-05-01
      • 2023-03-26
      • 1970-01-01
      • 1970-01-01
      • 2022-11-17
      • 1970-01-01
      相关资源
      最近更新 更多