2013-11-26 1 views
2

다음 2 세트의 데이터가 있으며 하나의 전체 세트 (하나의 열 합계)로 결합하려고합니다. 이 예에서는 1 개의 항목 만 표시됩니다.동일한 키를 사용하여 2 세트의 데이터를 1 세트로 그룹화하십시오.

(마스터 테이블)

1 세트 :

start_date |item 
2013-10-01 | 1 
2013-10-15 | 1 
2013-10-25 | 1 

2 세트 (상세 테이블) 마스터 테이블과 함께 상세 테이블의 데이터에 따라

working_date | item_no | qty 
2013-10-03 | 1  | 2 
2013-10-05 | 1  | 6 
2013-10-18 | 1  | 3 
2013-10-23 | 1  | 4 
2013-10-27 | 1  | 6 
2013-10-28 | 1  | 3 

이어서, 난 그룹화 할

최종 테이블 (결과) :

다음과 같이 시작 날짜와 합산 수량에

어려운 점은 qty를 합산하기 전에 working_date를 starting_date 범위와 일치 시켜서 수량을 합하는 방법을 알지 못한다는 것입니다.

즉, 2013-10-01부터 2013-10-14, 2013-10-15에서 2013-10-24 및 2013-10-25에서 2013-10-25까지 31

+0

는 내부되지는 수량에 의해, item_no에 의해 합에 가입하고 그룹인가요? –

+0

@ AllanS.Hansen. 분명히 아니다. 그는 세부 사항의 날짜를 일정 범위와 일치시켜야합니다. –

+0

질문에 완전히 답할 수 없습니다. "Qty를 합산하기 전에 working_date를 starting_date 범위로 구별/구분할 수 있습니까?"라는 질문에 대해 자세히 설명해 주시겠습니까? – DarkKnight

답변

0

세부 테이블의 각 레코드에 대해 가장 가까운 이전 날짜를 마스터 테이블에서 찾아야합니다. 일단 찾으면 그 날짜에 집계하십시오.

select starting_date, sum(qty) 
from (
    select working_date, 
     item_no, 
     qty, 
     (select max(master_table.starting_date) 
      from master_table 
      where master_table.starting_date <= details_table.working_date 
      and master_table.item_no = details_table.item_no 
     ) as starting_date 
from details_table 
) as modfied_table 
group by starting_date 
order by starting_date; 

Demo.

0

귀하의 질문이 완전히 명확하지는 않지만, 귀하가 함께 할 수있는 충분한 예제 여야합니다.

SELECT master_table.starting_date, 
    details_table.item_no, 
    SUM(details_table.qty) 
FROM master_table 
JOIN details_table 
    ON master_table.item_no = details_table.item_no 
GROUP BY master_table.starting_date, 
     details_table.item_no 
0
declare @startDate Datetime 
set @startDate=DATEADD(month,-2,GETDATE()) -- set the startdate 2 months before today 
declare @endDate Datetime 
set @endDate=GETDATE() -- set the ending date equal to current datetime 
declare @DaysStep int 
set @DaysStep=10 -- set the step from day to the next day equal to 10 days. 
       -- so if today is 12/01/2013 the next day will be 12/11/2013 

declare @results table (starting_date Datetime, item_no int, total_qty int) 

while @startDate<@endDate 
    begin 
     insert into @results 
     select starting_date, item_no, sum(qty) as total_qty 
     from master as m 
     inner join detail as d 
     on m.item_no=d.item_no 
     where startingDate between @starDate and DATEADD(Days, @DaysStep, @startDate) 
    @startDate=DATEADD(Days, @DaysStep, @startDate) 
    end 

select * 
from @results 

난 당신이 MSSQL을 usel 있다고 가정합니다. 그렇지 않으면 위의 코드를 SQL Dialect로 변경해야합니다. 또한 @startDate, @endDate 및 @DaysStep을 원하는대로 설정할 수 있습니다.

+0

죄송합니다, 잊지 말고 가입 할 수 있습니다. PL/SQL, oracle DB를 사용 중입니다. – calvinyankf

+0

@calvinyankf 전혀 문제가 없습니다! 불행히도, 나는 오라클 DB를 사용한 적이 없습니다. 그래서 나는 그것의 SQL 방언을 모른다. 그러나 위의 TSQL 코드를 PL/SQL 언어로 변환하는 것이 어려울 것이라고 생각하지 않습니다. 모호한 점이 있으면 알려 주시고, 설명을하고 오류없이 변환 과정을 진행하십시오. – Christos

0

이 당신을 위해 작동합니다

SELECT start_date, item_no, sum(qty) as qty 
FROM #mastertable as aa INNER JOIN #detailtable as bb ON aa.item = bb.item_no 
WHERE (start_date <= working_date 
    AND working_date < (SELECT TOP 1 start_date FROM #mastertable as innerT WHERE innerT.start_date > aa.start_date)) 
    OR (working_date >= (SELECT TOP 1 start_date FROM #mastertable as innerT ORDER BY innerT.start_date DESC) 
    AND start_date = (SELECT TOP 1 start_date FROM #mastertable as innerT ORDER BY innerT.start_date DESC)) 
GROUP BY start_date, item_no 
ORDER BY start_date 
관련 문제