Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

DB2 Sql help needed

Status
Not open for further replies.

malini0905

Programmer
Aug 8, 2006
6
US
I have data which has 13 periods per year and ineach period there will be 4 weeks.Reports are generated for each week
with some amount fields corresponding to each week.
we also have week numbers which are integers from (1,2--52)
This amount field is for 1st week in each period is fine,for next week we need to subtract 2nd week amt from 1st week amt to get 2nd weeks amount, and similarly 3rd week amount from the 2nd week amount to get 3rd week amount and 4th week amount from 3rd week amt to get 4th weeks amount.

But when we go to second period we should not subtract the week 1 amount field from previous period's 4th week amount.
week 1 amount for period is fine,now we need to subtract 2nd week amt from 1st week amount.

I wrote sql statement as below

SELECT A.CUSTOMERNO, A.DATE, A.PERIOD,
( case when Mod(A.WEEKNO,4)= 0 then 4 Else Mod(A.WEEKNO,4) end),
A.ADDRESS, ( CASE WHEN (Mod(A.CAL_WEEK_ID,4)) = 1 Then A.GROCERY Else (A.AMT- B.AMT ) END )
FROM TABLEA A, TABLEB B
WHERE B.CUSTOMERNO = A.CUSTOMERNO year(B.DATE) = year(A.DATE) and
B.WEEKNO=A.WEEKNO-1 )

It is working fine for all the Periods from 2 to 13,but for the first period the week1 data is not displayed.

Do i need to change the SQL or do some additions to the above SQL.Please provide me the SQL for this problem.

Any help is appreciated.

Thanks
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top