반응형
다른 행을 기준으로 행 필터링
보류 중인 거래와 완료된 거래 기록이 포함된 테이블을 가지고 있습니다.표의 각 레코드는 트랜잭션 라이프사이클의 한 단계를 나타냅니다.
e.x.: 트랜잭션이 성공하면 테이블에 타임스탬프가 동일한 두 개의 레코드가 있습니다.
- id: 1, timestamp: 111111111, type: pending, created_at: 2020-10-10
- id: 2, timestamp: 111111111, type: complete, created_at: 2020-14-10
모든 트랜잭션을 선택하고 다음 방법으로 필터링합니다.
완료된 모든 트랜잭션이 표시되어야 합니다. 일치하는 완료 타임스탬프가 있는 보류 중은 필터링되어야 합니다. 일치하는 완료 타임스탬프가 없는 보류 중은 7일 이상 경과한 모든 보류 중도 필터링되어야 합니다.
원하는 출력은 다음과 같아야 합니다.
표의 모든 행:
- id: 1, timestamp: 111111111, type: pending, created_at: 2020-10-10
- id: 2, timestamp: 111111111, type: complete, created_at: 2020-14-10
- id: 3, timestamp: 222222222, type: pending, created_at: 1999-01-01
출력:
- id: 2, timestamp: 111111111, type: complete, created_at: 2020-14-10
음... 내가 정확히 이해했다면, 당신은 이것들을 로 변환할 수 있습니다.where조건:
select t.*
from transactions t
where t.type = 'complete' or
(t.type = 'pending' and
not exists (select 1
from transactions t2
where t2.timestamp = t.timestamp and
t2.type = 'complete'
) and
t.timestamp >= now() - interval 7 day
);
다음과 같이 창 기능을 사용할 수 있습니다.
Select * from
(Select t.*,
Max(case when type = 'complete' created_at end) over (partition by timestamp) as mx_c
From t) t
Where type = 'completed' or (type = 'pending' and (created_at <= mx_c - interval 7 day or mx_c is null)
언급URL : https://stackoverflow.com/questions/65350546/filtering-out-rows-based-on-other-rows
반응형
'programing' 카테고리의 다른 글
| Oracle이 전날 기록 가져오기 (0) | 2023.07.13 |
|---|---|
| git 저장소에서 병합한 후 .orig 파일을 삭제하는 방법은 무엇입니까? (0) | 2023.07.13 |
| VS 코드 작업 공간에서 기호로 이동 - 외부 파일 제외 (0) | 2023.07.13 |
| 파이썬에서 HTML을 탈출하는 가장 쉬운 방법은 무엇입니까? (0) | 2023.07.13 |
| Windows MongoDB - 나침반이 설치되었지만 시스템에서 나침반을 찾을 수 없습니다. (0) | 2023.07.13 |