簡體   English   中英

基於另一行中的值排除 SQL 查詢中的行,同時保留單個部件 ID 的多個輸出

[英]Excluding rows in a SQL query based on values in another row while preserving multiple outputs of a single part ID

我有以下形式的機器輸出數據:

DATETIME            ID       VALUE
8-28-20 20:55:10    part1    13
8-28-20 20:56:60    part1    20
8-28-20 20:57:22    part1    25
8-28-20 20:59:39    part2    9
8-28-20 21:10:55    part3    33
8-28-20 21:14:30    part1    14

我需要通過刪除一些行來生成一個新表:

DATETIME            ID       VALUE
8-28-20 20:57:22    part1    25
8-28-20 20:59:39    part2    9
8-28-20 21:10:55    part3    33
8-28-20 21:14:30    part1    14

機器有時會為每次運行收集多個 VALUE,但我只需要最后一個(它是累積的)。 但是,每個班次我可能會多次運行相同 ID,並且連續 2 次運行相同 ID 並非不可能。

只有當 VALUE 大於其上方行的 VALUE 時,SQL 是否可以過濾掉所有行的 ID 等於其上方行的 ID 的行?

這里發布了一些類似的問題,但它們都會導致對行進行分組並取最大值,但是我只會在每個時間段內為每個 ID 捕獲一次運行。

更通用一點,也作為一個例子來獲取沒有特定 OLAP 函數的會話 id:

WITH
-- your input
input(dttm,id,value) AS (
          SELECT TIMESTAMP '2020-08-28 20:55:10','part1',13
UNION ALL SELECT TIMESTAMP '2020-08-28 20:56:60','part1',20
UNION ALL SELECT TIMESTAMP '2020-08-28 20:57:22','part1',25
UNION ALL SELECT TIMESTAMP '2020-08-28 20:59:39','part2',9
UNION ALL SELECT TIMESTAMP '2020-08-28 21:10:55','part3',33
UNION ALL SELECT TIMESTAMP '2020-08-28 21:14:30','part1',14
)
,
-- add a counter that is at 1 whenever the id changes over time
with_chg AS (
  SELECT
    CASE 
      WHEN LAG(id) OVER(ORDER BY dttm) <> id THEN 1
      ELSE 0
    END AS chg_count
  , *
  FROM input
)
,
-- use the running sum of that change counter to get a session id
with_session AS (
  SELECT
    SUM(chg_count) OVER(ORDER BY dttm) AS session_id
  , dttm
  , id
  , value
  FROM with_chg
)
,
-- partition by the session id, order by datetime descending to get
-- the row number of 1 for the right row
with_rownum AS (
  SELECT
    ROW_NUMBER() OVER(PARTITION BY session_id ORDER BY dttm DESC) AS rownum
  , dttm
  , id
  , value
  FROM with_session
)
-- finally, filter by row number 1 and order back by datetime
SELECT
  dttm
, id
, value
FROM with_rownum
WHERE rownum = 1
ORDER BY 1
;
-- out         dttm         |  id   | value 
-- out ---------------------+-------+-------
-- out  2020-08-28 20:57:22 | part1 |    25
-- out  2020-08-28 20:59:39 | part2 |     9
-- out  2020-08-28 21:10:55 | part3 |    33
-- out  2020-08-28 21:14:30 | part1 |    14

您可以嘗試以下操作 - 使用row_number()

select * from
(
select *, 
       row_number() over(partition by dateadd(hour, datediff(hour, 0, DATETIME), 0), id order by DATETIME desc) as rn
from tablename
)A where rn=1

您似乎想要id更改且值增加的行:

select t.*
from (select t.*,
             lead(id) over (order by datetime) as next_id,
             lead(value) over (order by datetime) as next_value
      from t
     ) t
where next_id is null or next_id <> id or
      (next_id = id and next_value < value)

暫無
暫無

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

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