Author - StudySection Post Views - 70 views
Concatenation in Totara

How to perform the Group Concatenation in Totara

There are two in-built functions in moodle/totara that return the result as a single field. For each result set row, these functions return a single string that represents the argument values concatenated together.

  1. sql_group_concat($expr, $separator, $orderby = ”): This function returns SQL for group concatenation on the specified field or expression.
    • In the first parameter, we have to pass the expression/fieldname.
    • The optional separator string is added between each pair of concatenated values if we give it in the second parameter.
    • When you want to sort array values, you have to pass in the third parameter the names of the fields you want to sort on. Applying the ORDER BY clause to the SELECT statement will not affect the result set because the SELECT clause returns only one string value.

    Example: Suppose you want to display the groups that each user is a member of:
    global $DB;
    $group_ids = $DB->sql_group_concat('gm.groupid', ',');
    $sql = " SELECT u.firstname,{$group_ids} as groups
    FROM {user} u
    LEFT JOIN {groups_members} gm ON gm.userid = u.id
    GROUP BY u.firstname,gm.userid";
    $users = $DB->get_records_sql($sql);

    This query returns the result as:

    Name Groups
    Riya 1,2
    Monika NULL
    Rahul 2,3
  2. sql_group_concat_unique($expr, $separator, $orderby = ”): The only difference between both functions is that it eliminates duplicate values from the result set. It means it takes only one value from all rows with the same values.

Microsoft Windows 10 is a widely used operating system in computers all over the world. If you have skills in Microsoft Windows 10 then you can get a Windows 10 Certification from StudySection which can help you in getting hired. A beginner level certification exam for newbies and an advanced level certification exam for experts is available on StudySection.

Leave a Reply

Your email address will not be published. Required fields are marked *

fiteesports.com rivierarw.com cratosroyalbet betwoon grandpashabet grandpashabet giriş deneme bonusu veren siteler casino siteleri