Oracle, где Case быстрее, чем среднее время бега в беге на 5 км на карнавале RM, который состоится 4 апреля 2018 года - PullRequest
0 голосов
/ 01 декабря 2019

Таким образом, в настоящее время мой запрос выглядит следующим образом и отображает всех бегунов, которые участвовали в забеге на 5 км на карнавале в РМ, который состоится 8 сентября.

select 
    concat(competitor.compfname,competitor.complname) as fullname ,
    entry.carndate,
    carnival.carnname,
    entry.eventno,
    event.eventypecode,
    eventtype.eventypedesc,
    round((entryfinishtime - entrystarttime) * 24 * 60, 2) as duration_mins

from competitor
    JOIN entry ON competitor.compno = entry.compno
    JOIN carnival ON entry.carndate = carnival.carndate
    JOIN event ON entry.eventno = event.eventno
    JOIN eventtype ON event.eventypecode = eventtype.eventypecode

where 
    event.eventypecode = '5K'
    AND entry.carndate = '08/SEP/2018'

Order by
    entry.carndate,
    fullname;

Что дает мне таблицу:

FULLNAME      CARNDATE  CARNNAME                        EVENTNO EVE EVENTYPEDESC DURATION_MINS
------------- --------- ------------------------------- ------- --- ------------ -------------
AnnamariaRose 08/SEP/18 RM Spring Series Caulfield 2018 6       5K  5 Km Run     35.23
FanShu        08/SEP/18 RM Spring Series Caulfield 2018 6       5K  5 Km Run     44.73
JaneRyan      08/SEP/18 RM Spring Series Caulfield 2018 6       5K  5 Km Run     18.23
LingShu       08/SEP/18 RM Spring Series Caulfield 2018 6       5K  5 Km Run     45.73
NanShu        08/SEP/18 RM Spring Series Caulfield 2018 6       5K  5 Km Run     42.73
Sam Ryan      08/SEP/18 RM Spring Series Caulfield 2018 6       5K  5 Km Run     26.23
SebastianCoe  08/SEP/18 RM Spring Series Caulfield 2018 6       5K  5 Km Run     30.23

Как добавить в оператор where, чтобы в таблице были показаны только те бегуны, которые участвовали в забеге на 5 км на карнавале в РМ 8 сентября 2018 года, который был быстрее, чемсреднее время бега бегунов на «5 км» на карнавале в РМ, состоявшемся 4 апреля 2018 года.

Ответы [ 2 ]

0 голосов
/ 01 декабря 2019

Вы можете использовать аналитическую функцию AVG для получения среднего времени, затем отфильтровать по времени> среднее:

WITH cteData AS (select concat(competitor.compfname,competitor.complname) as fullname ,
                        entry.carndate,
                        carnival.carnname,
                        entry.eventno,
                        event.eventypecode,
                        eventtype.eventypedesc,
                        round((entryfinishtime - entrystarttime) * 24 * 60, 2) as duration_mins
                   from competitor
                   JOIN entry ON competitor.compno = entry.compno
                   JOIN carnival ON entry.carndate = carnival.carndate
                   JOIN event ON entry.eventno = event.eventno
                   JOIN eventtype ON event.eventypecode = eventtype.eventypecode
                 where event.eventypecode = '5K'
                   AND entry.carndate = '08/SEP/2018'),
  cteWithAvg AS (SELECT d.*,
                        AVG(DURATION_MINS) OVER (PARTITION BY CARNNAME, CARNDATE, EVENTNO) AS AVERAGE_DURATION_MINS
               FROM cteData d)
SELECT *
  FROM cteWithAvg
  WHERE DURATION_MINS > AVERAGE_DURATION_MINS
Order by carndate,
         fullname

Я предполагаю, что поля используются в предложении PARTITION BY - отрегулироватьпо мере необходимости.

0 голосов
/ 01 декабря 2019

Для этого можно использовать коррелированный подзапрос:

with ce as (
      select concat(co.compfname, co.complname) as fullname ,
             en.carndate, c.carnname,
             en.eventno, e.eventypecode, et.eventypedesc,
             round((en.entryfinishtime - en.entrystarttime) * 24 * 60, 2) as duration_mins
      from competitor co join
           entry en 
           on co.compno = en.compno join
           carnival c
           on en.carndate = c.carndate join
           event e
           on en.eventno = e.eventno join
           eventtype et
           on e.eventypecode = et.eventypecode
      <your query here with no order by>
     )
select ce.*
from ce
where ce.eventypecode = '5K' and
      ce.carndate = '08/SEP/2018' and
      ce.duration_mins > (select avg(ce2.duration_mins)
                          from ce ce2
                          where ce.eventypecode = '5K' and
                                ce.carndate = '04/APR/2018'
                         );
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...