Codility SqlEventsDelta(计算每个事件类型的最新值和第二个最新值之间的差异)

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

最近在Codility练习代码练习。 在这里你可以找到问题,它在练习 6 - SQL 部分。 刚开始测试看问题描述! SqlEventsDelta

问题定义:

我为 SQLite 中的 SqlEventDelta 问题写了这个解决方案。它在本地工具中工作正常但是,它在网络工具中不起作用。

任何人都可以就如何解决这个问题提出任何建议吗?

※ 我在 Stackoverflow 中搜索了这个问题,我知道比我自己的方法更好的代码。 但是,如果可能的话,我想使用我自己的 SQLite 代码逻辑和函数。

WITH cte1 AS
(
    SELECT *, CASE WHEN e2.event_type = e2.prev THEN 0 
                 WHEN e2.event_type = e2.next THEN 0 
                 ELSE 1 END AS grp
    FROM (SELECT *, LAG(e1.event_type) OVER(ORDER BY (SELECT 1)) AS prev , LEAD(e1.event_type) OVER(ORDER BY (SELECT 1)) AS next FROM events e1) e2
)
,cte2 AS 
(
    SELECT cte1.event_type, cte1.time, cte1.grp, cte1.value - LAG(cte1.value) OVER(ORDER BY cte1.event_type, cte1.time) AS value 
    FROM cte1 
    WHERE cte1.grp = 0 
    ORDER BY cte1.event_type, cte1.time
)

SELECT c2.event_type, c2.value 
FROM cte2 c2
WHERE (c2.event_type, c2.time) IN (
    SELECT c2.event_type, MAX(c2.time) AS time 
    FROM cte2 c2 
    GROUP BY c2.event_type)
GROUP BY c2.event_type
ORDER BY c2.event_type, c2.time

它在我的本地工具(DB Browser for SQLite Version 3.12.2)上运行得很好没有错误。

event_type | value
-----------+-----------
2          | -5
3          | 4

Execution finished without errors.
Result: 2 rows returned in 7ms

但是,在web工具(Codility test editor-SQLite Version 3.11.0)上无法运行,我得到以下错误。

| Compilation successful.

| Example test:   (example test)
| Output (stderr):
| error on query: ...
| ...
| ...,
| details: near "(": syntax error
| RUNTIME ERROR (tested program terminated with exit code 1)

Detected some errors.


SqlEventDelta 问题:

编写一个 SQL 查询,对于每个已注册多次的 event_type,返回最新(即时间上最近的)和第二个最新值之间的差异。

  • 表格应按事件类型(升序)排序。
  • 行集中列的名称无关紧要,但它们的顺序很重要。

给定一个具有以下结构的表事件

create table events (
       event_type integer not null,
       value integer not null,
       time timestamp not null,
       unique(event_type, time)
   );

例如,给定以下数据:

event_type | value      | time
-----------+------------+--------------------
2          | 5          | 2015-05-09 12:42:00
4          | -42        | 2015-05-09 13:19:57
2          | 2          | 2015-05-09 14:48:30
2          | 7          | 2015-05-09 12:54:39
3          | 16         | 2015-05-09 13:19:57
3          | 20         | 2015-05-09 15:01:09

鉴于上述数据,output 应返回以下行集:

event_type | value
-----------+-----------
2          | -5
3          | 4

谢谢。

sql sqlite lag lead over-clause
10个回答
5
投票

我尝试使用一种有点幼稚的方法。我知道由于有很多子查询,这对性能非常不利,但这里的问题是 PostgreSQL 的“DISTINCT ON”,但是我得到了 100% 😃

希望你喜欢!

select distinct on (event_type) event_type, result * -1
from (select event_type, value, lead(value) over (order by event_type) - value result
      from (select *
            from events
            where event_type in (select event_type
                                 from events
                                 group by event_type
                                 having count(event_type) >= 2)
            order by event_type, time desc) a) b

2
投票
with data as (SELECT a.event_type, a.value, a.time,
 --Produce a virtual table that stores the next and previous values for each event_type.
LEAD(a.value,1) over (PARTITION by a.event_type ORDER by 'event_type', 'time' DESC) as recent_val,
LAG(a.value,1) over (PARTITION by a.event_type ORDER by 'event_type', 'time' DESC) as penult_val
   
    from events a
    
    JOIN (SELECT event_type 
            from events --Filter the initial dataset for duplicates. Store in correct order
                group by event_type HAVING COUNT(*) > 1 
                    ORDER by event_type) b
        
        on a.event_type = b.event_type) --Compare the virtual table to the filtered dataset

SELECT event_type, ("value"-"penult_val") as diff --Perform the desired arithematic
    from data 
    where recent_val is NULL --Filter for the most recent value

嗨团队!这是我的答案。它主要是上面答案的糊涂集团,但它读起来更简单,并且针对上下文进行了评论。作为新手,希望对其他新手有所帮助


1
投票
with deltas as (
  select distinct event_type, 
     first_value(value) over (PARTITION by event_type ORDER by time DESC) - 
     nth_value(value, 2) over (PARTITION by event_type ORDER by time DESC) as delta
    from events
)
select * from deltas where delta is not null order by 1;

0
投票

我在使用 sqlite 时确实遇到了同样的问题。 尝试将以下代码与 PostgreSQL 一起使用

with data as (select 
e.event_type,
e.value,
e.time,
lead(e.value,1) over (PARTITION by e.event_type order by e.event_type,e.time asc) as next_val,
lag (e.value,1) over (PARTITION by e.event_type order by e.event_type,e.time asc) as prev_val
from events e)
select distinct d.event_type, (d.value-d.prev_val) as diff
from 
events e,data d
where e.event_type = d.event_type
and d.next_val is null
and e.event_type in ( SELECT event_type
                        from data 
                        group by 
                        event_type
                        having count(1) > 1)
order by 1;

0
投票

添加另一个涉及自连接的答案 -

PostgreSQL

-- write your code in PostgreSQL 9.4

WITH TotalRowCount AS (
    SELECT
        event_type,
        COUNT(*) as row_count
    FROM events
    GROUP BY 1
),

RankedEventType AS (
    SELECT
        event_type,
        value,
        ROW_NUMBER() OVER(PARTITION BY event_type ORDER BY time) as row_num
    FROM events
)


SELECT
    a.event_type,
    a.value - b.value as value
FROM RankedEventType a
INNER JOIN TotalRowCount c
    ON a.event_type = c.event_type
INNER JOIN RankedEventType b
    ON a.event_type = b.event_type
WHERE 1 = 1
AND a.row_num = c.row_count
AND b.row_num = c.row_count - 1
ORDER BY 1

0
投票

没有嵌套查询,得到 100%

with data as (
with count as (select event_type
                                 from events
                                 group by event_type
                                 having count(event_type) >= 2)
select e.event_type , e.value, e.time from events as e inner join count as r on e.event_type=r.event_type  order by e.event_type, e.time desc                               
)
select distinct on (event_type) event_type,
           value - (LEAD(value) over (order by event_type))  result from data

0
投票

一个子查询的解决方案

WITH diff AS
  (SELECT event_type,
          value,
          LEAD(value) OVER (PARTITION BY event_type
                            ORDER BY TIME DESC) AS prev
   FROM EVENTS
   GROUP BY event_type,
            value,
            time
)

SELECT DISTINCT ON (event_type) event_type,
                   value - prev
FROM diff
WHERE prev IS NOT NULL;

0
投票

--在 PostgreSQL 9.4 中

with ct1 as (SELECT 
    event_type,
    value,
    time,
    rank() over (partition by event_type order by time desc) as rank
from events),
ct2 as (
select event_type, value, rank, lag (value,1) over (order by event_type) as previous_value
from ct1
order by event_type)
select event_type, previous_value - value from ct2
where rank = 2
order by event_type

0
投票

我的解决方案:


--Get table with rank 1, 2 group by event_type
with t2 as(
select event_type, value, rank from (
    select event_type, value,
        rank() over(
        partition by event_type
        order by time desc) as rank,
        count(*) over (partition by event_type) as count
    from events) as t
where t.rank <= 2 and t.count > 1
)

--Calculate diff using Lead() and filter out null diff with max
select t3.event_type, max(t3.diff) from (
    select event_type, 
    value - lead(value, 1) over (
        partition by event_type
        order by rank) as diff
    from t2) as t3
group by t3.event_type

0
投票

--1) “极简”老式解决方案(不使用窗口函数) WITH maxevents AS(从事件中选择 event_type 作为 Et1,max(time) 作为 maxtime 按计数 (1)>1 的 event_type 分组, 防止 AS(选择 event_type 作为 Et2,max(time) 作为 nextime 来自 events JOIN maxevents ON event_type=Et1 AND time

SELECT event_type, v1-v2 作为值 FROM maxeventvalues JOIN preveventvalues ON event_type=et 按 event_type 升序排列 ; --2) 使用窗口函数的现代解决方案 WITH maxevents AS(选择 event_type 作为 Et1,max(time) 作为 maxtime 来自按 event_type 计数 (1)>1) 的事件组 ,AllValDifs as (SELECT event_type, value- LAG(value,1,0) OVER (PARTITION BY event_type ORDER BY time ASC) as ValDif , 时间 FROM events JOIN maxevents ON event_type=et1) 选择不同的事件类型,第一个值(ValDif)
OVER (PARTITION BY event_type ORDER BY time DESC) 来自 AllValDifs 按事件类型 ASC 排序;

© www.soinside.com 2019 - 2024. All rights reserved.