궁금한 사항이 있어 질문드립니다.
먼저 예제 테이블을 설명드리겠습니다.
table A는 다음과 같은 column으로 구성되어있습니다.
select id, start_date, end_date, price from A
id | start_date | end_date | price
------------------------------------------------------------
AAA | 2017-03-15 | 2017-04-14 | 100000
BBB | 2017-03-16 | 2017-04-15 | 100000
CCC | 2017-03-01 | 2017-03-31 | 200000
DDD | 2017-03-30 | 2017-04-29 | 200000
이렇게 되었다고 가정했을때
end_date와 start_date 간의 날짜 차이를 이용해
일별로 정렬을 하고(date1) price를 날짜 차이로 나눠서 값을 구하려고 합니다.
결과값은
id | start_date | end_date | price | date1 | price1
-------------------------------------------------------------------------------------------
AAA | 2017-03-15 | 2017-04-14 | 100000 | 2017-03-15 | 3333
AAA | 2017-03-15 | 2017-04-14 | 100000 | 2017-03-16 | 3333
AAA | 2017-03-15 | 2017-04-14 | 100000 | 2017-03-17 | 3333
Comment 1
-
건우아빠
2018.12.04 20:54
3333이 아니라 3225 가 되어야 할듯... 2017-03-15 | 2017-04-14 이면 30 이아닌 31로 나워져야 맞지 않나요..;with resas(select 'AAA' id ,'2017-03-15' start_date,'2017-04-14' end_date, 100000 price union allselect 'BBB' id ,'2017-03-16' start_date,'2017-04-15' end_date, 100000 price union allselect 'CCC' id ,'2017-03-01' start_date,'2017-03-31' end_date, 200000 price union allselect 'DDD' id ,'2017-03-30' start_date,'2017-04-29' end_date, 200000 price)select a.id, a.start_date , a.end_date , a.price ,b.number+1 no , convert(varchar(10), dateadd(dd, b.number, a.start_date) ,120) date1 , a.price / (DATEDIFF(dd, a.start_date, a.end_date) + 1) price1from res across apply(select numberfrom master..spt_values where type = 'P' and number <= DATEDIFF(dd, a.start_date, a.end_date)) b