I've been thinking of a few days ago to make a select less verbose, turning rows into columns. Today I have it this way:
(SELECT s.code_sample
FROM app_sample s
WHERE s.id_analysis = a.id
AND s.sequential_order = '001') AS code_sample_1,
(SELECT s.code_sample
FROM app_sample s
WHERE s.id_analysis = a.id
AND s.sequential_order = '002') AS code_sample_2,
(SELECT s.code_sample
FROM app_sample s
WHERE s.id_analysis = a.id
AND s.sequential_order = '003') AS code_sample_3,
(SELECT s.code_sample
FROM app_sample s
WHERE s.id_analysis = a.id
AND s.sequential_order = '004') AS code_sample_4,
(SELECT s.code_sample
FROM app_sample s
WHERE s.id_analysis = a.id
AND s.sequential_order = '005') AS code_sample_5,
(SELECT s.code_sample
FROM app_sample s
WHERE s.id_analysis = a.id
AND s.sequential_order = '006') AS code_sample_6,
(SELECT s.code_sample
FROM app_sample s
WHERE s.id_analysis = a.id
AND s.sequential_order = '007') AS code_sample_7,
(SELECT s.code_sample
FROM app_sample s
WHERE s.id_analysis = a.id
AND s.sequential_order = '008') AS code_sample_8,
The above script works, but I have two problems with it: First that there is a lot of information that is in rows and need to turn columns like this and are not just 8 columns - can vary up to 100 for each item according to its sequence. Second that the order received is not always sequential, for example ('001', '003', '040'), because it depends on the situation.
The script in this format is giant and impractical to maintain, another is that I need to sort the sequential so there is no gap in the report display.
I am open to suggestions and discussions.
Thank you.