correlated subquery

correlated subquery - SQL/NoSQL - Programmation

Marsh Posté le 12-02-2008 à 15:32:44    

Bonjour, j'ai besoin d'un coup de pouce car je bloque. :cry:  
 
Je dois utiliser une 'correlated subquery'.
Je dois créer une requête qui retournera tous les clients qui ont une limite de credit égal ou inférieur à la somme des commandes clients.
 
 
Actuellement, j'affiche tout :
 
select c.customer_id as id,
c.name,
c.credit_limit as limit,
sum(s.total) as spent
from customer c,
sales_order s
where c.customer_id = s.customer_id
group by c.customer_id, c.name, c.credit_limit;
 
      ID NAME                                               LIMIT      SPENT
---------- --------------------------------------------- ---------- ----------
       106 SHAPE UP                                      6000     9076.8
       228 FITNESS FIRST                               4000       7101
       204 THE POWER FORWARD                     12000    15071.4
       208 AL AND BOB'S SPORTS                       4000    21119.8
 
 
Ca affiche tout alors que je dois afficher que ceux qui ne dépasse pas leur limite de crédit avec 'correlated subquery'.
 
Je ne sais pas trop comment faire la suite.
 
Merci pour votre aide.

Reply

Marsh Posté le 12-02-2008 à 15:32:44   

Reply

Marsh Posté le 12-02-2008 à 15:42:32    

Apres ton group

Code :
  1. HAVING c.credit_limit >= sum(s.total)


---------------
Software and cathedrals are much the same - first we build them, then we pray.
Reply

Marsh Posté le 12-02-2008 à 15:56:29    

j'ai trouvé !!
 
select c.customer_id as id,
c.name,
c.credit_limit as limit,
sum(s.total) as spent
from customer c,
sales_order s
where c.customer_id=s.customer_id
group by c.customer_id, c.name, c.credit_limit
having sum(s.total)<=
        (select cr.credit_limit
        from customer cr
        where cr.customer_id=c.customer_id);

Reply

Marsh Posté le 12-02-2008 à 16:16:43    

johnny1234 a écrit :

j'ai trouvé !!
 
select c.customer_id as id,
c.name,
c.credit_limit as limit,
sum(s.total) as spent
from customer c,
sales_order s
where c.customer_id=s.customer_id
group by c.customer_id, c.name, c.credit_limit
having sum(s.total)<=
        (select cr.credit_limit
        from customer cr
        where cr.customer_id=c.customer_id);


[:prozac] s'quoi l'interêt de la subquery dans ton having ?


---------------
Software and cathedrals are much the same - first we build them, then we pray.
Reply

Sujets relatifs:

Leave a Replay

Make sure you enter the(*)required information where indicate.HTML code is not allowed