3

I have a query I can run, which produces rows like this:

 ID | category | property_A | property_B
----+----------+------------+------------
  1 |        X |       tall |        old
  2 |        X |      short |        old
  3 |        X |       tall |        old
  4 |        X |      short |      young
  5 |        Y |      short |        old
  6 |        Y |      short |        old
  7 |        Y |       tall |        old

I'd like to find, for each category and property_B, what is the most common property_A, and put that into another table somewhere for later use. So here I'd like to know that in category X, old people tend to be tall and young people short, while in category Y, old people tend to be short.

The domain of each column is finite, and not too large - there are something like 200 categories, and a dozen or so of property_A and property_B. So I could write a dumb script on my client, which queries the database 200*12*12 times doing a limited query, but that seems like it must be the wrong approach, as well as wasteful given that it's expensive to produce this table and then throw most of it away.

But I don't even know what words to look up to find the right approach: "sql find correlated rows" shows how to find integer correlations, but I'm not interested in integers. So what do I do instead?

2 Answers 2

4

You can readily do this with aggregation and the window/analytic functions. You want the top ranked one by count. The following returns the most popular A:

select category, property_b, property_a as MostPopularA
from (select category, property_b, property_a, count(*) as cnt,
             row_number() over (partition by category, property_b order by count(*) desc) as seqnum
      from table t
      group by category, property_b, property_a
     ) t
where seqnum = 1;

If you want to get all values when there is a tie, then use dense_rank() instead of row_number().

3

I suggest a combination of GROUP BY and DISTINCT ON, which is faster / simpler / more elegant in Postgres:

SELECT DISTINCT ON (category, property_b)
       category, property_b, property_a, count(*) AS ct
FROM   tbl
GROUP  BY category, property_b, property_a
ORDER  BY category, property_b, ct DESC;

Returns:

category | property_b | property_a | ct
---------+------------+------------+----
X        | old        | tall       | 2
X        | young      | short      | 1
Y        | old        | short      | 2

If multiple peers tie for the most common value, only one arbitrary pick is returned.

This works in a single query level without subquery, since aggregation (GROUP BY) is applied before the DISTINCT step. Detailed explanation for DISTINCT ON:
Select first row in each GROUP BY group?

SQL Fiddle.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.