DevX Home Today's Headlines   Articles Archive   Tip Bank   Forums

Thread: Is it possible to make a total sum of the sum of present month + (present month - 1)?

Hybrid View

1. Registered User
Join Date
Jul 2004
Location
Sweden
Posts
1

Is it possible to make a total sum of the sum of present month + (present month - 1)?

The SQL below is functioning when it comes to summarize ONE month. My problem is that I want the sum of three previous month, i.e.

TOTAL SUM of
(present month (SUM (A1.SA_DEB_M_SE_HBO - A1.SA_KRE_M_SE_HBO)
+ previous month (SUM (A1.SA_DEB_M_SE_HBO - A1.SA_KRE_M_SE_HBO)
+ present month -2 (SUM (A1.SA_DEB_M_SE_HBO - A1.SA_KRE_M_SE_HBO))

The month of interest is located in DB2.EMIIHB02 A3

How do I accomplish this!??!
/Maria

SELECT A1.MONTH
,A1.ORGNR_DB
,A2.KOD_SEKTOR
,A2.KAT_EGARE
,A2.KOD_LAND
,A2.ID_GRUPP_PROD || A2.ID_STR
,A1.ID_KTO_HBO
,A2.ID_VALUTA
,A2.ID_BELOPP_TYP
,A2.ID_LOPTID
,A3.ID_HBO_DATUM
,SUM (A1.SA_DEB_M_SE_HBO - A1.SA_KRE_M_SE_HBO)

FROM DB2.STYIV011 A1,
DB2.EMIISA01 A2,
DB2.EMIIHB02 A3

WHERE A1.MONTH = A3.MONTH
AND A1.DAT_OVERF_HBO = A3.ID_HBO_DATUM
AND A1.ORGNR_DB = A3.BANKID
AND A1.ID_KODOVERF_HBO <> '2'
AND A2.ID_GRUPP_PROD = 'HBO'
AND A2.ID_MATVARDE_TYP = 'MV022'
AND A2.KOD_BANKTILLH = '2'
AND A1.ORGNR_DB < 8999
AND A1.ID_KTO_HBO BETWEEN A2.PRODID_FROM AND A2.PRODID_TOM

GROUP BY A1.MONTH
,A1.ORGNR_DB
,A2.KOD_SEKTOR
,A2.KAT_EGARE
,A2.KOD_LAND
,A2.ID_GRUPP_PROD
,A2.ID_STR
,A1.ID_KTO_HBO
,A2.ID_VALUTA
,A2.ID_BELOPP_TYP
,A2.ID_LOPTID
,A3.ID_HBO_DATUM
HAVING SUM (A1.SA_DEB_M_SE_HBO - A1.SA_KRE_M_SE_HBO) ^= 0
;

2. Registered User
Join Date
Jun 2004
Posts
22
I'm having some trouble reading your field names, but I felt I should throw this out anyway:

SELECT Sum(fieldOne - fieldTwo)
FROM tableName
WHERE Month(YourDateField) > Month(Date())-3;

An example of the data you're starting with and the data you want to get from your query might be helpful in clarifying the problem.

Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•

 FAQ Latest Articles Java .NET XML Database Enterprise