Uploaded image for project: 'Plugins'
  1. Plugins
  2. CONTRIB-2192

Use case statements in large sql query instead of many small ones for counts

XMLWordPrintable

    • Icon: Improvement Improvement
    • Resolution: Fixed
    • Icon: Minor Minor
    • 2.0.2
    • 1.9.9
    • Block: AJAX Marking
    • None
    • MOODLE_19_STABLE
    • MOODLE_20_STABLE

      From http://www.dbapool.com/downloads/whitepapers/tipsforesql.pdf

      FROM emp
      WHERE sal < 2000;
      SELECT COUNT
      FROM emp
      WHERE sal BETWEEN 2000 AND 4000;
      SELECT COUNT
      FROM emp
      WHERE sal>4000;
      However, it is more efficient to run the entire query in a single statement. Each number is
      calculated as one column. The count uses a filter with the CASE statement to count only the
      rows where the condition is valid. For example:
      SELECT COUNT (CASE WHEN sal < 2000
      THEN 1 ELSE null END) count1,
      COUNT (CASE WHEN sal BETWEEN 2001 AND 4000
      THEN 1 ELSE null END) count2,
      COUNT (CASE WHEN sal > 4000
      THEN 1 ELSE null END) count3
      FROM emp;

            mattgibson Matt Gibson (Inactive)
            mattgibson Matt Gibson (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:

                Error rendering 'clockify-timesheets-time-tracking-reports:timer-sidebar'. Please contact your Jira administrators.