I am getting the following error with the code below:
ERROR: argument of AND must be type boolean, not type character varying LINE 9: and case ^ SQL state: 42804 Character: 300
This code does a few things, but what I am having trouble with is the case statment. I want this piece to look for instances where the first 11 characters of two strings match. If that is not true for a given record, then look at the first 10 characters, then 9, then 8. After that, null is an acceptable result.
select cm.course_id, cm.course_name, cmp.course_id as parentcourse,
(select cmm.course_id
from course_main cmm
where cmm.course_id ilike '%Master%'
and cmm.course_id not ilike '%Ground%'
and cmm.course_id not ilike '%Surprise%'
and cmm.course_id not ilike '%emba%'
and cmm.row_status != 2
and case
when left(cm.course_id,11) = left(cmm.course_id,11)
then cmm.course_id
else
case
when left(cm.course_id,10) = left(cmm.course_id, 10)
then cmm.course_id
else
case
when left(cm.course_id,9) = left(cmm.course_id, 9)
then cmm.course_id
else
case
when left(cm.course_id,8) = left(cmm.course_id,8)
then cmm.course_id
end
end
end
end) as mastercourse
from course_main as cm
left join course_course cc
on cc.crsmain_pk1 = cm.pk1
left join course_main cmp
on cmp.pk1 = cc.crsmain_parent_pk1
where cm.course_id ilike '%-ES-2020-%'
and cm.row_status != 2
and cmp.course_id is null
order by cm.course_id;
Thank you for the assistance, Z4. I took a shot at applying your suggestions and was able to get past the error. The problem with these strings is that I am trying to match something like:
'NRSG-46009-ES-2020-OA' to 'NRSG-46009-Master-Online-Content' in an environment that also contains these:
'NRSG-46006-Master-Online-Content' 'NRSG-46003-Master-Online-Content' 'NRSG-4600-Master-Online-Content'
If I look at the first 8 characters before looking at the first 11, I will get mismatches. So, I am looking at 11 first. If there is nothing that matches, look at the first ten, and so on. Strings of 8 are the floor in our ID schema with examples like this:
'IT-7003-ES-2019-AE' that needs to be matched with 'IT-7003-Master-Online-Content'
Anyway, I took your advice and ran the following:
select distinct cm.course_id, cm.course_name, cmp.course_id as parentcourse,
case
when left(cm.course_id,11) = left(cmm.course_id,11)
then cmm.course_id
else
case
when left(cm.course_id,10) = left(cmm.course_id, 10)
then cmm.course_id
else
case
when left(cm.course_id,9) = left(cmm.course_id, 9)
then cmm.course_id
else
case
when left(cm.course_id,8) = left(cmm.course_id,8)
then cmm.course_id
end
end
end
end as mastercourse
from course_main cm
left join course_course cc
on cc.crsmain_pk1 = cm.pk1
left join course_main cmp
on cmp.pk1 = cc.crsmain_parent_pk1
left join course_main cmm
on cm.pk1 = cm.pk1
where cm.course_id ilike '%-ES-2020-%'
and cm.row_status != 2
and cmp.course_id is null
and cmm.course_id ilike '%Master%'
and cmm.course_id not ilike '%Ground%'
and cmm.course_id not ilike '%Surprise%'
and cmm.course_id not ilike '%emba%'
and cmm.row_status != 2
order by cm.course_id;
This seems to be working, but I am getting duplicate results:
Any ideas on how I can exclude the duplicates?