0

I am stuck in a select for a BI. I need a pivot table based on categorie IDs on rows to be set up as columns. I tried using IF but it was not the best answer.

The select to see data is like this:

SELECT 
    cva.customized_id as ID_issue,
    cva.custom_field_id as VAR_customID,
    cva.value as VAR_customValue
FROM
    custom_values cva
WHERE cva.customized_id = '103766'
ORDER BY cva.customized_id DESC

This returns data in rows with the "custom_field_id" which I would love to put in columns:

# ID_issue, VAR_customID, VAR_customValue
'103766', '41', '-24.9429443 -53.4635777'
'103766', '45', 'Desl.Inicio: ____:____  Final: ____:____                                                           Ativd. Inicio: ____:____Final: ____:____'
'103766', '50', 'CSC-A006-Canadá'
'103766', '54', 'Nada consta'
'103766', '62', ''
'103766', '63', ''
'103766', '66', ''
'103766', '70', '0'
'103766', '72', '0'
'103766', '73', '0'
'103766', '77', ''
'103766', '78', ''
'103766', '79', ''
'103766', '80', ''
'103766', '40', '93'

So what I would love was to have some of the categories (I don't need all of them) in coluns having only one line for each ID_issue but if I try using IF it shows me other things:

SELECT 
    cva.customized_id as ID_issue,
    IF(cva.custom_field_id = '50',cva.value,'') as VAR_areaAtendimento,
    IF(cva.custom_field_id = '54',cva.value,'') as VAR_falhaEncontrada
FROM
    custom_values cva
WHERE cva.custom_field_id IN ('50','54')
    AND cva.customized_id = '103766'
ORDER BY cva.customized_id DESC

Shows me like this:

# ID_issue, VAR_areaAtendimento, VAR_falhaEncontrada
'103766', 'CSC-A006-Canadá', ''
'103766', '', 'Nada consta'

And I would love to have something like:

# ID_issue, VAR_areaAtendimento, VAR_falhaEncontrada
'103766', 'CSC-A006-Canadá', 'Nada consta'

I just tried a new solution using JOIN but it is really slow:

SELECT
  iss.id AS ID_issue,
  cv50.value as VAR_areaAtendimento,
  cv54.value as VAR_falhaEncontrada
FROM
  issues iss
INNER JOIN custom_values cv50 ON cv50.customized_id = iss.id AND cv50.custom_field_id = '50'
INNER JOIN custom_values cv54 ON cv54.customized_id = iss.id AND cv54.custom_field_id = '54'
WHERE
  iss.tracker_id = '12' #Mudar para cada consulta de outros tipos de tarefa
ORDER BY iss.id DESC
LIMIT 100
Rick James
  • 135,179
  • 13
  • 127
  • 222

0 Answers0