Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
pprakash
Helper I
Helper I

Showing error while fetching data through sql query

i am trying to connect power bi with moodle through sql query:

it is showing fatal error ODBC. please let me know if there is any other way to fetch all data or we can optimize this query or divide it into parts to get data

 

select ta.student_name,ta.idnumber,ta.email,cte.anonymouscode,ta.fullname,ta.shortname,ta.section,

t1.internaltotal,t1.internalmax,t1.internal_remarks,

t2.externaltotal,t2.externalmax,t2.external_remarks,

t3.coursetotal,t3.courssetotalmax,t3.coursetotal_remarks,(round(t3.coursetotal) + IFNULL(mm.marks,0)) as moderatedtotal,

mm.marks as moderatedmarks,

clg.grade,clg.specialgrades

from

( SELECT u.id as userid,c.id as courseid,c.fullname,c.shortname,concat(u.firstname," ",u.lastname) as student_name,u.email,u.idnumber,g.name as section

FROM mdl_user u

JOIN mdl_user_enrolments ue ON ue.userid = u.id

JOIN mdl_enrol e ON e.id = ue.enrolid

JOIN mdl_role_assignments ra ON ra.userid = u.id

JOIN mdl_context ct ON ct.id = ra.contextid

AND ct.contextlevel = 50

JOIN mdl_course c ON c.id = ct.instanceid

AND e.courseid = c.id

JOIN mdl_role r ON r.id = ra.roleid

JOIN mdl_groups g ON g.courseid = c.id

JOIN mdl_groups_members gm on g.id = gm.groupid and gm.userid = u.id

WHERE e.status =0

AND u.suspended =0

AND u.deleted =0

AND ue.status =0

AND r.shortname ="student"

) as ta

Left JOIN

(

select c.id as courseid,u.id as userid,

gg.finalgrade as internaltotal,gi.grademax as internalmax,gg.feedback as internal_remarks

from mdl_grade_categories gc

JOIN mdl_grade_items gi ON gc.id = gi.categoryid and gc.gradeitemtype =1

JOIN mdl_course c ON gi.courseid = c.id

JOIN mdl_grade_grades gg ON gi.id = gg.itemid

JOIN mdl_user as u ON gg.userid = u.id

) as t1 ON ta.userid = t1.userid and ta.courseid = t1.courseid

LEFT JOIN

(

select c.id as courseid,u.id as userid ,gc.fullname as external,gc.gradeitemtype ,gg.finalgrade as externaltotal,gi.grademax as externalmax,gg.feedback as external_remarks

from mdl_grade_categories gc

JOIN mdl_grade_items gi ON gc.id = gi.categoryid

JOIN mdl_course c ON gi.courseid = c.id

JOIN mdl_grade_grades gg ON gi.id = gg.itemid

JOIN mdl_user as u ON gg.userid = u.id

Where gc.gradeitemtype =2

) as t2

ON ta.courseid = t2.courseid and ta.userid = t2.userid

LEFT JOIN

(

select c.id as courseid,u.id as userid ,gg.finalgrade as coursetotal,gi.grademax as courssetotalmax,gg.feedback as coursetotal_remarks

from mdl_grade_categories gc

JOIN mdl_grade_items gi ON gc.id = gi.iteminstance and (gc.gradeitemtype is null)

JOIN mdl_course c ON gi.courseid = c.id

Left JOIN mdl_grade_grades gg ON gi.id = gg.itemid

JOIN mdl_user as u ON gg.userid = u.id

) as t3 on ta.courseid = t3.courseid and ta.userid = t3.userid

LEFT JOIN mdl_moderated_marks as mm on ta.courseid = mm.courseid and ta.userid = mm.userid

LEFT JOIN mdl_course_letter_grades as clg on ta.courseid = clg.courseid and ta.userid = clg.userid

LEFT JOIN mdl_course_term_enrol cte on cte.userid = ta.userid AND cte.courseid = ta.courseid

where t3.coursetotal is not null;

2 REPLIES 2
vivran22
Community Champion
Community Champion

Hi,

Has this been resolved?

Rgds,
Vivek
vivran22
Community Champion
Community Champion

@pprakash 

 

Question: Are you getting the desired output from the query when you are running it in SQL Server Management studio (or something similar)?

 

If yes, then try this

 

Rgds,

Vivek

 

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

Find out what's new and trending in the Fabric Community.