네 개의 열이있는 테이블이 있습니다.
--------------------------------------
| ID | DESCRIPTION | PAY | TIMESTAMP |
--------------------------------------
| 1 | ABA123 | 100 | 02-10-2019|
--------------------------------------
| 2 | ABA123 | 200 | 02-06-2019|
--------------------------------------
| 3 | ABA123 | 300 | 01-11-2019|
내 요구 사항에서 1 일 급여, 5 일 급여 및 1 개월 급여를 가져와야하며 오늘이 2019 년 3 월 11 일이라고 가정합니다. 다음과 같이 세 가지 쿼리를 실행하여 데이터를 가져올 수 있습니다.
select * from salary where timestamp <= sysdate -1 ; // this will return row with one day old salary
select * from salary where timestamp
<= sysdate -1 and timestamp >= sysdate -4 // this one return all records between 1 to 4 days old and I can have similar query for one month old data
결과를 다음 형식으로 표시하는 하나의 쿼리를 실행할 수있는 방법이 있습니까?
| DESCRIPTION | 1DAYPAY | 4DAYPAY | 1MONTHPAY |
----------------------------------------------------
| ABA123 | 100 | 200 | 300 |
이전 데이터에 대해 sum()
함수를 사용 add_months
하십시오 1 month
.
select description
, sum(case when timestamp between sysdate and sysdate - 1 then Pay else 0 end)
, sum(case when timestamp between sysdate and sysdate - 4 then Pay else 0 end)
, sum(case when timestamp between sysdate and add_months(sysdate,-1) then Pay else 0 end)
from salary
group by description
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다