Skip to Main Content

SQL & PL/SQL

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Interested in getting your voice heard by members of the Developer Marketing team at Oracle? Check out this post for AppDev or this post for AI focus group information.

Processing Cost - How to catch a soccer team with the highest combined score?

Filippe Soares RozaJun 21 2013 — edited Jul 2 2013

Hi to all.

It's a pleasure to receive your help.


I am using Oracle Database 11g Express Edition Release 11.2.0.2.0 - Beta and I am working with the two follows tables.

I want to get a soccer team with the highest combined score averages and participations considering all possible formations and my budget, but I'm not finding a way to get a cost effective processing.


Suggestions?

create table ESQUEMA

(

  ESQUEMA   INTEGER,

  ZAGUEIROS INTEGER,

  LATERAIS  INTEGER,

  MEIAS     INTEGER,

  ATACANTES INTEGER,

  P1        CHAR(3),

  P2        CHAR(3),

  P3        CHAR(3),

  P4        CHAR(3),

  P5        CHAR(3),

  P6        CHAR(3),

  P7        CHAR(3),

  P8        CHAR(3),

  P9        CHAR(3),

  P10       CHAR(3)

)

;

esquema table - sample data

ESQUEMAZAGUEIROSLATERAISMEIASATACANTESP1P2P3P4P5P6P7P8P9P10
14422242ZAGZAGLATLATMEIMEIMEIMEIATAATA
24332233ZAGZAGLATLATMEIMEIMEIATAATAATA
35323232ZAGZAGZAGLATLATMEIMEIMEIATAATA
4352352ZAGZAGZAGMEIMEIMEIMEIMEIATAATA
5343343ZAGZAGZAGMEIMEIMEIMEIATAATAATA
64512251ZAGZAGLATLATMEIMEIMEIMEIMEIATA
75413241ZAGZAGZAGLATLATMEIMEIMEIMEIATA


create table JOGADORES

(

  ID      INTEGER not null,

  CLUBE   VARCHAR2(100),

  NOME    VARCHAR2(100),

  POSICAO CHAR(3),

  PRECO   NUMBER(4,2),

  MEDIA   NUMBER(4,2),

  JOGOS   INTEGER

)

;


jogadores table - sample data

IDCLUBENOMEPOSICAOPRECOMEDIAJOGOS
80403FluminenseAbel BragaTEC17,515,364
70393CoritibaAlexMEI16,985,085
42365NáuticoAuremirLAT7,735,484
18341GrêmioBarcosATA18,963,674
102425GrêmioBressanZAG10,855,904
31354CrisciúmaBrunoGOL10,663,205
105428CruzeiroBruno RodrigoZAG15,475,285
82405VitóriaCaio JúniorTEC11,404,455
41364FluminenseCarlinhosLAT12,406,933
61384VascoCarlos AlbertoMEI15,016,752
13336Ponte PretaChiquinhoATA9,974,645
72395Ponte PretaCicinhoMEI11,424,725
92415SantosClaudinei OliveiraTEC11,923,173
104427Ponte PretaCléberZAG14,615,785
68391PortuguesaCorreaMEI8,445,604
93416BahiaCristóvão BorgesTEC8,272,925
96419Atlético-MGCucaTEC12,622,324
29352CorinthiansCássioGOL12,513,745


I want a result like this query could bring, if it had a low cost. Should I make any changes in tables or attempting to use PL / SQL?

select *

  from jogadores j1,

       jogadores j2,

       jogadores j3,

       jogadores j4,

       jogadores j5,

       jogadores j6,

       jogadores j7,

       jogadores j8,

       jogadores j9,

       jogadores j10,

       jogadores j11,

       jogadores j12,

       esquema   e

where j1.posicao = e.p1

   and j2.posicao = e.p2

   and j3.posicao = e.p3

   and j4.posicao = e.p4

   and j5.posicao = e.p5

   and j6.posicao = e.p6

   and j7.posicao = e.p7

   and j8.posicao = e.p8

   and j9.posicao = e.p9

   and j10.posicao = e.p10

   and j11.posicao = 'TEC'

   and j12.posicao = 'GOL'

      --

   and j1.id NOT IN (j2.id,

                     j3.id,

                     j4.id,

                     j5.id,

                     j6.id,

                     j7.id,

                     j8.id,

                     j9.id,

                     j10.id,

                     j11.id,

                     j12.id)

      --

   and j2.id NOT IN (j3.id,

                     j4.id,

                     j5.id,

                     j6.id,

                     j7.id,

                     j8.id,

                     j9.id,

                     j10.id,

                     j11.id,

                     j12.id)

      --

   and j3.id NOT IN

       (j4.id, j5.id, j6.id, j7.id, j8.id, j9.id, j10.id, j11.id, j12.id)

      --

   and j4.id NOT IN

       (j5.id, j6.id, j7.id, j8.id, j9.id, j10.id, j11.id, j12.id)

      --

   and j5.id NOT IN (j6.id, j7.id, j8.id, j9.id, j10.id, j11.id, j12.id)

      --

   and j6.id NOT IN (j7.id, j8.id, j9.id, j10.id, j11.id, j12.id)

      --

   and j7.id NOT IN (j8.id, j9.id, j10.id, j11.id, j12.id)

      --

   and j8.id NOT IN (j9.id, j10.id, j11.id, j12.id)

      --

   and j9.id NOT IN (j10.id, j11.id, j12.id)

      --

   and j10.id NOT IN (j11.id, j12.id)

      --

   and j11.id <> j12.id

   and (e.esquema,

        (j1.media + j2.media + j3.media + j4.media + j5.media + j6.media +

        j7.media + j8.media + j9.media + j10.media + j11.media + j12.media +

        j1.jogos + j2.jogos + j3.jogos + j4.jogos + j5.jogos + j6.jogos +

        j7.jogos + j8.jogos + j9.jogos + j10.jogos + j11.jogos + j12.jogos)) in

       (select e.esquema,

               nvl(max(j1.media + j2.media + j3.media + j4.media + j5.media +

                       j6.media + j7.media + j8.media + j9.media + j10.media +

                       j11.media + j12.media + j1.jogos + j2.jogos +

                       j3.jogos + j4.jogos + j5.jogos + j6.jogos + j7.jogos +

                       j8.jogos + j9.jogos + j10.jogos + j11.jogos +

                       j12.jogos),

                   0)

          from jogadores j1,

               jogadores j2,

               jogadores j3,

               jogadores j4,

               jogadores j5,

               jogadores j6,

               jogadores j7,

               jogadores j8,

               jogadores j9,

               jogadores j10,

               jogadores j11,

               jogadores j12,

               esquema   e

         where j1.posicao = e.p1

           and j2.posicao = e.p2

           and j3.posicao = e.p3

           and j4.posicao = e.p4

           and j5.posicao = e.p5

           and j6.posicao = e.p6

           and j7.posicao = e.p7

           and j8.posicao = e.p8

           and j9.posicao = e.p9

           and j10.posicao = e.p10

           and j11.posicao = 'TEC'

           and j12.posicao = 'GOL'

              --     

           and j1.id NOT IN (j2.id,

                             j3.id,

                             j4.id,

                             j5.id,

                             j6.id,

                             j7.id,

                             j8.id,

                             j9.id,

                             j10.id,

                             j11.id,

                             j12.id)

              --

           and j2.id NOT IN (j3.id,

                             j4.id,

                             j5.id,

                             j6.id,

                             j7.id,

                             j8.id,

                             j9.id,

                             j10.id,

                             j11.id,

                             j12.id)

              --

           and j3.id NOT IN (j4.id,

                             j5.id,

                             j6.id,

                             j7.id,

                             j8.id,

                             j9.id,

                             j10.id,

                             j11.id,

                             j12.id)

              --

           and j4.id NOT IN

               (j5.id, j6.id, j7.id, j8.id, j9.id, j10.id, j11.id, j12.id)

              --

           and j5.id NOT IN

               (j6.id, j7.id, j8.id, j9.id, j10.id, j11.id, j12.id)

              --

           and j6.id NOT IN (j7.id, j8.id, j9.id, j10.id, j11.id, j12.id)

              --

           and j7.id NOT IN (j8.id, j9.id, j10.id, j11.id, j12.id)

              --

           and j8.id NOT IN (j9.id, j10.id, j11.id, j12.id)

              --

           and j9.id NOT IN (j10.id, j11.id, j12.id)

              --

           and j10.id NOT IN (j11.id, j12.id)

              --

           and j11.id <> j12.id

           and (j1.preco + j2.preco + j3.preco + j4.preco + j5.preco +

               j6.preco + j7.preco + j8.preco + j9.preco + j10.preco +

               j11.preco + j12.preco) <= &patrimonio

         group by e.esquema)

   and (j1.preco + j2.preco + j3.preco + j4.preco + j5.preco + j6.preco +

       j7.preco + j8.preco + j9.preco + j10.preco + j11.preco + j12.preco) <=

       &patrimonio;


Thank in advance,

Filippe

Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jul 30 2013
Added on Jun 21 2013
14 comments
2,371 views