Groups | Blog | Home
all groups > sql server data mining > january 2004 >

sql server data mining : Problem with a SQL Statement


Atley
1/31/2004 12:58:07 AM
I have three tables:

Products Table
PID
PName
PType

Linking Table
LID
PID
CID

Customers Table
CID
CName
CPhone


I am trying to make a single statement that will give me all the customers
that haven't bought a certain single product so i can make a list of people
to contact regarding that product.

Any help or suggestions would be greatly appreciated... I just can't seem to
get this one right.

@

Dave Nettleton [MSFT]
2/1/2004 10:37:16 AM
Hi,
the following query should achieve what you want:

select distinct pname, cname from linking as link
inner join product as p on link.pid = p.pid
inner join customers as c on link.cid = c.cid
order by pname,cname;

the distinct is optional but removes duplicates if someone has bought the
same item twice. Below is some data I tried it on. The resulsts are:

pname cname
----------------------------------------
chair fred
football george
table fred
table george

Can easilly add a where clause if you are looking for a particular pname.

Thanks
Dave.

This posting is provided "AS IS" with no warranties, and confers no rights.

create table product (pid int primary key, pname nvarchar(10), ptype int);
create table customers (cid int primary key, cname nvarchar(10), cphone
nvarchar(12));
create table linking (lid int, pid int references product(pid), cid int
references customers(cid));

insert into product values (1, 'table', 1);
insert into product values (2, 'chair', 1);
insert into product values (3, 'football', 2);
insert into customers values (1, 'fred', '123456');
insert into customers values (2, 'george', '654321');

insert into linking values (1,1,1);
insert into linking values (2,2,1);
insert into linking values (3,3,2);
insert into linking values (4,1,1);
insert into linking values (5,1,2);


[quoted text, click to view]

AddThis Social Bookmark Button