PostgreSQL:有没有一种在Case语句中编写SQL查询的方法/更简便的方法?

时间:2018-10-19 22:35:32

标签: sql postgresql nested chained-select

问题

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

我想要做的事情的结构是这样的

--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)执行的查询进行“大小写” 。

  • 其他度量标准是:Formula1 = ax + b-c; Formula2 = c / x + 1等;
  • P.S:我已在代码中标记*** ***以表示为什么需要 不同的查询

    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

预期的输出:将根据聚合类型显示 top N条记录,这些记录按VideoId分组,并按照例子。提供的参数:记录数(整数),聚合类型(字符串)

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 

0 个答案:

没有答案