Share via


Sample Azure SQL database queries for using the Microsoft Learn Organizational Reporting service

This article will assist you querying the Microsoft Learn Organizational Reporting Service when syncing to an Azure SQL database. If you aren't familiar with the details of the Learn Organizational Reporting service or the use cases for it, we recommend that you review the feature overview article first.

Get progress for user via Microsoft Entra Object ID

The userId column refers to the Learn profile user ID, not the Microsoft Entra Object ID. To retrieve progress for a specific user using a Microsoft Entra Object ID, join the users and progress tables.

DECLARE @aadObjectId VARCHAR(50)

SELECT users.AADObjectId, progress.*
FROM users_TENANT_ID_HERE users
JOIN progress_TENANT_ID_HERE progress ON users.userId = progress.userId
WHERE users.AADObjectId = @aadObjectId

Get progress with percent complete for a specific Learn training item

Percent completion can be calculated based on the durationInMinutes, which can be calculated while retrieving progress data for specific Microsoft Learn training items.

DECLARE @sourceType NVARCHAR(50)
DECLARE @sourceUid NVARCHAR(300)

-- If querying for a specific user:
-- DECLARE @aadObjectId VARCHAR(50)

SELECT
    users.AADObjectId,
    @sourceUid AS SourceUid,
    @sourceType AS SourceType,
    progress.XPReason,
    progress.XP,
    progress.AwardedOn,
    CASE
        WHEN progress.AwardedOn IS NOT NULL THEN
        (
            SELECT 1
        )

        WHEN @sourceType = 'LearningPath' THEN
        (
            -- User completed minutes of learning path
            SELECT SUM(m.durationInMinutes)
            FROM HierarchyLearningPathModule lpm
            JOIN HierarchyModule m ON m.moduleUid = lpm.ModuleUid
            JOIN progress_TENANT_ID_HERE p ON p.SourceUid = lpm.ModuleUid
            WHERE lpm.LearningPathUid = @sourceUid
            AND p.userId = users.userId
            AND p.SourceType = 'Module'
            AND p.XPReason = 'points.module.basic.completed'
        ) / CAST(
        (
            -- Total duration of learning path
            SELECT lp.durationInMinutes
            FROM HierarchyLearningPath lp
            WHERE lp.LearningPathUid = @sourceUid
        ) AS DECIMAL(5, 2))

        WHEN @sourceType = 'Module' THEN
        (
            -- User completed minutes of module
            SELECT SUM(unit.durationInMinutes)
            FROM progress_TENANT_ID_HERE p
            JOIN HierarchyUnit unit ON unit.unitUid = p.SourceUid
            AND unit.moduleUid = @sourceUid
            AND p.userId = users.userId
            AND p.SourceType = 'Unit'
            AND p.XPReason = 'points.unit.basic.completed'
        ) / CAST(
        (
            -- Total duration of module
            SELECT m.durationInMinutes
            FROM HierarchyModule m
            WHERE m.moduleUid = @sourceUid
        ) AS DECIMAL(5, 2))
    END AS PercentComplete
FROM users_TENANT_ID_HERE users
LEFT JOIN progress_TENANT_ID_HERE progress
ON users.userId = progress.userId
AND progress.SourceType = @sourceType
AND progress.SourceUid = @sourceUid
-- If querying for a specific user:
-- WHERE users.AADObjectId = @aadObjectId