Subject Re: [firebird-support] SQL headache
Author Arno Brinkman
Hi,

> Say there is a sales order number 93, the SOL sales order lines are for
> 2 keyboards, 1 mouse, 30 cdroms. I need to know all other sales orders
> which have the same, and only those, products.

I doubt you'll find an easy and cheap query:

Try this:

SELECT
sol.OrderNumber,
sol.ProductID,
Count(*)
FROM
SalesOrderLines sol
WHERE
(SELECT Count(sol2.ProductID) FROM SalesOrderLines sol2
WHERE sol2.OrderNumber = sol.OrderNumber) =
(SELECT Count(sol3.ProductID) FROM SalesOrderLines sol3
WHERE sol3.OrderNumber = 93)
HAVING
Count (*) = (SELECT Count(sol4.ProductID)
FROM SalesOrderLines sol4
WHERE sol4.OrderNumber = 93 and
sol4.ProductID = sol.ProductID)
GROUP BY
sol.OrderNumber,
sol.ProductID

A index on (OrderNumber) and on (OrderNumber, ProductID) will help.

Regards,
Arno Brinkman
ABVisie

-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
Firebird open source database (based on IB-OE) with many SQL-99 features :
http://www.firebirdsql.org
http://www.firebirdsql.info
http://www.fingerbird.de/
http://www.comunidade-firebird.org/

Support list for Interbase and Firebird users :
firebird-support@yahoogroups.com

Nederlandse firebird nieuwsgroep :
news://newsgroups.firebirdsql.info