簡體   English   中英

Codility SqlEventsDelta(計算每種事件類型的最新值和第二最新值之間的差異)

[英]Codility SqlEventsDelta (Compute the difference between the latest and the second latest value for each event type)

最近,我正在 Codility 中練習代碼練習。 在這里你可以找到問題,它在練習 6 - SQL 部分。 剛開始測試看問題描述! SqlEventsDelta

問題定義:

我在 SQLite 中針對SqlEventDelta問題編寫了此解決方案。它在本地工具中工作正常但是,它在 web 工具中不起作用。

任何人都可以就如何解決這個問題提出任何建議嗎?

※ 我在 Stackoverflow 中搜索了這個問題,我知道比我自己的方法更好的代碼。 但是,如果可能的話,我想使用我自己的 SQLite 代碼邏輯和 function。

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 測試編輯器-SQLite 版本 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,返回最新(即時間上最近的)和第二個最新值之間的差異。

  • 該表應按 event_type 排序(升序)。
  • 行集中列的名稱無關緊要,但它們的順序很重要。

給定具有以下結構表 events

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

謝謝你。

我試圖使用某種天真的方法。 我知道由於許多子查詢,這對性能非常不利,但這里的問題是 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
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

大家好! 這是我的答案。 它在很大程度上是上述答案的一個糊塗組合,但它讀起來更簡單,並且針對上下文進行了評論。 作為新手,希望對其他新手有所幫助。

我在使用 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;

添加另一個涉及自我連接的答案 -

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

沒有嵌套查詢,得到 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

一個子查詢的解決方案

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;
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;

--在 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

我的解決方案:


--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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM