계약기간 밖의 비용들을 계약기간 첫달, or 마지막달에 넣어서 월별 금액이 나오게 쿼리를 하고 싶은데요.
계약테이블 | ||||
계약기간 | ||||
2015-05-01 | 2016-04-30 | |||
비용테이블 | 결과 | |||
2015-04 | 1500000 | 2015-05 | 2000000 | |
2015-05 | 500000 | 2015-06 | 500000 | |
2015-06 | 500000 | 2015-07 | 1500000 | |
2015-07 | 1500000 | 2015-08 | 500000 | |
2015-08 | 500000 | 2015-09 | 500000 | |
2015-09 | 500000 | 2015-10 | 500000 | |
2015-10 | 500000 | 2015-11 | 500000 | |
2015-11 | 500000 | 2015-12 | 500000 | |
2015-12 | 500000 | 2016-01 | 500000 | |
2016-01 | 500000 | 2016-02 | 500000 | |
2016-02 | 500000 | 2016-03 | 500000 | |
2016-03 | 500000 | 2016-04 | 6500000 | |
2016-04 | 500000 | |||
2016-05 | 2500000 | |||
2016-06 | 3500000 |
어떻게 해야 할까요?
이런 데이터가 계약건 별로 쭉 있어서... 특정월의 총 비용등을 뽑으려고 합니다.
group by 를 할때... 계약기간 이전 비용은 계약 첫달... 계약기간 이후 비용은 계약 마지막 달에.....
비용을 select 를 할때... 비용 테이블의 월을 계약기간 이후의 것은 계약 달로 select 해서 뽑아서 group by를 하거나...
어떻게 하면 될꺼 같긴 한데.. 잘 안되네요..
Select cType, B.so_code, sum(convert(money, money)) as cost, ma_date = ( CASE When sContractDate > ma_date then left(sContractDate,7)
WHEN eContractDate < ma_date then left(eContractDate,7)
Else ma_date
end )
from
( Select cType, so_code, ma_p_code, sContractDate, eContractDate from view_so_detail where use_yn = 1 ) A inner join
( Select so_code, ma_p_code, ma_date, money from MaMcDetail where use_yn = 1 ) B
on A.so_code = B.so_code and A.ma_p_code = B.ma_p_code
group by cType, B.so_code,
CASE When sContractDate > ma_date then left(sContractDate,7)
WHEN eContractDate < ma_date then left(eContractDate,7)
Else ma_date
end
이렇게 처리하긴 했는데... 이렇게 하면 될지... 확신이 안서서...
Comment 2
-
Terry
2016.02.11 10:34
-
Terry
2016.02.11 10:58
본문글에 작성해두신 쿼리 토대로 조금 수정해봤습니다..
그럼 수고하세요~
Declare @ls_from_ymd char(8) = '2015-05-01'
Declare @ls_to_ymd char(8) = '2016-04-30';with tblA(cType,so_code,ma_p_code,sContractDate,eContractDate,ma_date,money) As
(
Select a.cType
,a.so_code
,a.ma_p_code
,a.sContractDate
,a.eContractDate
,b.ma_date
,b.money
From view_so_detail a
Inner Join
MaMcDetail b
On a.so_code = b.so_code
And a.ma_p_code = b.ma_p_code
And b.use_yn = 1
Where a.use_yn = 1
)
Select a.cType
,a.so_code
,Sum(Convert(money,a.money)) + IsNull(Max(Convert(money,b.money)),0) + IsNull(Max(Convert(money,c.money)),0) As cost
,Left(a.ma_date,7) As ma_date
From tblA a
Left Outer Join
(
Select a.cType
,a.so_code
,Sum(Convert(money,a.money)) As money
,@ls_from_ymd As ma_date
From tblA a
Where a.ma_date < @ls_from_ymd
) b
On a.cType = b.cType
And a.so_coe = b.so_code
And a.ma_date = b.ma_date
Left Outer Join
(
Select a.cType
,a.so_code
,Sum(Convert(money,a.money)) As money
,@ls_to_ymd As ma_date
From tblA a
Where a.ma_date > @ls_to_ymd
) b
On a.cType = c.cType
And a.so_coe = c.so_code
And a.ma_date = c.ma_date
Where a.ma_date Between @ls_from_ymd And @ls_to_ymd
Group By a.cType
,a.so_code
,Left(a.ma_date,7)
Case 로 할 필요없이
Inline View 로 Join 하는게 효율적으로 보입니다.
1. 원데이터 Select
2. 계약기간 이전데이터 Inline View 로 가져옴
3. 계약기간 이후데이터 Inline View 로 가져옴
1의 데이터에 2,3 의 데이터를 Join
하기 쿼리 참고하세요..
---쿼리시작---
Declare @ls_from_ymd char(8) = '2015-05-01'
Declare @ls_to_ymd char(8) = '2016-04-30'
Declare @ls_from_yymm char(7) = Left(@ls_from_ymd,7)
Declare @ls_to_yymm char(7) = Left(@ls_to_ymd ,7)
;with tblA (month,amt) As
(
Select '2015-04', 1500000 Union All
Select '2015-05', 500000 Union All
Select '2015-06', 500000 Union All
Select '2015-07', 1500000 Union All
Select '2015-08', 500000 Union All
Select '2015-09', 500000 Union All
Select '2015-10', 500000 Union All
Select '2015-11', 500000 Union All
Select '2015-12', 500000 Union All
Select '2016-01', 500000 Union All
Select '2016-02', 500000 Union All
Select '2016-03', 500000 Union All
Select '2016-04', 500000 Union All
Select '2016-05', 2500000 Union All
Select '2016-06', 3500000
)
Select a.month
,SUM(a.amt) + ISNULL(Max(b.amt),0) + ISNULL(Max(c.amt),0)
From tblA a
--계약기간 이전 데이터
Left Outer Join
(
Select @ls_from_yymm As month
,SUM(a.amt) As amt
From tblA a
Where a.month < @ls_from_yymm
) b
On a.month = b.month
--계약기간 이후 데이터
Left Outer Join
(
Select @ls_to_yymm As month
,SUM(a.amt) As amt
From tblA a
Where a.month > @ls_to_yymm
) c
On a.month = c.month
Where a.month Between @ls_from_yymm And @ls_to_yymm
Group By a.month
---쿼리끝---