2011-07-19 5 views
3

J'ai quatre tables: produits, pc, ordinateur portable et imprimante.Utilisation de MAX avec plusieurs tables

Products(maker, model, type) 
    PC(code, model, speed, hd, cd, price) 
    Laptop(code, model, speed, ram, hd, screen, price) 
    Printer(code, model, color, type price) 

Ce que je dois est de trouver le numéro de modèle du produit (PC, portable ou imprimante), qui a le prix le plus élevé. Cela ne fonctionnera pas avec une instruction case car si deux numéros de modèle ont le prix le plus élevé, les deux doivent être affichés, et l'utilisation d'un cas en sélectionnera un seul, puis quittera l'instruction case. Je voudrais le faire en utilisant l'opérateur UNION, mais je ne suis pas sûr de savoir comment le faire. C'est ce que j'ai jusqu'à présent:

Mais c'est une syntaxe incorrecte et je ne sais pas pourquoi. Des idées?

Répondre

1

Vous devez créer un alias de vos tables dérivées: see this post


Edit: cela devrait fonctionner pour obtenir les modèles avec le prix max. (Je ne sais pas si cela est la syntaxe correcte pour le serveur SQL.)

with max_price(price) as (
    SELECT MAX(price) 
     FROM (
      SELECT price 
      FROM Pc 
      UNION ALL 
      SELECT price 
      FROM Laptop 
      UNION ALL 
      SELECT price 
      FROM Printer 
     ) as sub1 
) 

SELECT model, price 
FROM (
    SELECT model, price 
    FROM Pc 
    UNION ALL 
    SELECT model, price 
    FROM Laptop 
    UNION ALL 
    SELECT model, price 
    FROM Printer 
) as sub2 
JOIN max_price m ON m.price = sub2.price 
+0

qui a résolu le problème de syntaxe, mais maintenant ma requête retourne tous les numéros de modèle unique, pas seulement celui avec le prix max .... – nathpilland

+0

Presque, mais il dit le prix est un nom de colonne non valide. Ai-je besoin d'une table opérateur de rendre le prix sans ambiguïté? – nathpilland

+0

Je ne suis pas exactement sûr de ce que le ... avec, comme vous pouvez le voir, je suis tout à fait le débutant:/Je pense que je comprends, mais le serveur SQL ne l'aime toujours pas. Il dit qu'aucun nom de colonne n'a été spécifié pour la colonne 1 de 'max_price' – nathpilland

3
Select datatable.model as price from (
    Select P.model,P.price from PC P where P.price=(select Max(Q.price) from PC Q) 
    Union 
    Select P.model,P.price from Laptop P where P.price=(select Max(Q.price) from Laptop Q) 
    Union 
    Select P.model,P.price from Printer P where P.price=(select Max(Q.price) from Printer Q) 
) as datatable where datatable.price=(
    Select Max(newtable.price) as price from (
     Select P.model,P.price from PC P where P.price=(select Max(Q.price) from PC Q) 
     Union 
     Select P.model,P.price from Laptop P where P.price=(select Max(Q.price) from Laptop Q) 
     Union 
     Select P.model,P.price from Printer P where P.price=(select Max(Q.price) from Printer Q)) as newtable) 
-1
select model from (
Select model, price from pc 
where price = (select max(price) from pc) 
union 
Select model, price from laptop 
where price = (select max(price) from laptop) 

union 
Select model, price from printer 
where price = (select max(price) from printer) 
) as G 
where price = (
select max(price) from (
Select model, price from pc 
where price = (select max(price) from pc) 
union 
Select model, price from laptop 
where price = (select max(price) from laptop) 

union 
Select model, price from printer 
where price = (select max(price) from printer) 
) as T 
) 
0
Select b.model from 
(Select a.model, Max(a.price) as price from 
(Select model, MAX(price) as price from PC group by model 
union 
Select model, MAX(price) as price from Laptop group by model 
union 
Select model, MAX(price) as price from Printer group by model)a 
Group by a.model)b 
where b.price=(Select Max(c.price) from(Select model, MAX(price) as price from PC group by model 
union 
Select model, MAX(price) as price from Laptop group by model 
union 
Select model, MAX(price) as price from Printer group by model)c) 
1

C'est ma solution et il est des œuvres. Je l'ai déjà essayé.

WITH PRICE_MAX AS (select model, price 
from pc 
where price = (select max(price) 
       from pc) 
UNION 
select model, price 
from laptop 
where price = (select max(price) 
       from laptop) 
UNION 
select model, price 
from printer 
where price = (select max(price) 
       from printer)) 

select model from PRICE_MAX 
where price = (select Max(price) 
       from PRICE_MAX) 
Questions connexes