Skip site navigation (1) Skip section navigation (2)

Re: Lowest 2 items per

From: "Oliveiros d'Azevedo Cristina" <oliveiros(dot)cristina(at)marktest(dot)pt>
To: "Relyea, Mike" <Mike(dot)Relyea(at)xerox(dot)com>
Cc: <pgsql-sql(at)postgresql(dot)org>
Subject: Re: Lowest 2 items per
Date: 2012-06-01 16:28:01
Message-ID: 6C8D39EA43594F12ADF379A91DEECF59@marktestcr.marktest.pt (view raw or flat)
Thread:
Lists: pgsql-sql
Oliver,

I had to make a few grammatical corrections on your query to get it to
run, but once I did it gave me almost correct results.  It leaves out
all of the printer models that only have one consumable with a cost.
Some printers might have more than two black inks and some might have
only one.  Your query only returns those printers that have two or more.

Here's your query with the corrections I had to make
SELECT subquery2.Make, subquery2.Model, subquery2.Color,subquery2.Type,
subquery1.cpp, min(Cost/Yield) as cpp2
FROM(  SELECT Printers.Make, Printers.Model, Consumables.Color,
Consumables.Type, min(Cost/Yield) AS cpp  FROM Printers  JOIN
PrintersAndConsumables ON Printers.PrinterID =
PrintersAndConsumables.PrinterID  JOIN Consumables ON
Consumables.ConsumableID =  PrintersAndConsumables.ConsumableID
 WHERE Consumables.Cost Is Not Null
 AND Consumables.Yield Is Not Null
 GROUP BY Printers.Make, Printers.Model, Consumables.Color,
Consumables.Type
 ) subquery1
 JOIN
 (
 SELECT Printers.Make, Printers.Model, Consumables.Color,
Consumables.Type,Cost,Yield  FROM Printers  JOIN PrintersAndConsumables
ON Printers.PrinterID =  PrintersAndConsumables.PrinterID  JOIN
Consumables ON Consumables.ConsumableID =
PrintersAndConsumables.ConsumableID
 WHERE Consumables.Cost Is Not Null
 AND Consumables.Yield Is Not Null
 ) subquery2
ON (subquery1.Make = subquery2.Make
AND subquery1.Model = subquery2.Model
AND subquery1.Color = subquery2.Color
AND subquery1.Type = subquery2.Type)
 WHERE subquery2.Cost / subquery2.Yield <> subquery1.cpp  GROUP BY
subquery2.Make,subquery2.Model,
subquery2.Color,subquery2.Type,subquery1.cpp
 ORDER BY Make, Model;

* Hello again, Mike,

Thank you for your e-mail.

Yes, you are right, now, thinking about the way I built it, the query, 
indeed, leaves out the corner case of models which have just one
consumable.

I didn't try ur version of the query.
Does itork now with your improvements ?
Or were they only gramatical ?

Best,
Oliver


Mike




In response to

Responses

pgsql-sql by date

Next:From: Relyea, MikeDate: 2012-06-01 16:35:53
Subject: Re: Lowest 2 items per
Previous:From: Relyea, MikeDate: 2012-06-01 16:17:50
Subject: Re: Lowest 2 items per

Privacy Policy | About PostgreSQL
Copyright © 1996-2014 The PostgreSQL Global Development Group