2014-09-21 2 views
0

다음 문제를 해결하는 데 도움을주십시오. ProductID, price, startWeek, endWeek, numWeeks 등 5 개 필드가있는 table1이 있습니다. 그런 식으로,MS Access 2010 : 하나의 가격 레코드를 기반으로 여러 주 레코드를 만듭니다

xx ProdID xx price xx startWeek xx endWeek xx NumWeeks 
    Prod1  $15  1   3   3 
    Prod2  $20  2   4   3 

나는 주 단위로 제품 ID에 의해 가격을 기록하는 또 다른 표 2를 작성해야, 각각의 제품은 각 주 레코드 수있을 것입니다 : 사전에

Prod1 week1 $15, 
Prod1 week2 $15 
Prod1 week3 $15 
Prod2 week2 $20 
Prod2 week3 $20 
Prod3 week4 $20 

많은 감사를

+0

에 오신 것을 환영합니다. 스택 오버플로는 코드 작성 서비스가 아니기 때문에이 질문은 주제와는 다른 것으로 보입니다. *** 귀하의 *** 코드와 관련된 문제를 자세하게 설명하기 위해 질문을 편집하고 [* minimal * example] (http://stackoverflow.com/help/mcve)을 포함하십시오. –

답변

0

TEST SQLFiddle

첫째, 주 동안 테이블을 생성합니다. 일반적으로 집계 테이블 또는 숫자 테이블로 알려져 있습니다.

당신은 VBA를 통해 만들 수 있습니다,하지만 난 (단순히 긴하지만) 단순히 유지에 wanto

이 코드를 한 번 실행해야합니다.

create table weeks(weekNumber int, weekName varchar(10)) 
insert into weeks 
select 1, 'week1' 
union all 
select 2, 'week2' 
union all 
select 3, 'week3' 
union all 
select 4, 'week4' 
union all 
select 5, 'week5' 
union all 
select 6, 'week6' 
union all 
select 7, 'week7' 
union all 
select 8, 'week8' 
union all 
select 9, 'week9' 
union all 
select 10, 'week10' 
union all 
select 11, 'week11' 
union all 
select 12, 'week12' 
union all 
select 13, 'week13' 
union all 
select 14, 'week14' 
union all 
select 15, 'week15' 
union all 
select 16, 'week16' 
union all 
select 17, 'week17' 
union all 
select 18, 'week18' 
union all 
select 19, 'week19' 
union all 
select 20, 'week20' 
union all 
select 21, 'week21' 
union all 
select 22, 'week22' 
union all 
select 23, 'week23' 
union all 
select 24, 'week24' 
union all 
select 25, 'week25' 
union all 
select 26, 'week26' 
union all 
select 27, 'week27' 
union all 
select 28, 'week28' 
union all 
select 29, 'week29' 
union all 
select 30, 'week30' 
union all 
select 31, 'week31' 
union all 
select 32, 'week32' 
union all 
select 33, 'week33' 
union all 
select 34, 'week34' 
union all 
select 35, 'week35' 
union all 
select 36, 'week36' 
union all 
select 37, 'week37' 
union all 
select 38, 'week38' 
union all 
select 39, 'week39' 
union all 
select 40, 'week40' 
union all 
select 41, 'week41' 
union all 
select 42, 'week42' 
union all 
select 43, 'week43' 
union all 
select 44, 'week44' 
union all 
select 45, 'week45' 
union all 
select 46, 'week46' 
union all 
select 47, 'week47' 
union all 
select 48, 'week48' 
union all 
select 49, 'week49' 
union all 
select 50, 'week50' 
union all 
select 51, 'week51' 
union all 
select 52, 'week52' 
union all 
select 53, 'week53' 
union all 
select 54, 'week54' 
union all 
select 55, 'week55' 

그럼 해결책은 간단하다 :

1 만들받을 수 있나요 실행 작업

insert into table2 
    select a.ProductID, w.weekName, a.price 
    from table1 a inner join weeks w 
    on a.startWeek<=w.weekNumber and a.endWeek>=w.weekNumber 

을 한 시간

create table table2(ProductID varchar(10), weekName varchar(10), price int) 

을 표 2의 결과

select * from table2 
,536,913,632 10 개

결과 : 스택 오버플로

ProductID weekName price 
Prod1  week1  15 
Prod1  week2  15 
Prod1  week3  15 
Prod2  week2  20 
Prod2  week3  20 
Prod2  week4  20 

TEST SQLFiddle

+0

액세스 권한이 없으므로 튜닝이 필요하면 알려주십시오 http://www.sqlfiddle.com/#!3/b32b6/1 – Horaciux

+0

@zosha이 솔루션을 사용해 보셨습니까? – Horaciux

관련 문제