Subject | Select for take first record from tableB for each record in tableA |
---|---|
Author | emb_blaster |
Post date | 2009-03-17T17:04:07Z |
Hi all,
I was searching for a way to achieve the same result
as aggregate function first(column). Example:
I had a Customer table, and a Sales table so I wish to
Select the Sales with Customers that din´t payed the bill :) and
the bill was 30 days old.
Now is the problem, I want only the oldest bill.
So, I´m doing this:
select C.name, SS.*, (current_date - SS.date) as DelayedDays
from customer C
inner join Sales SS on C.ID = SS.IDCustomer
where SS.PKSales = (select first 1 stemp.PKSales
from Sales stemp
where (stemp.PayDate is null) and (current_date - stemp.date > 30 ) and (stemp.IDCustomer = C.ID)
order by (stemp.date))
order by C.name
Using FB 2.0 there´s a way to simplify or optimize this query?
I don´t believe that I´m doing the rigth way this time,
... but still don´t know why ;)
I was searching for a way to achieve the same result
as aggregate function first(column). Example:
I had a Customer table, and a Sales table so I wish to
Select the Sales with Customers that din´t payed the bill :) and
the bill was 30 days old.
Now is the problem, I want only the oldest bill.
So, I´m doing this:
select C.name, SS.*, (current_date - SS.date) as DelayedDays
from customer C
inner join Sales SS on C.ID = SS.IDCustomer
where SS.PKSales = (select first 1 stemp.PKSales
from Sales stemp
where (stemp.PayDate is null) and (current_date - stemp.date > 30 ) and (stemp.IDCustomer = C.ID)
order by (stemp.date))
order by C.name
Using FB 2.0 there´s a way to simplify or optimize this query?
I don´t believe that I´m doing the rigth way this time,
... but still don´t know why ;)