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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T07:37:16+00:00 2026-05-29T07:37:16+00:00

Here is a simplified version of my table: group price spec a 1 .

  • 0

Here is a simplified version of my table:

group price spec
a     1     .
a     2     ..
b     1     ...
b     2
c     .
.     .
.     .

I’d like to produce a result like this: (I’ll refer to this as result_table)

price_a |spec_a |price_b |spec_b |price_c ...|total_cost
1       |.      |1       |..     |...        |
(min)            (min)                        =1+1+...

Basically I want to:

  1. select the rows containing the min price within each group
  2. combine columns into a single row

I know this can be done using several queries and/or combined with some non-sql processing on the results, but I suspect that there maybe better solutions.

The reason that I want to do task 2 (combine columns into a single row)
is because I want to do something like the following with the result_table:

select *,
(result_table.total_cost + table1.price + table.2.price) as total_combined_cost
from result_table 
right join table1
right join table2

This may be too much to ask for, so here is some other thoughts on the problem:

Instead of trying to combine multiple rows(task 2), store them in a temporary table
(which would be easier to calculate the total_cost using sum)

Feel free to drop any thoughts, don’t have to be complete answer, I feel it’s brilliant enough if you have an elegant way to do task 1 !

==Edited/Added 6 Feb 2012==

The goal of my program is to identify best combinations of items with minimal cost (and preferably possess higher utilitarian value at the same time).

Consider @ypercube’s comment about large number of groups, temporary table seems to be the only feasible solution. And it is also pointed out there is no pivoting function in MySQL (although it can be implemented, it’s not necessary to perform such operation).

Okay, after study @Johan’s answer, I’m thinking about something like this for task 1:

select * from
(
    select * from
    result_table
    order by price asc
) as ordered_table
group by group
;

Although looks dodgy, it seems to work.

==Edited/Added 7 Feb 2012==

Since there could be more than one combination may produce the same min value, I have modified my answer :

select result_table.* from  
(
    select * from
    (
        select * from
        result_table
        order by price asc
    ) as ordered_table
    group by group
) as single_min_table
inner join result_table
on result_table.group = single_min_table.group
and result_table.price = single_min_table.price 
;

However, I have just realised that there is another problem I need to deal with:
I can not ignore all the spec, since there is a provider property, items from different providers may or may not be able to be assembled together, so to be safe (and to simplify my problem) I decide to combine items from the same provider only, so the problem becomes:

For example if I have an initial table like this(with only 2 groups and 2 providers):

id group price spec provider
1  a     1     .    x
2  a     2     ..   y
3  a     3     ...  y
4  b     1     ...  y
5  b     2          x
6  b     3          z 

I need to combine

id group price spec provider
1  a     1     .    x
5  b     2          x

and

2  a     2     ..   y
4  b     1     ...  y

record (id 6) can be eliminated from the choices since it dose not have all the groups available.

So it’s not necessarily to select only the min of each group, rather it’s to select one from each group so that for each provider I have a minimal combined cost.

  • 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-29T07:37:18+00:00Added an answer on May 29, 2026 at 7:37 am

    Producing the total_cost only:

    SELECT SUM(min_price) AS total_cost
    FROM 
        ( SELECT MIN(price) AS min_price
          FROM TableX
          GROUP BY `group`
        ) AS grp
    

    If a result set with the minimum prices returned in row (not in column) per group is fine, then your problem is of the gretaest-n-per-group type. There are various methods to solve it. Here’s one:

    SELECT tg.grp 
           tm.price AS min_price
           tm.spec 
    FROM
          ( SELECT DISTINCT `group` AS grp
            FROM TableX
          ) AS tg 
      JOIN
          TableX AS tm
        ON 
          tm.PK =                             --- the Primary Key of the table
          ( SELECT tmin.PK 
            FROM TableX AS tmin
            WHERE tmin.`group` = tg.grp
            ORDER BY tmin.price ASC
            LIMIT 1
          )
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this table for documents (simplified version here): id rev content 1 1
I have an html table. Here is a simplified version: <table> <tr> <td><div style=display:
Here's a simplified version of my table. CREATE TABLE TBLAGENT(AGENTID NUMBER, NUMBERSENT NUMBER, AGENTNAME
Here's a simplified version of a table I have: sometable title | tag |
I have a DataTables table. Here's a simplified version of what I have as
Here is a simplified version of my table tbl_records -title -created -views I am
Here is a simplified version of my database model. I have two tables: Image,
Here is simplified version of my requirement I have a java class say Processor
Here's a simplified version of what I'm trying to do : Before any other
Here is a simplified version of my application showing what I'm doing. /* in

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.