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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T12:08:46+00:00 2026-05-30T12:08:46+00:00

I’ve got a very frustrating SQL issue which i can’t for the life of

  • 0

I’ve got a very frustrating SQL issue which i can’t for the life of me solve with a derived query returning a composite key but also performing a MIN() aggregate function on another field within that table. If i was performing the MIN() on one of the composite keys it would be easy, but since i need to return both keys and perform the MIN() function as well to the outer query i can’t work out how to do this. The entire query looks like this:

SELECT
    p.name as productname
   ,tmp.packageid
   ,tmp.price
   ,ppk2.packageoptionid
   ,ppk2.selcomproductid
FROM ( 
        SELECT ppk.productid, ppk.packageid, MIN(ppk.price) as price
        FROM  product_package ppk
                 INNER JOIN package pk ON ppk.packageid = pk.id
                 INNER JOIN [plan] pl ON pk.planid = pl.id
        WHERE pk.networkid = 1
        GROUP BY ppk.productid, ppk.packageid
) tmp
INNER JOIN product_package ppk2 ON ( 
        ppk2.productid = tmp.productid 
    AND ppk2.packageid = tmp.packageid
)    
INNER JOIN product p ON (p.id = ppk2.productid)  
WHERE p.isenabled = 1;

Current Results:

--------------------------------------
productid   |   packageid   |   price
1               500             0
1               501             19.95
1               502             29.95
2               501             0
3               500             15    
3               504             39.95 

Desired Results:

--------------------------------------
productid   |   packageid   |   price
1               500             0
2               501             0
3               500             15  

The derived query “tmp” is where my issue lies as i need a unique rows back for each product/package combination with the lowest price, before joining onto the outer tables.

Any help would be greatly appreciated!

  • 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-30T12:08:47+00:00Added an answer on May 30, 2026 at 12:08 pm

    Well, I don’t know the data you actually have in your table. I just have the data your query returns. You didn’t answer to my comment asking for a sample of the data of your table and the DBMS you were using.

    However, assuming the current data of your table is the one that comes out of your query, the following query will give you the “Desired Result” you’ve specified:

    select t1.* from t t1
    left join t t2
    on t1.productid = t2.productid and t1.details > t2.details
    where t2.details is null
    

    In table words, the query turns this:

    +-----------+-----------+---------+
    | PRODUCTID | PACKAGEID | DETAILS |
    +-----------+-----------+---------+
    |         1 |       500 |       0 |
    |         1 |       501 |      20 |
    |         1 |       502 |      30 |
    |         2 |       501 |       0 |
    |         3 |       500 |      15 |
    |         3 |       504 |      40 |
    +-----------+-----------+---------+
    

    Into this:

    +-----------+-----------+---------+
    | PRODUCTID | PACKAGEID | DETAILS |
    +-----------+-----------+---------+
    |         1 |       500 |       0 |
    |         2 |       501 |       0 |
    |         3 |       500 |      15 |
    +-----------+-----------+---------+
    

    Let me know if it’s clear or not.

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

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am trying to understand how to use SyndicationItem to display feed which is
I used javascript for loading a picture on my website depending on which small
I have a jquery bug and I've been looking for hours now, I can't
I've got a string that has curly quotes in it. I'd like to replace
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I would like to run a str_replace or preg_replace which looks for certain words
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have a text area in my form which accepts all possible characters from
i got an object with contents of html markup in it, for example: string

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.