Simplified table structures, all INT columns and no PKs outside of the identity columns:
Nodes (n) table: id
Attributes (a) table: id
, node_id
, type_id
Type (t) table: id
, priority
I'm trying to select a set of attributes, each of which has the lowest type.priority for its respective node. Though there are multiple attributes per node_id
, I only want to select the one with the lowest priority value:
a1 n1 t1 p0 *
a2 n1 t2 p1
a3 n2 t2 p1 *
a4 n2 t3 p2
This is the basic query that I'm working from, at which point I'm also getting stuck:
SELECT *
FROM a
LEFT JOIN t ON a.type_id = t.id
GROUP BY node_id
My first thought was to use an aggregate, MIN, but I'm then having problems matching up the lowest priority for a node_id with the correct attribute.