Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

do my sql homework

Name: Anonymous 2008-01-29 1:17

you've got an entity called dude
dude likes porn
your task is to find for every dude a second dude whose taste in porn matches the most in percent
the resulting table would look like this:

dude1 dude2 match
    1     2   100%
.
.
.

shit requires some serious thinking
you're not allow to create own tables in the database but you can use as many views as you feel like

Name: Anonymous 2008-01-29 8:27

Hmm...

create table dude_porn (dude_id integer, porn_id integer);
insert into dude_porn values (1,1),(1,2),(1,3),(2,2),(2,3),(3,1),(3,3),(4,2),(5,null),(6,null);
create view dude_porn_count as select dude_id, count(porn_id) from dude_porn group by dude_id;
select distinct a.dude_id as dude1,b.dude_id as dude2,100-100*abs(ac.count-bc.count)/(select max(count) from dude_porn_count) as match from dude_porn a, dude_porn b, dude_porn_count ac, dude_porn_count bc where a.dude_id=ac.dude_id and b.dude_id=bc.dude_id and a.dude_id<b.dude_id order by match desc;

 dude1 | dude2 | match
-------+-------+-------
     2 |     3 |   100
     5 |     6 |   100
     1 |     2 |    67
     1 |     3 |    67
     2 |     4 |    67
     3 |     4 |    67
     4 |     5 |    67
     4 |     6 |    67
     1 |     4 |    34
     2 |     5 |    34
     2 |     6 |    34
     3 |     5 |    34
     3 |     6 |    34
     1 |     5 |     0
     1 |     6 |     0

Seems to produce the results you're looking for, although I'm sure it could be optimized.

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List