Connecting Tech Pros Worldwide Help | Site Map

Problem with calculate difference between rows

  #1  
Old October 30th, 2008, 05:55 PM
roberto
Guest
 
Posts: n/a
I have the following problem:

In a view I obtain three rows and I have to group them together making
some particular operation to add dinamic column.

The query without the group by give me these results :


CMP CNT CODE GROUP ENDQTY ROWPARTITIONED
001 PRD 100 AAA 20 1
001 PRD 100 BBB 50 2
001 PRD 100 CCC 100 3

-------------------------------------------------


CMP CNT CODE GROUP ENDQTY ROWPARTITIONED
001 PRD 100 AAA 20 1
001 PRD 110 BBB 50 1
001 PRD 120 CCC 100 1



In the first case what I want is this one

CMP CNT CLM1 CLM2 CLM3
001 PRD 20 30 50



in the second case what I want is

CMP CNT CLM1 CLM2 CLM3
001 PRD 20 50 100


The columns(CLMx) must be calculated in the following way :

If the ROWPARTITIONED column is equal to 1 the the column is equal to
ENDQTY

If the ROWPARTITIONED column is greater then 1 the the column is equal
to ENDQTY -( ENDQTY of the previous group)
CLM2 = 30 because ENDQTY wit hrow partiioned = 2 --50 and ENDQTY of
the previous row = 20... so 50-20 = 30

What I have done is a the following query :

SELECT CMP, CNT, SUM(CASE WHEN GROUP = 'AAA' THEN ENDQTY ELSE 0 END)
CLM1,
SUM(CASE WHEN GROUP = 'BBB' THEN ENDQTY ELSE 0 END) - SUM(CASE WHEN
GROUP = 'AAA' THEN ENDQTY ELSE 0 END) CLM2,
SUM(CASE WHEN GROUP = 'CCC' THEN ENDQTY ELSE 0 END) - SUM(CASE WHEN
GROUP = 'BBB' THEN ENDQTY ELSE 0 END) CLM3

GROUP BY CMP, CNT

This query gives me the possibility to obtain a single row but I'm not
able to write something like this

CASE WHEN ROWPARTITIONED 1 THEN SUM(CASE WHEN GROUP = 'BBB' THEN
ENDQTY ELSE 0 END) - SUM(CASE WHEN GROUP = 'AAA' THEN ENDQTY ELSE 0
END) ELSE SUM(CASE WHEN GROUP = 'BBB' THEN ENDQTY ELSE 0 END) END

because I obtain an sql error (SQLCODE -119) due to the fact that the
field ROWPARTITIONED is in select and not in group by.


I don't want to use (if it is possible) an "inner/sub" select in each
CASE statement, becuase this is only a little part of the real very
complex statement.

Can someone help me pls?

Thank You very much (in advance) to all for the support!

bye!

  #2  
Old October 31st, 2008, 05:45 PM
roberto
Guest
 
Posts: n/a

re: Problem with calculate difference between rows


Thank you very much to all for the reply.
Tonkuma: no, I haven't alwyas 3 rows

but fortunatelly both Tonkuma and Lennart give me same very good
suggestion. and I solved (with your help) my problem.

Thanks again!
Roberto
  #3  
Old October 31st, 2008, 06:35 PM
Tonkuma
Guest
 
Posts: n/a

re: Problem with calculate difference between rows


but fortunatelly both Tonkuma and Lennart give me same very good
Quote:
suggestion. and I solved (with your help) my problem.
>
Thanks again!
Roberto
I feel very glad, if I could give you some help to solve your problem.
Closed Thread


Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem with datagridview Martin answers 2 December 16th, 2006 04:05 PM
Inserting Multiple Rows into one table (with calculated fields) Mohd Al Junaibi answers 9 November 30th, 2006 09:15 PM
numpy help Chris Smith answers 2 November 3rd, 2006 11:45 PM
PROBLEM WITH THREADS - WIN 2003 Mr answers 5 February 14th, 2006 06:05 PM