Skip to main content

Uma alternativa seria utilizar WITH para cada tipo de registros e depois realizar uma consulta entre eles.

http://sqlfiddle.com/#!17/fd98d/16

with funcionario_treinado as (
    select
        issues.id as id,
        custom_values.value funcionarios_treinados,
        start_date
    from
        issues
    left join custom_values on issues.id = custom_values.issue_id
    left join custom_fields on custom_fields.id = custom_values.cf_id
where
    custom_values.value <> ''
    and custom_fields.id = 489
group by
    issues.id,
    custom_fields.id,
    custom_values.value,
    start_date),
duracao_treinamento as (
    select
        issues.id as id,
        custom_values.value duracao,
        start_date
    from
        issues
    left join custom_values on issues.id = custom_values.issue_id
    left join custom_fields on custom_fields.id = custom_values.cf_id
where
    custom_values.value <> ''
    and custom_fields.id = 502
group by
    issues.id,
    custom_fields.id,
    custom_values.value,
    start_date
)
select
    f.id,
    f.funcionarios_treinados,
    d.duracao,
    f.start_date
from
    funcionario_treinado f
    left join duracao_treinamento d on (f.id = d.id)
where 
    f.funcionarios_treinados IS NOT NULL AND d.duracao IS NOT NULL
order by
    f.start_date;

Uma alternativa seria utilizar WITH para cada tipo de registros e depois realizar uma consulta entre eles.

http://sqlfiddle.com/#!17/fd98d/16

with funcionario_treinado as (
    select
        issues.id as id,
        custom_values.value funcionarios_treinados,
        start_date
    from
        issues
    left join custom_values on issues.id = custom_values.issue_id
    left join custom_fields on custom_fields.id = custom_values.cf_id
where
    custom_values.value <> ''
    and custom_fields.id = 489
group by
    issues.id,
    custom_fields.id,
    custom_values.value,
    start_date),
duracao_treinamento as (
    select
        issues.id as id,
        custom_values.value duracao,
        start_date
    from
        issues
    left join custom_values on issues.id = custom_values.issue_id
    left join custom_fields on custom_fields.id = custom_values.cf_id
where
    custom_values.value <> ''
    and custom_fields.id = 502
group by
    issues.id,
    custom_fields.id,
    custom_values.value,
    start_date
)
select
    f.id,
    f.funcionarios_treinados,
    d.duracao,
    f.start_date
from
    funcionario_treinado f
    left join duracao_treinamento d on (f.id = d.id)
order by
    f.start_date;

Uma alternativa seria utilizar WITH para cada tipo de registros e depois realizar uma consulta entre eles.

http://sqlfiddle.com/#!17/fd98d/16

with funcionario_treinado as (
    select
        issues.id as id,
        custom_values.value funcionarios_treinados,
        start_date
    from
        issues
    left join custom_values on issues.id = custom_values.issue_id
    left join custom_fields on custom_fields.id = custom_values.cf_id
where
    custom_values.value <> ''
    and custom_fields.id = 489
group by
    issues.id,
    custom_fields.id,
    custom_values.value,
    start_date),
duracao_treinamento as (
    select
        issues.id as id,
        custom_values.value duracao,
        start_date
    from
        issues
    left join custom_values on issues.id = custom_values.issue_id
    left join custom_fields on custom_fields.id = custom_values.cf_id
where
    custom_values.value <> ''
    and custom_fields.id = 502
group by
    issues.id,
    custom_fields.id,
    custom_values.value,
    start_date
)
select
    f.id,
    f.funcionarios_treinados,
    d.duracao,
    f.start_date
from
    funcionario_treinado f
    left join duracao_treinamento d on (f.id = d.id)
where 
    f.funcionarios_treinados IS NOT NULL AND d.duracao IS NOT NULL
order by
    f.start_date;
Fonte Link
Camilo Santos
  • 1,2mil
  • 10
  • 17

Uma alternativa seria utilizar WITH para cada tipo de registros e depois realizar uma consulta entre eles.

http://sqlfiddle.com/#!17/fd98d/16

with funcionario_treinado as (
    select
        issues.id as id,
        custom_values.value funcionarios_treinados,
        start_date
    from
        issues
    left join custom_values on issues.id = custom_values.issue_id
    left join custom_fields on custom_fields.id = custom_values.cf_id
where
    custom_values.value <> ''
    and custom_fields.id = 489
group by
    issues.id,
    custom_fields.id,
    custom_values.value,
    start_date),
duracao_treinamento as (
    select
        issues.id as id,
        custom_values.value duracao,
        start_date
    from
        issues
    left join custom_values on issues.id = custom_values.issue_id
    left join custom_fields on custom_fields.id = custom_values.cf_id
where
    custom_values.value <> ''
    and custom_fields.id = 502
group by
    issues.id,
    custom_fields.id,
    custom_values.value,
    start_date
)
select
    f.id,
    f.funcionarios_treinados,
    d.duracao,
    f.start_date
from
    funcionario_treinado f
    left join duracao_treinamento d on (f.id = d.id)
order by
    f.start_date;