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 6831541
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T22:44:58+00:00 2026-05-26T22:44:58+00:00

I’m trying to order a grouped resultset. Problem is that one of the join

  • 0

I’m trying to order a grouped resultset. Problem is that one of the join conditions references another table. Specifically, I need to select records from table_a that have the highest value within a group, but the group id is a field in a different table.

Below are some of the things I tried, in different variations. Whenever I add GROUP BY pr.id, some results that know should be in the top 3, are excluded. When I do not add it I get more than 1 record per group.

Any help much appreciated.

SELECT * FROM ivalues AS iv

INNER JOIN mprod AS p ON (p.id = iv.p_id)
INNER JOIN contact AS pr ON (pr.id = p.pr_id)


LEFT OUTER JOIN ivalues i2
    ON i2.p_id = p2.id

    AND i2.period = iv.period
    AND i2.current = iv.current
    AND i2.cert = iv.cert
    AND i2.exists = iv.exists
    AND iv.value > i2.value
WHERE 
iv.period = 0
AND iv.current = 1
AND iv.cert = 1
AND p.type_id = 15747
AND iv.exists = 1
AND i2.id IS NULL
GROUP BY pr.id  
ORDER BY iv.value ASC
LIMIT 10;

SELECT * FROM ivalues AS iv

INNER JOIN mprod AS p ON (p.id = iv.p_id)
INNER JOIN contact AS pr ON (pr.id = p.pr_id)

LEFT OUTER JOIN contact pr2 
    ON pr2.id = p.pr_id

LEFT OUTER JOIN mprod p2
    ON p2.type_id = p.type_id           

LEFT OUTER JOIN ivalues i2
    ON i2.p_id = p2.id

    AND i2.period = iv.period
    AND i2.current = iv.current
    AND i2.cert = iv.cert
    AND i2.exists = iv.exists
    AND iv.value > i2.value
WHERE 
iv.period = 0
AND iv.current = 1
AND iv.cert = 1
AND p.type_id = 15747
AND iv.exists = 1
AND i2.id IS NULL

ORDER BY iv.value ASC
LIMIT 10;

SELECT * FROM ivalues AS iv

INNER JOIN mprod AS p ON (p.id = iv.p_id)
INNER JOIN contact AS pr ON (pr.id = p.pr_id)

LEFT OUTER JOIN contact p2 
    ON p2.pr_id = p.pr_id   
    AND p2.type_id = p.type_id

INNER JOIN mprod p2
    ON p2.type_id = p.type_id           

INNER JOIN ivalues i2
    ON i2.p_id = p2.id

    AND i2.period = iv.period
    AND i2.current = iv.current
    AND i2.cert = iv.cert
    AND i2.exists = iv.exists
    AND iv.value > i2.value

WHERE 
iv.period = 0
AND iv.current = 1
AND iv.cert = 1
AND p.type_id = 15747
AND iv.exists = 1

ORDER BY iv.value ASC
LIMIT 3;
  • 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-26T22:44:59+00:00Added an answer on May 26, 2026 at 10:44 pm

    I think you will need to join ivalues and mprod as a subselect, either as an outer join to exclude non-maximum values, or as an inner group to find the maximum value. Hopefully it looks something like this:

    SELECT * FROM contact
      INNER JOIN mprod ON mprod.contact_id = contact.id
      INNER JOIN ivalues ON ivalues.mprod_id = mprod.id
      LEFT OUTER JOIN (
        SELECT mprod.contact_id, ivalues.*
          FROM mprod
          INNER JOIN ivalues ON mprod.id = ivalues.mprod_id
          WHERE ivalues.period = 0
            AND ivalues.current = 1
            AND ivalues.cert = 1
            AND mprod.type_id = 15747
            AND ivalues.exists = 1
      ) AS pv ON pv.contact_id = contact.id AND pv.value > ivalues.value
      WHERE ivalues.period = 0
        AND ivalues.current = 1
        AND ivalues.cert = 1
        AND mprod.type_id = 15747
        AND ivalues.exists = 1
        AND pv.id IS NULL
      GROUP BY pr.id  
      ORDER BY iv.value ASC
      LIMIT 10;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I need a function that will clean a strings' special characters. I do NOT
I'm trying to create an if statement in PHP that prevents a single post
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
Specifically, suppose I start with the string string =hello \'i am \' me And

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.