View previous topic :: View next topic |
Author |
Message |
psridhar Beginner
Joined: 16 May 2004 Posts: 68 Topics: 26
|
Posted: Tue Feb 01, 2005 10:26 am Post subject: Resultant table.... |
|
|
Can anybody help me in achieving the resultant table as follows.
My main table contains the columns as follows.
[code:1:181fa4cca1]
----------------------------------------------------------------------
Month Year Customer Id Fee Type Fee Amount
----------------------------------------------------------------------
12 2004 C1 AAAA 100.00
12 2004 C1 BBBB 70.00
12 2004 C2 AAAA 30.00
12 2004 C2 CCCC 50.00
12 2004 C3 AAAA 55.00
12 2004 C3 BBBB 75.00
01 2005 C2 AAAA 65.00
01 2005 C3 AAAA 45.00
01 2005 C3 BBBB 55.00
01 2005 C4 AAAA 25.00
01 2005 C4 CCCC 35.00
----------------------------------------------------------------------
Say the fee paid in 12-2004 as Previous Sum and in 01-2005 as Current Sum.
My resultant table should be as follows.
-------------------------------------------------------
Customer Id Current Sum Previous Sum |
|
Back to top |
|
 |
kolusu Site Admin

Joined: 26 Nov 2002 Posts: 12378 Topics: 75 Location: San Jose
|
Posted: Tue Feb 01, 2005 11:50 am Post subject: |
|
|
Psridhar,
What is the column definitions of Month, YR and amount fields?
Kolusu _________________ Kolusu
www.linkedin.com/in/kolusu |
|
Back to top |
|
 |
kolusu Site Admin

Joined: 26 Nov 2002 Posts: 12378 Topics: 75 Location: San Jose
|
Posted: Tue Feb 01, 2005 1:32 pm Post subject: |
|
|
Psridhar,
Assuming that your column definitions for month and year are integers, the following query will give you the desired results.
Code: |
SELECT A.CUST_ID
,SUM(A.PREV1) CURR_SUM
,SUM(A.PREV2) PREV_SUM
FROM (SELECT CUST_ID
,CASE WHEN MO = MONTH(CURRENT DATE) AND
YR = YEAR(CURRENT DATE)
THEN FEE_AMT ELSE 0 END CURR1
,CASE WHEN MO = MONTH(CURRENT DATE - 1 MONTH ) AND
YR = YEAR(CURRENT DATE - 1 MONTH)
THEN FEE_AMT ELSE 0 END PREV1
,CASE WHEN MO = MONTH(CURRENT DATE - 2 MONTH ) AND
YR = YEAR(CURRENT DATE - 2 MONTH)
THEN FEE_AMT ELSE 0 END PREV2
FROM T1) A
GROUP BY A.CUST_ID
;
|
Hope this helps...
Cheers
Kolusu _________________ Kolusu
www.linkedin.com/in/kolusu |
|
Back to top |
|
 |
psridhar Beginner
Joined: 16 May 2004 Posts: 68 Topics: 26
|
Posted: Wed Feb 02, 2005 5:14 am Post subject: |
|
|
Hi Kolusu
Thank you very much..... With some slight modifications to your SQL, I could able to achieve my requirement. Thanks for the suggessions.
Thanks
Sridhar |
|
Back to top |
|
 |
|
|