Configurable Reports Nested 'IF' statement v3.0.2+ (Build: 20160114)

Configurable Reports Nested 'IF' statement v3.0.2+ (Build: 20160114)

by Tracey Randall -
Number of replies: 6

Hi, I'm hoping this will be quite simple for someone who is more of an expert with the syntax required for Moodle.  We have a couple of courses that have been named with a numerical number at the end of the title.  What we would like to do is to strip the number from the course ID.  There are not many, so happy to do on an individual basis.  The courseID's are not consistent in length to limit the extract to a certain number of characters.  Currently I have the below SQL working fine for the adjustment of 1 course ID, however I need to be able to do this for several different courseID's.... Moodle does not seem to like 'else', or I am putting it in the wrong syntax.  An example of what I need (currently getting errors) where the bold line is below:

If (c.idnumber='MBLQ1', 'MBLQ', c.idnumber) else if (c.idnumber='MBLQ2', 'MBLQ', c.idnumber) etc.... AS 'Certification Code'


SELECT DISTINCT u.username AS 'ID Number',
if (c.idnumber='MBLQ1', 'MBLQ', c.idnumber) AS 'Certification Code',
FROM_UNIXTIME(gg.timemodified, '%d/%m/%y') AS 'Obtained Date'
FROM prefix_course AS c

JOIN prefix_context AS ctx ON c.id = ctx.instanceid
JOIN prefix_role_assignments AS ra ON ra.contextid = ctx.id
JOIN prefix_user AS u ON u.id = ra.userid
JOIN prefix_grade_grades AS gg ON gg.userid = u.id
JOIN prefix_grade_items AS gi ON gi.id = gg.itemid
JOIN prefix_course_categories AS cc ON cc.id = c.category
 

WHERE  gi.courseid = c.id and cc.name = '2017' and gg.rawgrademax=gg.finalgrade
ORDER BY lastname


If anyone could offer a suggestion it would be greatly appreciated!  Thanks!



Average of ratings: -
In reply to Tracey Randall

Re: Configurable Reports Nested 'IF' statement v3.0.2+ (Build: 20160114)

by Nadav Kavalerchik -
Picture of Core developers Picture of Plugin developers Picture of Testers Picture of Translators

You should use CASE, instead of IF.

Average of ratings: Useful (1)
In reply to Nadav Kavalerchik

Re: Configurable Reports Nested 'IF' statement v3.0.2+ (Build: 20160114)

by Tracey Randall -

Thanks for your response Nadav.... can you give me an example syntax with CASE.... every time I've put it in, I'm still getting errors.  Greatly appreciated (again)!

In reply to Tracey Randall

Re: Configurable Reports Nested 'IF' statement v3.0.2+ (Build: 20160114)

by Randy Thornton -
Picture of Documentation writers

There are numerous good examples of CASE in the ad-hoc reports page: https://docs.moodle.org/32/en/ad-hoc_contributed_reports#What_teachers_and_courses_considered_active.3F


Also, if you post SQL questions in the Configurable Report forum, you will get more attention for your post ;)

Average of ratings: Useful (1)
In reply to Randy Thornton

Re: Configurable Reports Nested 'IF' statement v3.0.2+ (Build: 20160114)

by Tracey Randall -

Thanks Randy... sorry first post... will try and work it out and if I get stuck will post in Configurable Reports... thanks again.


Cheers,

Tracey

In reply to Tracey Randall

Re: Configurable Reports Nested 'IF' statement v3.0.2+ (Build: 20160114)

by Tracey Randall -

Solved... just needed to be pointed in the right direction.... thanks to you both for your assistance smile

In reply to Tracey Randall

Re: Configurable Reports Nested 'IF' statement v3.0.2+ (Build: 20160114)

by Randy Thornton -
Picture of Documentation writers

Tracey,

No problem at all. Welcome to the forums smile


SQL action happens mostly in the Configurable Reports forum. If you get a good solution to a problem, it is often helpful to others to post your solution, so they can see working code. Many people there don't have your obvious level of knowledge with SQL.

If you have any good, generic reports you thing would be useful to others, do consider sharing those over in the Ad-ho queries page. The more the merrier when it comes to reports smile


Randy