用于映射列的 SQL 查询

问题描述 投票:0回答:1

下面的查询给出了过去季度的分数、当前分数和当前季度的输出。

SELECT Greatest(1, Least(5, score))      AS past_score,
       Greatest(1, Least(5, curr_score)) AS current_score,
       current_quarter,
       past_quarter
FROM   (SELECT Round(( 0.4 * normalized_sick_leave ) +
                     ( 0.3 * normalized_total ) + (
                            0.3 *
                                    overall_rating ), 1) AS score,
               Round(( 0.4 * normalized_sick_leave_current ) + ( 0.3 *
                     normalized_total_curr )
                     + (
                     0.3 * overall_rating_curr ), 1)     AS curr_score,
               current_quarter,
               past_quarter
        FROM   (SELECT Nvl(abs_record.duration / 10, 0)                  AS
                               normalized_sick_leave,
                       time_entry.hours / 540                            AS
                       normalized_total,
                       5 - To_number(perf_rating.overall_rating)         AS
                       overall_rating,
                       abs_record_current.duration / 10                  AS
                               normalized_sick_leave_current,
                       time_entry_current.hours / 540                    AS
                               normalized_total_curr,
                       5 - To_number(perf_rating_current.overall_rating) AS
                       overall_rating_curr
                               ,
                       time_entry.quarter
                               past_quarter,
                       time_entry_current.quarter
                       CURRENT_QUARTER
                FROM   abs_record ABS_RECORD,
                       time_entry TIME_ENTRY,
                       perf_rating perf_rating,
                       abs_record_current ABS_RECORD_current,
                       time_entry_current TIME_ENTRY_current,
                       perf_rating_current perf_rating_current
                WHERE  abs_record.person_id(+) = time_entry.person_id
                       AND time_entry.person_id = perf_rating.person_id(+)
                       AND abs_record_current.person_id(+) =
                           time_entry_current.person_id
                       AND time_entry_current.person_id =
                           perf_rating_current.person_id(+)
                       AND time_entry_current.person_id (+) =
                           time_entry.person_id)) 

输出如下 -

过去_得分 当前_得分 当前_季度 过去_季度
1.2 4.8 Q1 Q4

我可以使用什么功能将其更改为

得分 四分之一
1.2 Q4
4.8 Q1
sql oracle-sqldeveloper
1个回答
0
投票

一个简单的选择是使用当前查询作为 CTE 并使用

UNION (ALL)
提取您感兴趣的数据,例如

WITH
   your_query
   AS
      -- your query begins here ...
      (SELECT GREATEST (1, LEAST (5, score)) AS past_score,
              GREATEST (1, LEAST (5, curr_score)) AS current_score,
              current_quarter,
              past_quarter
         FROM (SELECT ROUND(...)
               FROM ...
              )
      )      
      -- ... and ends here        
SELECT past_score AS score, past_quarter AS quarter FROM your_query
UNION ALL
SELECT current_score, current_quarter FROM your_query;
© www.soinside.com 2019 - 2024. All rights reserved.