2
I’ve been thinking for a few days about how to make a select less verbose, turning rows into columns. Today I have it that 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 are in rows and need to turn columns like this and there are only 8 columns - can vary up to 100 for each item according to its sequence. According to that the order received is not always sequential, for example ('001', '003', '040'), as it depends on the situation.
The script in this format is giant and impossible to maintain, another is that I need to order the sequence so that there is no 'gap' in the report display.
I’m open to suggestions and discussions.
Grateful.
Cool the suggestion @Rodolpho Sa, It would decrease the lines well, however I still have the problem of sequential is not exactly a sequence and there may be failures. Thanks for the tip.
– user39564
Maybe you can build a precedent that generates a dynamic query string (using this example as a template ) and then run it.
– Rodolpho Sa