Oracle FAQ | Your Portal to the Oracle Knowledge Grid |
Home -> Community -> Usenet -> c.d.o.server -> Re: column to row (computed)
You probably left out something. Otherwise you practically have
answered your own question. This logic would get the job done.
> select key, sum (case when State = "STATE1" then + Value
> when State = "STATE2" then - Value)
> from tab1
> group by key
garodiar_at_yahoo.com (Ritesh G.) wrote in message news:<1ada02e0.0210251510.5f0c01b5_at_posting.google.com>...
> hi,
>
> I wanted to know if one can do following with one/two/more SQL select
> statements, (may be using some oracle functions also)
>
> Suppose there is a table tab1 as
>
> Key State Value
> =======================
> P1 STATE1 Value1
> P1 STATE2 Value2
> P2 STATE1 Value3
> P2 STATE2 Value4
>
> Now the result desired is following with two columns
>
> P1 (Value1 - Value2) ie { STATE1 - STATE2}
> P2 (Value3 - Value4) ie { STATE1 - STATE2}
>
> In short for each key in table we get (state, value) pairs, then get a
> computed state based on state values and rule as 'STATE1's Value -
> 'STATE2's Value.
>
> Something like
>
> select key, sum (case when State = "STATE1" then + Value
> when State = "STATE2" then - Value)
> from tab1
> group by key
>
> Thanks
> Ritesh
> garodiar_at_yahoo.com
Received on Sat Oct 26 2002 - 12:13:49 CDT