Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 366309
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T13:37:13+00:00 2026-05-12T13:37:13+00:00

Simplified table structures, all INT columns and no PKs outside of the identity columns:

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T13:37:14+00:00Added an answer on May 12, 2026 at 1:37 pm

    This question is a variation of the “greatest-n-per-group” problem, but you’re looking for the least instead of the greatest, and your criteria are in a lookup table (Type) instead of the principle table (Attributes).

    So you want the rows (a1) from Attributes such that no other row with the same node_id is associated with a lower priority.

    SELECT a1.*
    FROM Attributes a1 INNER JOIN Type t1 ON (a1.type_id = t1.id)
    LEFT OUTER JOIN (
      (Attributes a2 INNER JOIN Type t2 ON (a2.type_id = t2.id))
      ON (a1.node_id = a2.node_id AND t1.priority > t2.priority)
    WHERE a2.node_id IS NULL;
    

    Note that this can result in ties. You haven’t described how you would resolve ties if two Attributes referenced Types with the same priority. In other words, in the following examples, which attributes should be selected?

    a1 n1 t1 p0 
    a2 n1 t1 p0 
    a3 n2 t2 p1 
    a4 n2 t3 p1 
    

    PS: I hope you don’t mind I added the “greatest-n-per-group” tag to your question. Click that tag to see other questions on SO that I have tagged similarly.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

My (simplified) table consist of an Id int identity(1,1), File varchar(20), FileProcessed bit The
I have a (simplified) table consisting of three columns: id INT PRIMARY KEY NOT
I have a table with the following (simplified) structure: INT id, INT type, INT
I have two tables with the following (simplified) structures: table "Factors" which holds data
I have a table with the following columns (I simplified it a little): PRODUCT_name
Here is a simplified table structure: TABLE products ( product_id INT (primary key, auto_increment),
I have a table structure, simplified like so: period_id int suite_id varchar(20) suite_status varchar(50)
Simplified Table structure: CREATE TABLE IF NOT EXISTS `hpa` ( `id` bigint(15) NOT NULL
Here is the simplified table structure of PRICES : SKU PriceType FromDate ToDate Price
I have no control over database schema and have the following (simplified) table structure:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.