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.
Author |
Topic |
yosiasz
Master Smack Fu Yak Hacker
1635 Posts |
Posted - 2012-04-12 : 15:18:32
|
Greetings,Given the following data how can one go about trying to figure out only books have five terminologies in common .If less not interesteddeclare @books TABLE(book_id int, descr nvarchar(50))insert into @books SELECT 1, 'Gone with the wind'UNION ALLSELECT 2, 'Mars'UNION ALLSELECT 3, 'Do Androids Dream of Electric Sheep?'UNION ALLSELECT 4, 'Ender''s Game'UNION ALLSELECT 5, 'Dune'UNION ALLSELECT 6, 'Foundation'UNION ALLSELECT 7, 'Hitch Hiker''s Guide to the Galaxy'UNION ALLSELECT 8, '1984'UNION ALLSELECT 9, 'Star Wars'UNION ALLSELECT 10, 'Battlestar Galactica'UNION ALLSELECT 11, 'Fringe'UNION ALLSELECT 13, 'Prometheus'UNION ALLSELECT 14, 'Prometheus VI'UNION ALLSELECT 15, 'Contagion'UNION ALLSELECT 16, 'Deep Blue'UNION ALLSELECT 16, 'Hunger Games'--SELECT * FROM @booksdeclare @terminologies TABLE(terminology_id int, descr nvarchar(50))INSERT INTO @terminologies SELECT 1, 'Futuristic'UNION ALLSELECT 2, 'Propulsion'UNION ALLSELECT 3, 'Guidance'UNION ALLSELECT 4, 'Life Support'UNION ALLSELECT 5, 'Cabin Structure'UNION ALLSELECT 6, 'Communications'UNION ALLSELECT 7, 'Thermal Protection'UNION ALLSELECT 8, 'Displays And Controls'UNION ALLSELECT 9, 'Space Craft'UNION ALLSELECT 10, 'Light Speed'UNION ALLSELECT 11, 'Warp Speed'UNION ALLSELECT 12, 'Jedi'UNION ALLSELECT 13, 'Force is weak with you rookie Jedi'UNION ALLSELECT 14, 'UnCivil War'--SELECT * FROM @terminologiesDECLARE @book_terminologies TABLE(book_id int, terminology_id int)INSERT INTO @book_terminologies ( book_id, terminology_id )SELECT 1, 1UNION ALLSELECT 1, 2UNION ALLSELECT 1, 3UNION ALLSELECT 1, 4UNION ALLSELECT 1, 5UNION ALLSELECT 1, 6UNION ALLSELECT 1, 7UNION ALLSELECT 1, 8UNION ALLSELECT 1, 9UNION ALLSELECT 1, 10UNION ALLSELECT 2, 1UNION ALLSELECT 2, 2UNION ALLSELECT 2, 3UNION ALLSELECT 2, 4UNION ALLSELECT 2, 5UNION ALLSELECT 2, 6UNION ALLSELECT 2, 7UNION ALLSELECT 2, 8UNION ALLSELECT 2, 9UNION ALLSELECT 2, 10UNION ALLSELECT 3, 1UNION ALLSELECT 3, 2UNION ALLSELECT 3, 3UNION ALLSELECT 3, 4UNION ALLSELECT 3, 5SELECT b.descr, b.book_id, t.descr, t.terminology_id FROM @books b inner join @book_terminologies bt on b.book_id = bt.book_id inner join @terminologies t on t.terminology_id = bt.terminology_idorder by b.book_id, t.terminology_id <><><><><><><><><><><><><><><><><>If you don't have the passion to help people, you have no passion |
|
Bustaz Kool
Master Smack Fu Yak Hacker
1834 Posts |
Posted - 2012-04-12 : 19:01:11
|
[CODE];with BooksWithSameTerminologyas ( select bt1.book_id, bt1.terminology_id, bt2.book_id book_id2 from @book_terminologies bt1 inner join @book_terminologies bt2 on bt1.book_id < bt2.book_id and bt1.terminology_id = bt2.terminology_id ),BooksWithFiveMatchesas ( select book_id, book_id2, count(*) kount from BooksWithSameTerminology group by book_id, book_id2 having count(*) >= 5 )select b1.descr, b2.descr, bwfm.kountfrom @books b1inner join BooksWithFiveMatches bwfm on bwfm.book_id = b1.book_idinner join @books b2 on bwfm.book_id2 = b2.book_id[/CODE]HTH=================================================There is a foolish corner in the brain of the wisest man. -Aristotle, philosopher (384-322 BCE) |
 |
|
LoztInSpace
Aged Yak Warrior
940 Posts |
Posted - 2012-04-13 : 02:33:09
|
I can't help feeling I'm missing something:SELECT b1.book_id, b2.book_id, COUNT(*) FROM @books b1 inner join @book_terminologies t1 on b1.book_id = t1.book_id inner join @book_terminologies t2 on t1.terminology_id = t2.terminology_id inner join @books b2 on b2.book_id = t2.book_idwhere b1.book_id<>b2.book_id group by b1.book_id,b2.book_idhaving COUNT(*)>=5 Note this assumes a distinct set of terminologies per book. If that's not the case we'd have to sub-select distinct terminologies instead of using the table directly:SELECT b1.book_id, b2.book_id, COUNT(*) FROM @books b1 inner join (select distinct * from @book_terminologies) t1 on b1.book_id = t1.book_id inner join (select distinct * from @book_terminologies) t2 on t1.terminology_id = t2.terminology_id inner join @books b2 on b2.book_id = t2.book_idwhere b1.book_id<>b2.book_id group by b1.book_id,b2.book_idhaving COUNT(*)>=5 |
 |
|
yosiasz
Master Smack Fu Yak Hacker
1635 Posts |
Posted - 2012-04-13 : 02:43:41
|
this is looking very nice BustazLet me try it out. I assume you mean to say <> instead of>quote: Originally posted by Bustaz Kool [CODE];with BooksWithSameTerminologyas ( select bt1.book_id, bt1.terminology_id, bt2.book_id book_id2 from @book_terminologies bt1 inner join @book_terminologies bt2 on bt1.book_id < bt2.book_id and bt1.terminology_id = bt2.terminology_id ),BooksWithFiveMatchesas ( select book_id, book_id2, count(*) kount from BooksWithSameTerminology group by book_id, book_id2 having count(*) >= 5 )select b1.descr, b2.descr, bwfm.kountfrom @books b1inner join BooksWithFiveMatches bwfm on bwfm.book_id = b1.book_idinner join @books b2 on bwfm.book_id2 = b2.book_id[/CODE]HTH=================================================There is a foolish corner in the brain of the wisest man. -Aristotle, philosopher (384-322 BCE)
<><><><><><><><><><><><><><><><><>If you don't have the passion to help people, you have no passion |
 |
|
Bustaz Kool
Master Smack Fu Yak Hacker
1834 Posts |
Posted - 2012-04-13 : 12:05:10
|
quote: I assume you mean to say <> instead of>
Where?=================================================There is a foolish corner in the brain of the wisest man. -Aristotle, philosopher (384-322 BCE) |
 |
|
yosiasz
Master Smack Fu Yak Hacker
1635 Posts |
Posted - 2012-04-13 : 12:07:55
|
on bt1.book_id < bt2.book_id<><><><><><><><><><><><><><><><><>If you don't have the passion to help people, you have no passion |
 |
|
Bustaz Kool
Master Smack Fu Yak Hacker
1834 Posts |
Posted - 2012-04-13 : 12:17:40
|
The less than is there in order to impose a hierarchy on the books. Without it you'll get book X matching book Y AND book Y matching book X. If you want both, you're right to correct it to a not equal comparison.=================================================There is a foolish corner in the brain of the wisest man. -Aristotle, philosopher (384-322 BCE) |
 |
|
yosiasz
Master Smack Fu Yak Hacker
1635 Posts |
Posted - 2012-04-13 : 12:29:15
|
aha got it! Thanks BK<><><><><><><><><><><><><><><><><>If you don't have the passion to help people, you have no passion |
 |
|
|
|
|
|
|