Received: from localhost (unknown [200.46.204.183]) by postgresql.org (Postfix) with ESMTP id 44A3D9F937B for ; Tue, 16 Oct 2007 13:53:05 -0300 (ADT) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.183]) (amavisd-maia, port 10024) with ESMTP id 19394-07 for ; Tue, 16 Oct 2007 13:52:55 -0300 (ADT) X-Greylist: delayed 00:06:20.209354 by SQLgrey-1.7.5 Received: from py-out-1112.google.com (py-out-1112.google.com [64.233.166.183]) by postgresql.org (Postfix) with ESMTP id 174069F9375 for ; Tue, 16 Oct 2007 13:52:54 -0300 (ADT) Received: by py-out-1112.google.com with SMTP id u77so4299885pyb for ; Tue, 16 Oct 2007 09:52:53 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=googlemail.com; s=beta; h=domainkey-signature:received:received:message-id:date:from:to:subject:mime-version:content-type; bh=tyMTS3/xZ2F67dqYtHGAn1V8qq+kG6l46CHrSAxoCc0=; b=NCFTDqjZlC21IlnCRgmcBZ9RhAmS2Zx+GAd74vhVpYXB+Nmj0pimetcMvd3RBpe8C0cIUeo5XuP9xLHZXNn6N5+w+zbW80ulb/tRpm5lYifxWDftUic4BNuOKDuKJClvDgOv7kQ5Y8FlPyUKQZGthYGm3tbafesWBVsMa1Hi6Fk= DomainKey-Signature: a=rsa-sha1; c=nofws; d=googlemail.com; s=beta; h=received:message-id:date:from:to:subject:mime-version:content-type; b=F1cPF9AC6JzNdjzGIwoHJ64H+qt6d5VuCBlmz+BswAjkYwAU21aJIf+dJJfzd3ndetNJV4m9DIbKIP9U3UNUCh62ru/ONWVKKRI7o/Q9+pBIIS+jDuU9v4S2l/vWi7TQ5YwQKL2lxQ3DjFV9reDw9y3KAsq+cH8bkrnuxME0ewg= Received: by 10.35.70.17 with SMTP id x17mr9339313pyk.1192553193894; Tue, 16 Oct 2007 09:46:33 -0700 (PDT) Received: by 10.35.49.16 with HTTP; Tue, 16 Oct 2007 09:46:33 -0700 (PDT) Message-ID: Date: Tue, 16 Oct 2007 17:46:33 +0100 From: "Jamie Tufnell" To: pgsql-sql@postgresql.org Subject: Having difficulty writing a "best-fit" query.. MIME-Version: 1.0 Content-Type: multipart/alternative; boundary="----=_Part_3229_4747650.1192553193879" X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200710/70 X-Sequence-Number: 29442 ------=_Part_3229_4747650.1192553193879 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit Content-Disposition: inline Hi list, I have a many-to-many relationship between movies and genres and, in the link table I have a third field called which orders the "appropriateness" of the relations within each movie. For example: movie_id, genre_id, relevance (i've used movie/genre titles for clarity here, but in reality they're id's) -------------------------------------------- beverly hills cop, action, 2 beverly hills cop, comedy, 1 the heartbreak kid, comedy, 2 the heartbreak kid, romance, 1 The above would mean, to my application: "Beverly Hills Cop is both an Action and a Comedy movie but primarily an Action movie." "The Heartbreak Kid is both a Comedy and a Romance movie but primarily a Comedy movie." First of all, if there's a better way to model this kind of ranking/ordering of many-to-many relationships, please let me know. Now, to my problem.. I'm taking a subset of all my genres, and I want to get ONE row for each movie in the subset alongside its most appropriate genre (whichever has the highest relevance). In other words, the best fit. I've had a few goes at writing this query but I can't seem to get it right.. The theory in my mind is to: 1. filter the link table down to rows that fit the subset of categories (easy) 2. filter the link table further to keep only the max(relevance) for each movie_id .. this is where i'm having trouble. If someone can shed some light on this for me, I'd really appreciate it. Thanks for your time, Jamie ------=_Part_3229_4747650.1192553193879 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit Content-Disposition: inline Hi list,

I have a many-to-many relationship between movies and genres and, in the link table I have a third field called which orders the "appropriateness" of the relations within each movie. 

For example:

movie_id, genre_id, relevance (i've used movie/genre titles for clarity here, but in reality they're id's)
--------------------------------------------
beverly hills cop, action, 2
beverly hills cop, comedy, 1
the heartbreak kid, comedy, 2
the heartbreak kid, romance, 1

The above would mean, to my application:
"Beverly Hills Cop is both an Action and a Comedy movie but primarily an Action movie."
"The Heartbreak Kid is both a Comedy and a Romance movie but primarily a Comedy movie."

First of all, if there's a better way to model this kind of ranking/ordering of many-to-many relationships, please let me know.

Now, to my problem..

I'm taking a subset of all my genres, and I want to get ONE row for each movie in the subset alongside its most appropriate genre (whichever has the highest relevance).  In other words, the best fit.

I've had a few goes at writing this query but I can't seem to get it right.. The theory in my mind is to:

1. filter the link table down to rows that fit the subset of categories (easy)

2. filter the link table further to keep only the max(relevance) for each movie_id .. this is where i'm having trouble.

If someone can shed some light on this for me, I'd really appreciate it.

Thanks for your time,
Jamie
------=_Part_3229_4747650.1192553193879--