Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Data Bases > Microsoft SQL Server > Re: select pare...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 3 of 3 Topic 10979 of 11517
Post > Topic >>

Re: select parent records that do not have a particular child

by d-42 <db.porsche@[EMAIL PROTECTED] > Apr 18, 2008 at 03:52 PM

On Apr 18, 7:13 am, Ed Murphy <emurph...@[EMAIL PROTECTED]
> wrote:
> d-42 wrote:
> > I want to find all the people who belong to a particular account, who
> > are not associated with a particular location.
> [snip]
> > SELECT
> >         [t0].[personid] AS [personid],
> >         [t0].[namefirst] AS [namefirst],
> >         [t0].[accountid] AS [accountid],
> >         [t1].[locationid] AS [locationid]
> > FROM [dbo].[Persons] AS [t0]
> > LEFT OUTER JOIN
> >        (SELECT [t2].[personid], [t2].[locationid] FROM [dbo].
> > [PersonLocation] AS [t2]
> >                WHERE [t2].[locationid]=@[EMAIL PROTECTED]
)
> >        AS [t1] ON [t0].[personid] = [t1].[personid]
> > WHERE
> >         ([t0].[accountid] = @[EMAIL PROTECTED]
) AND
> >         ([t1].[locationid] IS NULL)
>
> > This appears to work, but is it the best way?
>
> The following syntax allows you to say what you really mean:
>
> select  personid, namefirst
> from    Persons t0
> where   accountid = @[EMAIL PROTECTED]
>   and   not exists (
>                 select  *
>                 from    PersonLocation t2
>                 where   t2.personid = t0.personid
>                   and   t2.locationid = @[EMAIL PROTECTED]
>         )

Thank you, yes, that is much more succint.

Unfortunately I can't seem to express this in linq (no 'exists'
keyword), but it has led me to a better way of expressing it in linq
than I was:

 var q2 = from p in Persons
             where p.account == accid
             where !(from x in PersonDistLocation
                        where x.DistLocationID == distlocationid
                        select x.PersonID).Contains(p.PersonID)
             select p;

which is much more readable than the linq I had, and its analogous to
what you've given me.
(linq is still using an outer join though, and I'm hoping the
performance is equivalent.)

Thanks,
Dave
 




 3 Posts in Topic:
select parent records that do not have a particular child
d-42 <db.porsche@[EMAI  2008-04-18 05:55:13 
Re: select parent records that do not have a particular child
Ed Murphy <emurphy42@[  2008-04-18 07:13:36 
Re: select parent records that do not have a particular child
d-42 <db.porsche@[EMAI  2008-04-18 15:52:39 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Wed Dec 3 1:02:45 CST 2008.