SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 How to get distinct list from two tables?
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

Zman
Starting Member

2 Posts

Posted - 02/28/2014 :  10:03:56  Show Profile  Reply with Quote
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!



Edited by - Zman on 02/28/2014 18:47:44

khtan
In (Som, Ni, Yak)

Singapore
17658 Posts

Posted - 02/28/2014 :  23:05:56  Show Profile  Reply with Quote
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
Time is always against us

Go to Top of Page

Zman
Starting Member

2 Posts

Posted - 03/01/2014 :  00:19:37  Show Profile  Reply with Quote
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;



Edited by - Zman on 03/01/2014 00:22:43
Go to Top of Page

khtan
In (Som, Ni, Yak)

Singapore
17658 Posts

Posted - 03/01/2014 :  00:31:54  Show Profile  Reply with Quote
Good for you


KH
Time is always against us

Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.08 seconds. Powered By: Snitz Forums 2000