Subject | Re: [firebird-support] Help with SQL |
---|---|
Author | Svein Erling Tysvaer |
Post date | 2007-02-19T19:29:36Z |
Of course it is possible to do this without two queries, though if your
tables are big, I do not guarantee decent speed.
SELECT * FROM ORDER O1
WHERE O1.customer_id IN (SELECT FIRST 25 C.customer_id FROM customer C)
AND NOT EXISTS(SELECT * FROM ORDERS 02
JOIN ORDERS O3 ON O2.customer_id = O3.customer_id
WHERE O1.customer_id = O2.customer_id
and O1.order_date < O2.order_date
and O2.order_date < O3.order_date)
I did not bother to change what you said worked OK (the IN part), and
assumed that no-one placed more than one order on the same date.
HTH,
Set
Fabio Gomes wrote:
tables are big, I do not guarantee decent speed.
SELECT * FROM ORDER O1
WHERE O1.customer_id IN (SELECT FIRST 25 C.customer_id FROM customer C)
AND NOT EXISTS(SELECT * FROM ORDERS 02
JOIN ORDERS O3 ON O2.customer_id = O3.customer_id
WHERE O1.customer_id = O2.customer_id
and O1.order_date < O2.order_date
and O2.order_date < O3.order_date)
I did not bother to change what you said worked OK (the IN part), and
assumed that no-one placed more than one order on the same date.
HTH,
Set
Fabio Gomes wrote:
> Hi guys,
>
> I have 2 tables, order and customer, i want to get a list with the
> last 2 orders of each customer, but also (for pagination) I want to
> get just 25 customers, how can i do it? So far I tried this:
>
> SELECT * FROM order WHERE customer_id IN (SELECT FIRST 25 customer_id
> FROM customer)
>
> it works, but returns all the orders from my 25 customers, but i want
> just 2 orders from each customer...
>
> if i use: SELECT FIRST 2 * FROM order
>
> It will just give me the last 2 orders, but i want to get 2 orders for
> each customer.
>
> Is there a way to do it without writing 2 queries?
>
> Thanx in advance.
>
> --
> Fabio Gomes