Subject | Re: How do I write query Part 2 |
---|---|
Author | Svein Erling Tysvær |
Post date | 2005-06-02T14:13:45Z |
I guess this is due to there being more than one matching record in
either BUS_VEN or ENQUIRES, the join approach you've taken means that
you get one row for each matching record in BD multiplied by the
number of matching records in BV and EQ. Try using either
Select DISTINCT BD.DetectId, BD.BUS_NAME, BD.OURREF
From BUSDETAILS BD
join BUS_VEN BV on (BV.HEADID = BD.DETECTID)
join ENQUIRES EQ on (EQ.HEADID = BV.DETECTID)
Where EQ.DATE_ENQUIRED between :D1 and :D2
Order By 2
or
Select BD.DetectId, BD.BUS_NAME, BD.OURREF
From BUSDETAILS BD
Where Exists(
Select *
From BUS_VEN BV
join ENQUIRES EQ on (EQ.HEADID = BV.DETECTID)
Where EQ.DATE_ENQUIRED between :D1 and :D2
And BV.HEADID = BD.DETECTID)
Order By 2
The only difference between the two is that the second will contain
duplicates if several records of BD have the same DetectId, BUS_NAME
and OURREF.
HTH,
Set
either BUS_VEN or ENQUIRES, the join approach you've taken means that
you get one row for each matching record in BD multiplied by the
number of matching records in BV and EQ. Try using either
Select DISTINCT BD.DetectId, BD.BUS_NAME, BD.OURREF
From BUSDETAILS BD
join BUS_VEN BV on (BV.HEADID = BD.DETECTID)
join ENQUIRES EQ on (EQ.HEADID = BV.DETECTID)
Where EQ.DATE_ENQUIRED between :D1 and :D2
Order By 2
or
Select BD.DetectId, BD.BUS_NAME, BD.OURREF
From BUSDETAILS BD
Where Exists(
Select *
From BUS_VEN BV
join ENQUIRES EQ on (EQ.HEADID = BV.DETECTID)
Where EQ.DATE_ENQUIRED between :D1 and :D2
And BV.HEADID = BD.DETECTID)
Order By 2
The only difference between the two is that the second will contain
duplicates if several records of BD have the same DetectId, BUS_NAME
and OURREF.
HTH,
Set
--- In firebird-support@yahoogroups.com, Grant Brown wrote:
> Hi guys and gals,
>
> I did what you said but I ended up with 3 records when I should have
> only received 1
>
> ENQUIRES has 3 records in it, but only 1 record matches the date
> span.
>
> What did I do wrong ?
>
> Select BD.DetectId, BD.BUS_NAME, BD.OURREF
> From BUSDETAILS BD
> join BUS_VEN BV on (BV.HEADID = BD.DETECTID)
> join ENQUIRES EQ on (EQ.HEADID = BV.DETECTID)
> Where EQ.DATE_ENQUIRED between :D1 and :D2
> Order By 2
>
> --
> Regards,
> Grant Brown
>
> Product Development Manager
> Phone : 02 4229 1185
> Mobile : 0412 926 995
> Email : grant@s...
> Web : www.sitedoc.com.au
>
> SiteDoc - Easy to Use - Powerful Results