Réécriture d'une requête complexe

Fermé
jihen_badreddine Messages postés 14 Date d'inscription vendredi 24 juin 2016 Statut Membre Dernière intervention 10 février 2018 - 28 nov. 2017 à 15:47
jordane45 Messages postés 38139 Date d'inscription mercredi 22 octobre 2003 Statut Modérateur Dernière intervention 20 avril 2024 - 28 nov. 2017 à 17:03
Bonjour à tous ;

J'ai une requête complexe et je la réécrire selon les parties entre le or donc j'ai trois jointures pour une autre travail.

Ma problème est comment je puisse prendre ces trois jointures et les mets dans la requêtes d'origine selon l'opérateur or.
merci de m'aider

Voila la Requête d'origine :

 select
sum(l_extendedprice* (1 - l_discount)) as revenue
from
lineitem,
part
where
( p_partkey = l_partkey and
p_brand = 'Brand#31' and
p_container in ('SM CASE', 'SM BOX', 'SM PACK', 'SM PKG') and
l_quantity >= 3 and l_quantity <= 3 + 10 and
p_size between 1 and 5 and
l_shipmode in ('AIR', 'AIR REG') and
l_shipinstruct = 'DELIVER IN PERSON' ) or

( p_partkey = l_partkey and
p_brand = 'Brand#21' and
p_container in ('MED BAG', 'MED BOX', 'MED PKG', 'MED PACK') and
l_quantity >= 15 and l_quantity <= 15 + 10 and
p_size between 1 and 10 and
l_shipmode in ('AIR', 'AIR REG') and
l_shipinstruct = 'DELIVER IN PERSON' ) or

( p_partkey = l_partkey and
p_brand = 'Brand#45' and
p_container in ('LG CASE', 'LG BOX', 'LG PACK', 'LG PKG') and
l_quantity >= 23 and l_quantity <= 23 + 10 and
p_size between 1 and 15 and
l_shipmode in ('AIR', 'AIR REG') and
l_shipinstruct = 'DELIVER IN PERSON') ;



et voila les trois jointures :
Code :

create table  Join1_p_l_Q19 As select * from  Filter1_l_shipinstruct_Q19 l , Filter1_p_size_Q19 p where   p.p_partkey = l.l_partkey ; 


Code :

create table  Join2_p_l_Q19 As select * from Filter2_l_shipinstruct_Q19 l , Filter2_p_size_Q19 p where   p.p_partkey = l.l_partkey ; 


Code :

create table  Join3_p_l_Q19 As select * from Filter3_l_shipinstruct_Q19 l , Filter3_p_size_Q19 p where   p.p_partkey = l.l_partkey ;   


Voila l’exécution du requête d'origine :


Et voila le résultat donné pour ma réécriture :

create table  Project_final_Q19 As  select sum(l_extendedprice* (1 - l_discount)) as revenue from Join1_p_l_Q19  union select sum(l_extendedprice* (1 - l_discount)) as revenue from  Join2_p_l_Q19 union select sum(l_extendedprice* (1 - l_discount)) as revenue from  Join3_p_l_Q19 ;


1 réponse

jordane45 Messages postés 38139 Date d'inscription mercredi 22 octobre 2003 Statut Modérateur Dernière intervention 20 avril 2024 4 649
Modifié le 28 nov. 2017 à 17:03
Bonjour,

Je ne comprend pas ce que tu souhaites faire.
Tu parles de jointure... mais en réalité tu créés des tables (qui ne seront donc pas dynamiques...)
Ensuite tu nous montres une requête "finale" qui utilise des UNION (et non des jointures)...

Bref....
Commence par nous donner un dump de tes tables
Explique nous en détail ce que tu souhaites obtenir.

PS: Déjà, dans ta requête initiale, certaines conditions peux être factorisées.
Par exemple :
SELECT SUM(l_extendedprice* (1 - l_discount)) as revenue 
FROM lineitem, 
  part 
WHERE  p_partkey = l_partkey 
  AND  l_shipinstruct = 'DELIVER IN PERSON'
  AND  l_shipmode in ('AIR', 'AIR REG')
  AND ( 
  (
     p_brand = 'Brand#31'
    AND p_container in ('SM CASE', 'SM BOX', 'SM PACK', 'SM PKG')
    AND l_quantity >= 3 and l_quantity <= 3 + 10 
    AND p_size between 1 and 5 and  
  )
  OR
  (
     p_brand = 'Brand#21'
    AND p_container in ('MED BAG', 'MED BOX', 'MED PKG', 'MED PACK')
    AND l_quantity >= 15 and l_quantity <= 15 + 10
    AND p_size between 1 and 10 and  
  )
  OR
  (
     p_brand = 'Brand#45'
    AND p_container in ('LG CASE', 'LG BOX', 'LG PACK', 'LG PKG')
    AND l_quantity >= 23 and l_quantity <= 23 + 10
    AND p_size between 1 and 15 and  
  )
 )



Cordialement, 
Jordane                                                                 
0