SQL Language (MOSC)

MOSC Banner

SQL that does a geometric/arithmetic series

edited Dec 16, 2011 3:40AM in SQL Language (MOSC) 7 commentsAnswered ✓
I have this data set:

Animal     precedence   number_column

CAT               1                     cat1

CAT                2                    cat2

CAT              3                      cat3

CAT               4                     cat4

DOG               1                     dog1

DOG               2                     dog2

FISH               1                     fish1

FISH               2                      fish2

FISH               3                      fish3

where the cat1...fish3 are numeric values

Desired output:

Animal     precedence   number_column      arith_sum

CAT               1                     cat1                     cat1

CAT                2                    cat2                     cat1 + cat2

CAT              3                      cat3                     cat1 + (cat1 + cat2 ) + cat3

CAT               4                     cat4                     cat1 + (cat1 + cat2) + (cat1 + (cat1 + cat2) + cat3) + cat4

DOG               1                     dog1                   dog1

DOG               2                     dog2                   dog1 + dog2

FISH               1                     fish1                   fish1

FISH               2                      fish2                 fish1 + fish2

FISH               3                      fish3                  fish1 + (fish1 + fish2) + fish3

'precedence' defines the order within each animal group

How can you write a query that returns the desited result set?

Howdy, Stranger!

Log In

To view full details, sign in to My Oracle Support Community.

Register

Don't have a My Oracle Support Community account? Click here to get started.

Category Leaderboard

Top contributors this month

New to My Oracle Support Community? Visit our Welcome Center

MOSC Help Center