0

I want to query a table in this format:

ID| QTY | CODE         
1 | 200 | C
1 | 500 | C
1 | 1000| C
1 | 2000| C 
2 | 5000| A
2 | 3000| A
2 | 2500| A
2 | 2000| A 
2 | 1000| B
2 | 500 | B

To create this:

ID | QTY1 | QTY2 | QTY3 | QTY4 | QTY5| CODE
1  | 200  | 500  | 1000 | 2000 |     |C,C,C,C
2  | 500  | 1000 | 2000 | 2500 |3000 |B,B,A,A

The first 5 quantities are taken in ascending order and the codes are concatenated. Sometimes there are less than 5 quantities, in which case missing values are blank. If there are more than 5, the lowest 5 are taken.

Edit: version 5.6.41 The records in the source table are not in any order, the goal is to have the smallest quantity in QTY 1 inclreasing to QTY 5

jfursedon
  • 11
  • 2
  • Consider handling issues of data display in application code. Failing that, see any of the enumerable duplicate questions. – Strawberry Feb 06 '20 at 09:42
  • Specify your MySQL version. Specify what sets the records order (how to understand what is first and what is next - by QTY increasing?). And where the code `C` is taken from for `ID=2`? – Akina Feb 06 '20 at 09:43

0 Answers0