cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
Highlighted
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
Highlighted
Super User II
Super User II

Re: Showing error while fetching data through sql query

@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.





Did I answer your question? Mark my post as a solution!

Kudos will be cherry on the top 🙂


Proud to be a Super User!




Highlighted
Super User II
Super User II

Re: Showing error while fetching data through sql query

Hi,

Has this been resolved?

Rgds,
Vivek




Did I answer your question? Mark my post as a solution!

Kudos will be cherry on the top 🙂


Proud to be a Super User!




Helpful resources

Announcements
Community Conference

Power Platform Community Conference

Check out the on demand sessions that are available now!

Upcoming Events

Experience what’s next for Power BI

See the latest Power BI innovations, updates, and demos from the Microsoft Business Applications Launch Event.

secondImage

Power Platform 2020 release wave 2 plan

Features releasing from October 2020 through March 2021

Get Ready for Power BI Dev Camp

Get Ready for Power BI Dev Camp

Mark your calendars and join us for our next Power BI Dev Camp!.

Top Solution Authors
Top Kudoed Authors