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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

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.AMT 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
 
Possibly there is no data for week1 in the first period because there is no A.WEEKNO-1 for that week, week zero.

A simple solution might be to UNION this query with one that will give the results for week1 only.

Code:
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.AMT 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 )

UNION

SELECT CUSTOMERNO, DATE, PERIOD,
       1,
       AMT
FROM TABLEA 
WHERE WEEKNO = 1

I post this code just to give substance to my suggestion. I dont thoroughly understand your requirement. Really, to arrive at the solution as you have is rather impressive. I am sure you will crack this last piece.
 
Thanks, rac this solution is working fine with more where conditions which i need.

Thanks for the quick reply.

 

how about this?

Code:
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.AMT Else (A.AMT- B.AMT ) END ) 
FROM TABLEA A left outer join TABLEB B 
on B.CUSTOMERNO = A.CUSTOMERNO 
and year(B.DATE) = year(A.DATE) 
and B.WEEKNO=A.WEEKNO-1 )


 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top