Oracle FAQ | Your Portal to the Oracle Knowledge Grid |
![]() |
![]() |
Home -> Community -> Usenet -> c.d.o.misc -> Re: Getting two fields from the one table.
"Prince Of Thieves" <thief_NOSPAM_at_hotmail.com> wrote in message
news:N5Wwb.73$wh7.3507_at_nnrp1.ozemail.com.au...
| Thanks McStock & VC. | | I can't get it to work. What should the resulting SQL look like? | | -- | | Julian Milano | |
here's the basic concept:
instead of this:
SQL> select
2 prod.name
3 , prod.id 4 , prc.price_class 5 , prc.price
NAME ID PRICE_CLASSPRICE
-------------------------------------------------- ---------- ----------- -- -------- Ace Ski Boot 10012 1 175 Ace Ski Boot 10012 2 178.5 Ace Ski Pole 10022 1 21.95 Ace Ski Pole 10022 2 23.05 Alexeyer Pro Lifting Bar 40421 1 85 Alexeyer Pro Lifting Bar 40421 2 77.35 Alomar Glove 50418 1 75 Alomar Glove 50418 295.25
do this:
1 select
2 prod.name 3 , prod.id 4 , prc1.price as CLASS1 5 , prc2.price as CLASS2
7 s_product prod 8 , s_price prc1 9 , s_price prc2
NAME ID CLASS1CLASS2
-------------------------------------------------- ---------- ---------- --- ------- Ace Ski Boot 10012 175 178.5 Ace Ski Pole 10022 21.95 23.05 Alexeyer Pro Lifting Bar 40421 85 77.35 Alomar Glove 50418 7595.25
here's the DECODE version someone suggested:
SQL> select
2 prod.name
3 , prod.id
4 , max(
5 decode(
6 prc.price_class 7 ,1,prc.price 8 ,null 9 )
13 prc.price_class 14 ,2,prc.price 15 ,null 16 )
NAME ID CLASS1CLASS2
-------------------------------------------------- ---------- ---------- --- ------- Ace Ski Boot 10012 175 178.5 Ace Ski Pole 10022 21.95 23.05 Alexeyer Pro Lifting Bar 40421 8577.35
with the decode, you artificially must select the max of each prices, and you must include all other columns in the GROUP BY -- which can then get quite large
again, the reference suggested earlier is http://asktom.oracle.com, search for PIVOT (which is a concept, not a SQL keyword or function)
![]() |
![]() |