Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 How to get distinct list from two tables?

Author  Topic 

Zman
Starting Member

2 Posts

Posted - 2014-02-28 : 10:03:56
Dear SQL gurus,

I am trying to write some script and can not figure out a (supposedly simple) trick, please help!



// C - Categories, P - Parameters, CP_Link - linkage table between C and P
// Basically, it is a list of features belonging to a particular category.

DROP TABLE [C];
CREATE TABLE [C] (
ID_C Autoinc,
Name varchar(32),
PRIMARY KEY( ID_C )
);

DROP TABLE [P];
CREATE TABLE [P] (
ID_P Autoinc,
Name varchar(32),
PRIMARY KEY( ID_P )
);

DROP TABLE [CP_Link];
CREATE TABLE [CP_Link] (
ID Autoinc,
FK_ID_C integer, // foreign keys
FK_ID_P integer,
PRIMARY KEY( ID )
);

INSERT INTO [C] ( Name ) VALUES ( 'boys' ); // 1
INSERT INTO [C] ( Name ) VALUES ( 'girls' ); // 2
INSERT INTO [C] ( Name ) VALUES ( 'snakes' ); // 3


INSERT INTO [P] ( Name ) VALUES ( 'leg' ); // 1
INSERT INTO [P] ( Name ) VALUES ( 'hand' ); // 2
INSERT INTO [P] ( Name ) VALUES ( 'beard' ); // 3
INSERT INTO [P] ( Name ) VALUES ( 'tail' ); // 4


INSERT INTO [CP_Link] ( FK_ID_C, FK_ID_P ) VALUES ( 1,1 ); // boys
INSERT INTO [CP_Link] ( FK_ID_C, FK_ID_P ) VALUES ( 1,2 );
INSERT INTO [CP_Link] ( FK_ID_C, FK_ID_P ) VALUES ( 1,3 );

INSERT INTO [CP_Link] ( FK_ID_C, FK_ID_P ) VALUES ( 2,1 ); // girls
INSERT INTO [CP_Link] ( FK_ID_C, FK_ID_P ) VALUES ( 2,2 );

INSERT INTO [CP_Link] ( FK_ID_C, FK_ID_P ) VALUES ( 3,4 ); // snakes


//-------------------------------------------------------


SELECT P.Name
FROM C,P,CP_Link
WHERE (FK_ID_C=ID_C) AND (FK_ID_P=ID_P) AND (C.Name='girls');
// this will result in:
// leg
// hand
// we have category 'girls' selected, which has two features linked. All works fine.

// now a bit more complex:
SELECT DISTINCT P.Name
FROM C,P,CP_Link
WHERE (FK_ID_C=ID_C) AND (FK_ID_P=ID_P) AND (C.Name IN ('boys','girls'));
// so we have more than one category as input parameter, this will result in:
// leg
// hand
// beard
// so this is a joint list of all features listed.



OK, this works fine, but I want to get a list of parameters (from P table) which holds only those items that linked to all categories requested. In other words, I want only 'leg' and 'hand', but not the 'beard', because 'girls' not linked to 'beard'.
Only common features for all of categories listed "IN (,,,)" wanted.

thank you!


khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2014-02-28 : 23:05:56
something like this ?
SELECT 	P.Name 
FROM C
INNER JOIN CP_Link L ON L.FK_ID_C = C.ID_C
INNER JOIN P ON L.FK_ID_P = P.ID_P
WHERE C.Name IN ('boys','girls')
GROUP BY P.Name
HAVING COUNT(*) = 2


by the way, this is a Microsoft SQL Server forum. Obviously you are not using MS SQL Server. You can try your luck at dbforums.com if this is not what you want


KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

Zman
Starting Member

2 Posts

Posted - 2014-03-01 : 00:19:37
khtan, thank you! this was a generic question rather than MS SQL related

here is how it eventually worked for me (a bit shorter)


SELECT p.name
FROM p, cp_link
WHERE cp_link.fk_id_p = p.id_p
AND cp_link.fk_id_c IN ( 1,2 )
GROUP BY p.name
HAVING Count(p.id_p) = 2;


Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2014-03-01 : 00:31:54
Good for you


KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page
   

- Advertisement -