Home All Groups Group Topic Archive Search About

Should be a simple select...



Author
17 Apr 2007 11:42 PM
David Thielen
Hi;

Ok, this should be an easy select but everything I have tried has not worked.

I have 2 tables with a PK:FK relationship. So Customer.customerId =
Purchase.customerId. I need all rows in Customer where they have zero
purchases. So all rows in the Customer table where there is no table in
Purchase with their customerId.

Any suggestions?

--
thanks - dave
david_at_windward_dot_net
http://www.windwardreports.com

Cubicle Wars - http://www.windwardreports.com/film.htm

Author
18 Apr 2007 1:16 AM
Greg Larsen
Here is something that should work for you. This example uses the
AdventureWorks database.

select * from AdventureWorks.Sales.Customer C
left outer join AdventureWorks.Sales.SalesOrderHeader H
on C.CustomerID = H.CustomerID
where H.CustomerID is null
--
If you are looking for SQL Server examples check out my Website at
http://www.geocities.com/sqlserverexamples


Show quoteHide quote
"David Thielen" wrote:

> Hi;
>
> Ok, this should be an easy select but everything I have tried has not worked.
>
> I have 2 tables with a PK:FK relationship. So Customer.customerId =
> Purchase.customerId. I need all rows in Customer where they have zero
> purchases. So all rows in the Customer table where there is no table in
> Purchase with their customerId.
>
> Any suggestions?
>
> --
> thanks - dave
> david_at_windward_dot_net
> http://www.windwardreports.com
>
> Cubicle Wars - http://www.windwardreports.com/film.htm
>
>
Are all your drivers up to date? click for free checkup

Author
18 Apr 2007 2:40 AM
David Thielen
Tried that - returned nothing. I think that select first gets just the ones
where there is a purchase and so none of them are null.

--
thanks - dave
david_at_windward_dot_net
http://www.windwardreports.com

Cubicle Wars - http://www.windwardreports.com/film.htm




Show quoteHide quote
"Greg Larsen" wrote:

> Here is something that should work for you. This example uses the
> AdventureWorks database.
>
> select * from AdventureWorks.Sales.Customer C
> left outer join AdventureWorks.Sales.SalesOrderHeader H
> on C.CustomerID = H.CustomerID
> where H.CustomerID is null
> --
> If you are looking for SQL Server examples check out my Website at
> http://www.geocities.com/sqlserverexamples
>
>
> "David Thielen" wrote:
>
> > Hi;
> >
> > Ok, this should be an easy select but everything I have tried has not worked.
> >
> > I have 2 tables with a PK:FK relationship. So Customer.customerId =
> > Purchase.customerId. I need all rows in Customer where they have zero
> > purchases. So all rows in the Customer table where there is no table in
> > Purchase with their customerId.
> >
> > Any suggestions?
> >
> > --
> > thanks - dave
> > david_at_windward_dot_net
> > http://www.windwardreports.com
> >
> > Cubicle Wars - http://www.windwardreports.com/film.htm
> >
> >
Author
18 Apr 2007 3:57 AM
Steven Cheng[MSFT]
Hi Dave,

Another simple way is first get the distinct customerID list in your FK
table and then query the PK table and filtering those rows matches the ones
in that customerID list from FK table. e.g.

============
select CustomerID, AccountNumber from Sales.Customer where CustomerID not
in (select DISTINCT CustomerID from Sales.SalesOrderHeader)
============

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead


This posting is provided "AS IS" with no warranties, and confers no rights.
Author
18 Apr 2007 4:48 AM
David Thielen
As always brillinat - thanks. (And tell MS to transfer you to Redmond and put
you on a dev team.)

--
thanks - dave
david_at_windward_dot_net
http://www.windwardreports.com

Cubicle Wars - http://www.windwardreports.com/film.htm




Show quoteHide quote
"Steven Cheng[MSFT]" wrote:

> Hi Dave,
>
> Another simple way is first get the distinct customerID list in your FK
> table and then query the PK table and filtering those rows matches the ones
> in that customerID list from FK table. e.g.
>
> ============
> select CustomerID, AccountNumber from Sales.Customer where CustomerID not
> in (select DISTINCT CustomerID from Sales.SalesOrderHeader)
> ============
>
> Sincerely,
>
> Steven Cheng
>
> Microsoft MSDN Online Support Lead

>
> This posting is provided "AS IS" with no warranties, and confers no rights.
>
>
>
Author
18 Apr 2007 5:57 AM
Uri Dimant
David
>> select CustomerID, AccountNumber from Sales.Customer where CustomerID not
>> in (select DISTINCT CustomerID from Sales.SalesOrderHeader)

Make sure that CustomerID in  Sales.SalesOrderHeader does not allow NULL's ,
otherwise you are about to get a wrong result

Show quoteHide quote
"David Thielen" <thielen@nospam.nospam> wrote in message
news:1D10381F-07BD-46BD-AD2D-B6DE08380D1D@microsoft.com...
> As always brillinat - thanks. (And tell MS to transfer you to Redmond and
> put
> you on a dev team.)
>
> --
> thanks - dave
> david_at_windward_dot_net
> http://www.windwardreports.com
>
> Cubicle Wars - http://www.windwardreports.com/film.htm
>
>
>
>
> "Steven Cheng[MSFT]" wrote:
>
>> Hi Dave,
>>
>> Another simple way is first get the distinct customerID list in your FK
>> table and then query the PK table and filtering those rows matches the
>> ones
>> in that customerID list from FK table. e.g.
>>
>> ============
>> select CustomerID, AccountNumber from Sales.Customer where CustomerID not
>> in (select DISTINCT CustomerID from Sales.SalesOrderHeader)
>> ============
>>
>> Sincerely,
>>
>> Steven Cheng
>>
>> Microsoft MSDN Online Support Lead
>>
>>
>> This posting is provided "AS IS" with no warranties, and confers no
>> rights.
>>
>>
>>

Bookmark and Share