Subject Re: First 1 and order by in subselect
Author Svein Erling Tysvær
--- In firebird-support@yahoogroups.com, Helen Borrie wrote:
> >select
> > CAMPAIGN_NAME,
> > (
> > SELECT
> > first 1 EE_SP_APPROVAL.APPROVAL_TYPE
> > FROM
> > EE_LNK_SHEDULE_APPROVAL
> > LEFT OUTER JOIN EE_SP_APPROVAL ON
> >(EE_LNK_SHEDULE_APPROVAL.EE_SP_APPROVAL_ID = EE_SP_APPROVAL.ID)
> > WHERE
> > (
> > (EE_LNK_SHEDULE_APPROVAL.EE_SP_SCHEDULE_ID =
> >EE_SP_SCHEDULE1.ID)
> > )
> > ORDER BY EE_LNK_SHEDULE_APPROVAL.created desc /*WORKS
> >IF TAKEN OUT*/
> > ) APPROVAL_TYPE
> >from
> > EE_SP_SCHEDULE EE_SP_SCHEDULE1
>
> Do this:
>
> select
> ess1.CAMPAIGN_NAME,
> (
> SELECT max(elsa.EE_SP_APPROVAL.APPROVAL_TYPE)
> FROM EE_LNK_SHEDULE_APPROVAL elsa
> LEFT JOIN EE_SP_APPROVAL esa ON
> elsa.EE_SP_APPROVAL_ID = esa.ID
> where elsa.EE_SP_SCHEDULE_ID = ess1.ID)
> as APPROVAL_TYPE
> from
> EE_SP_SCHEDULE EE_SP_SCHEDULE1 ess1

I think this is a different query, Helen. He seems to want
esa.APPROVAL_TYPE for the record with max(elsa.created). If the record
with max(elsa.created) doesn't match a record in esa, then I think he
wants the field to return null.

Set