1

課題、ディスカッション、シラバスの 3 つのコンポーネントを含むコース ID のリストを取得しようとしています。結果には、コース ID ごとに 1 行のみを表示し、それらのコースに課題、ディスカッション、およびシラバスがあるかどうかを示す Y または N を付けたいと考えています。ただし、特定のコース ID の Y と N のすべての一意の組み合わせを取得しています。

コース ID ごとに 1 行のみを返すにはどうすればよいですか?

select distinct cm.course_id as Course, 
case
    when exists (select 1 where gt.name ilike 'Assignment%')
        then 'Y'
        else 'N' 
end as are_there_assignments,
case
when exists (select 1 where gt.name ilike 'Discussion%')
    then 'Y'
      else 'N' 
end as are_there_discussions,
case
when exists (select 1 where ct.label ilike '%syllabus%' )
    then 'Y'
      else 'N' 
end as is_there_a_syllabus
from course_main cm
left join course_course cc
    on cm.pk1 = cc.crsmain_pk1
inner join gradebook_main gm
    on cm.pk1 = gm.crsmain_pk1
inner join gradebook_type gt
    on gm.gradebook_type_pk1 = gt.pk1
inner join course_toc ct
    on cm.pk1 = ct.crsmain_pk1
where cc.crsmain_parent_pk1 is NULL 
and (cm.course_id ilike '%SPF-2020-C%'
                    or cm.course_id ilike '%SPF-2020-S%'
                    or cm.course_id ilike '%SP2-2020-C%'
                    or cm.course_id ilike '%SP2-2020-S%'
                    or cm.course_id ilike '%SP2-2020-A%'
                    or cm.course_id ilike '%SPF-2020-A%')
order by cm.course_id;

ここに私の現在の結果があります:

結果

4

1 に答える 1

1

条件付き集計はできると思います。既存のクエリから始めると、次のようになります。

select
    cm.course_id,
    max(case when gt.name ilike 'Assignment%' then 'Y' else 'N' end) are_there_assignments,
    max(case when gt.name ilike 'Discussion%' then 'Y' else 'N' end) are_there_discussions,
    max(case when ct.label ilike '%syllabus%' then 'Y' else 'N' end) is_there_a_syllabus
from ...
where ...
group by cm.course_id
order by cm.course_id
于 2020-03-12T22:13:58.990 に答える