0

问题

  • 当前的实现只有一个指标(avg),它基于它显示结果。
  • 所需的实施也需要适应其他指标。但是,我不能只将它们放在 CASE 语句中,因为查询需要以不同的方式进行排序。
  • 所以,我有两个选择:
    1. 为每个指标编写不同的查询(尽管只有下面结构的 Query2 包含唯一的变化)
    2. 将每个指标的查询写为 Query2 中的案例语句 - 我将使用这个,因为我认为这更易于维护。

我正在尝试做的结构是这样的

--Query1 // that returns some fields based on a timerange(ex : Year1 - Year2 )
--Query2 // I need to manipulate this query to output records based on an input 
         // metric
--Query3 // That joins output from Query1 and Query2

**Existing query:**
WITH const as (
    select  
        /* Constants */
        'Formula' as *costfunction* -- Formula is a string which can take 
                                    -- the below formulae mentioned above  
                                    -- (Formula1 / Formula2/ etc)
    ),
    stats_analysis (
      /* fields to return */ 
    )AS(
      /* Main select query for Query1 */
    ),

--Query2 // Basically extracts out top 5 students based on average

top_students
(
stud_id,
metric_value,
metric_name
) AS (
SELECT
stud_id
, /*Calculation for metric*/AS metric_value
, 'marks' AS metric_name
FROM stats_analysis
GROUP BY stud_id
ORDER BY metric_value DESC
limit 5
)

--Query 3
Uses Query1 and Query2 to display final result

尝试的实现:基本上我正在尝试根据不同的指标(costFunction)来区分需要执行的查询。

  • 其他指标是:公式1 = ax+bc;公式 2 = c/x+1 等;
  • PS:我在代码中标记了 *** *** 表示为什么我需要不同的查询

    top_students
    (
       stud_id,
       metric_value,
       metric_name
    )
    AS (
        SELECT CASE const.costFunction
           When 'Formula1' THEN
           (
               stud_id,
               metric_value,
               metric_name
           ) AS (
           SELECT
            stud_id
            , /***Calculation for Formula1***/ AS metric_value
            , 'marks' AS metric_name
            FROM stats_analysis CROSS JOIN const
            GROUP BY stud_id
            ORDER BY metric_value ***DESC***
            limit 5
           )
           When 'Formula2' THEN
           (
               stud_id,
               metric_value,
               metric_name
           ) AS (
           SELECT
            stud_id
            , /***Calculation for Formula2***/ AS metric_value
            , 'marks' AS metric_name
            FROM stats_analysis CROSS JOIN const
            GROUP BY stud_id
            ORDER BY metric_value ***ASC***
            limit 5
           )
           When 'Formula3' THEN
           (
               stud_id,
               metric_value,
               metric_name
           ) AS (
           SELECT
            stud_id
            , /***Calculation for Formula3***/ AS metric_value
            , 'marks' AS metric_name
            FROM stats_analysis CROSS JOIN const
            GROUP BY stud_id
            ORDER BY metric_value ***DESC***
            limit 5
           )
    )
    

这会在 CASE 中引发 AS 语法错误。我是 PG 的新手,所以我也愿意接受任何更好的方法来构建这个查询。谢谢 !

编辑 :

样本数据

VideoID | StartTime         | EndTime            |Views|TotalTime  |MinTime        |    MaxTime
17276   |2018-09-26 20:33:43| 2018-09-26 20:48:43|  90 |554.2757137|    1.104655658|    25.59161658
17276   |2018-09-26 20:48:43| 2018-09-26 21:03:43|  418|3160.102025|    0.973088008|    167.0388009
17276   |2018-09-26 21:18:44| 2018-09-26 21:33:44|  14 |112.5031557|    0.997863734|    29.2182703
29083   |2018-09-26 20:48:43| 2018-09-26 21:03:43|  419|3552.922446| 0.964971822   |    152.9819936
29083   |2018-09-26 20:33:43| 2018-09-26 20:48:43|  90 |541.1001533|    1.316958002|    27.36436251
29083   |2018-09-26 21:33:44| 2018-09-26 21:48:44|  314|758.0945074|    0.013669366|    1.663391002
29083   |2018-09-26 21:33:44| 2018-09-26 21:48:44|  450|3029.140526|    0.969670667|    139.6291586

预期输出:将根据聚合类型显示N 条记录,按 VideoId 分组并按示例中所示的顺序排序。提供的参数:记录数(int),聚合类型(字符串)

Ex1 : Input = (2,avg) 

VideoId   | MetricValue
17276       7.33 // Calculated by Sum(Total Time)/Sum(Views)
29083       6.19
Explanation : top 2 by average would mean top 2 with highest avg. i.e:DESC 
Ex2 : Input = (1,max)

VideoId   | MetricValue
29083       1.31    // Calculated by Max(MaxTime) after grouping by ID
Explanation : top 1 by max would mean top 1 with highest MaxTime. i.e:DESC 

Ex3 : Input = (1,min)

VideoId   | MetricValue
29083       0.013669366 // Calculated by Min(MinTime) after grouping by ID
Explanation : top 1 by min would mean top 1 with lowest MinTime. i.e:ASC 
4

0 回答 0