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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T08:51:59+00:00 2026-05-30T08:51:59+00:00

I have a MySQL table with fields and data such as follows; PartNumber Priority

  • 0

I have a MySQL table with fields and data such as follows;

PartNumber  Priority SupName
a1            0        One
a2            0        One
a2            1        Two
a3            0        One
a4            1        Two
a5            2        Three

I am trying to create a view where the parts that have multiple rows are combined into a single row, and into separate fields such as

Ideally This;

PartNumber  Sup1  Sup2  Sup3
a1          One   NULL  NULL
a2          One   Two   NULL
a3          One   NULL  NULL
a4          Two   NULL  NULL
a5          Three NULL  NULL

Or I can live with this

PartNumber  Sup1  Sup2  Sup3
a1          One   NULL  NULL
a2          One   Two   NULL
a3          One   NULL  NULL
a4          NULL  Two   NULL
a5          NULL  NULL  Three

How would I build a view or select statement to accomplish this?

The closest I have come so far is;

SELECT PartNumber, 
       IF(Priority=0, SupName, NULL) AS Sup1, 
       IF(Priority=1, SupName, NULL) AS Sup2,
       IF(Priority=2, SupName, NULL) AS Sup3 
  FROM SupXref 
ORDER BY PartNumber

This however gives me a separate row for each of the fields and I need a single line.

  • 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-30T08:52:00+00:00Added an answer on May 30, 2026 at 8:52 am

    You’re just missing a group by 🙂

    SELECT PartNumber,
      MAX(IF (Priority = 0, SupName, NULL)) AS Sup1,
      MAX(IF (Priority = 1, SupName, NULL)) AS Sup2,
      MAX(IF (Priority = 2, SupName, NULL)) AS Sup3
    FROM SupXref
    GROUP BY PartNumber
    

    Edit:

    After playing for a while I think I got the first solution you’re looking for. Give it a try 🙂

    SELECT partnumber,
      COALESCE(Sup1, COALESCE(Sup2, Sup3)) AS Supp1,
      IF (Sup1 IS NULL, IF (Sup2 IS NULL, NULL, Sup3), COALESCE(Sup2, Sup3)) AS Supp2,
      IF (Sup1 IS NULL, NULL, IF (Sup2 IS NULL, NULL, Sup3)) AS Supp3
    FROM (
      SELECT PartNumber,
        MAX(IF (Priority = 0, SupName, NULL)) AS Sup1,
        MAX(IF (Priority = 1, SupName, NULL)) AS Sup2,
        MAX(IF (Priority = 2, SupName, NULL)) AS Sup3
      FROM SupXref
      GROUP BY PartNumber
    ) AS S
    

    For the following table:

    +------------+----------+---------+
    | PARTNUMBER | PRIORITY | SUPNAME |
    +------------+----------+---------+
    | a1         |        2 | Three   |
    | a2         |        1 | Two     |
    | a3         |        2 | Three   |
    | a3         |        1 | Two     |
    | a4         |        0 | One     |
    | a5         |        0 | One     |
    | a5         |        2 | Three   |
    | a6         |        0 | One     |
    | a6         |        1 | Two     |
    | a7         |        0 | One     |
    | a7         |        1 | Two     |
    | a7         |        2 | Three   |
    +------------+----------+---------+
    

    Data is turn into this:

    +------------+------+------+-------+
    | PARTNUMBER | SUP1 | SUP2 | SUP3  |
    +------------+------+------+-------+
    | a1         |      |      | Three |
    | a2         |      | Two  |       |
    | a3         |      | Two  | Three |
    | a4         | One  |      |       |
    | a5         | One  |      | Three |
    | a6         | One  | Two  |       |
    | a7         | One  | Two  | Three |
    +------------+------+------+-------+
    

    And finally into this:

    +------------+-------+-------+-------+
    | PARTNUMBER | SUPP1 | SUPP2 | SUPP3 |
    +------------+-------+-------+-------+
    | a1         | Three |       |       |
    | a2         | Two   |       |       |
    | a3         | Two   | Three |       |
    | a4         | One   |       |       |
    | a5         | One   | Three |       |
    | a6         | One   | Two   |       |
    | a7         | One   | Two   | Three |
    +------------+-------+-------+-------+
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a mysql table field set as time type which stores data in
I have a MySQL table LOGIN_LOG with fields ID, PLAYER, TIMESTAMP and ACTION. ACTION
My MySQL table is simple. I have 3 fields: an index a url a
I have a table in MySQL that has 3 fields and I want to
I have a MySQL table with approximately 3000 rows per user. One of the
I have a main MySQL table which holds products. Any multiple fields for each
I have few thousands of records with few 100 fields in a MySQL Table.
I have two questions: 1) I am trying to fill a form's fields with
I have 3 tables: table events . In this I have fields such as
I have a MySQL table with the following fields: id, morning, evening, event, other-fields

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.