Unix Technical Forum

Query using group

This is a discussion on Query using group within the Oracle Miscellaneous forums, part of the Oracle Database category; --> i have a table with 4 columns, they look like currency1 currency2 amt1 amt2 ======================= USD GBP 10 9 ...


Go Back   Unix Technical Forum > Database Server Software > Oracle Database > Oracle Miscellaneous

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 04-24-2008, 07:09 PM
Prasath
 
Posts: n/a
Default Query using group

i have a table with 4 columns, they look like

currency1 currency2 amt1 amt2
=======================
USD GBP 10 9
GBP USD 5 8
JPY INR 20 7
INR JPY 14 100

i want to cross add the amounts for each group of currency1 and
currency2. now, the currency groups are bit unusal. For example,
the first two rows in the above table fall under the group USD+GBP (or
GBP+USD, the other does not matter)

the result should look like

currency1 currency2 sum1 sum2
USD GBP 18 14 (from 10+8 and 9+5)
INR JPY 120 21 (from 20+100 and 14+7)

any ideas?
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 04-24-2008, 07:09 PM
Ed Prochak
 
Posts: n/a
Default Re: Query using group

On Apr 23, 7:35 am, Prasath <prasath....@gmail.com> wrote:
> i have a table with 4 columns, they look like
>
> currency1 currency2 amt1 amt2
> =======================
> USD GBP 10 9
> GBP USD 5 8
> JPY INR 20 7
> INR JPY 14 100
>
> i want to cross add the amounts for each group of currency1 and
> currency2. now, the currency groups are bit unusal. For example,
> the first two rows in the above table fall under the group USD+GBP (or
> GBP+USD, the other does not matter)
>
> the result should look like
>
> currency1 currency2 sum1 sum2
> USD GBP 18 14 (from 10+8 and 9+5)
> INR JPY 120 21 (from 20+100 and 14+7)
>
> any ideas?


what have you tried?
I can imagine a simple UNION may help. Then you will need a way to
weed out the "duplicates", e.g.
USD GBP 18 14
GBP USD 14 18
Show us what you tried and then we can help.
(This is to avoid doing someone's homework for them.)

Awaiting your reply,
ed
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 04-24-2008, 07:09 PM
Prasath
 
Posts: n/a
Default Re: Query using group

On Apr 23, 1:50 pm, Ed Prochak <edproc...@gmail.com> wrote:
> On Apr 23, 7:35 am, Prasath <prasath....@gmail.com> wrote:
>
>
>
> > i have a table with 4 columns, they look like

>
> > currency1 currency2 amt1 amt2
> > =======================
> > USD GBP 10 9
> > GBP USD 5 8
> > JPY INR 20 7
> > INR JPY 14 100

>
> > i want to cross add the amounts for each group of currency1 and
> > currency2. now, the currency groups are bit unusal. For example,
> > the first two rows in the above table fall under the group USD+GBP (or
> > GBP+USD, the other does not matter)

>
> > the result should look like

>
> > currency1 currency2 sum1 sum2
> > USD GBP 18 14 (from 10+8 and 9+5)
> > INR JPY 120 21 (from 20+100 and 14+7)

>
> > any ideas?

>
> what have you tried?
> I can imagine a simple UNION may help. Then you will need a way to
> weed out the "duplicates", e.g.
> USD GBP 18 14
> GBP USD 14 18
> Show us what you tried and then we can help.
> (This is to avoid doing someone's homework for them.)
>
> Awaiting your reply,
> ed


the below query works.

select LEAST(currency1,currency2) first, GREATEST(currency1,currency2)
second,
SUM(case when currency1 = greatest(currency1,currency2) then amt1 else
amt2 end) amt1,
SUM(case when currency1 = least(currency1,currency2) then amt1 else
amt2 end) amt2
from test
group by LEAST(currency1,currency2), GREATEST(currency1,currency2)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 07:25 AM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.2.0
www.UnixAdminTalk.com