Author |
Topic |
sarahmfr
Posting Yak Master
214 Posts |
Posted - 2012-02-05 : 10:19:42
|
Hi All,I would like to do the following using sub queriesI want to merge studentid's of all students enrolled either in parent or child section and all assessment id's for all assessments of the primary section and scores to the assessmentpoint table.If the student already has a score do not touch it if not add a row with null value.here are my tables section (sectionid, sectionname, parentsection)enrollment (studentid,sectionid,enrollmentstatus)assessment (sectionid,assessmentid,assessmentname,maxpoint)assessmentpoint(assessmentid,studentid,score)Note parentsection field is zero if the section is the parent other wise it will be the sectionid of the parent sectionI used before @declare @temp tables but this was taking memoryIs subqueries better and how to do that using a subquery.Thankssarah |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-02-05 : 10:30:47
|
can you show the sample output you require out of the tables above?I feel like you need to make use of recursive CTE for getting the parent child relationships from section table.------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
sarahmfr
Posting Yak Master
214 Posts |
Posted - 2012-02-05 : 10:37:19
|
thank you.here is my stored procedure to do the aboveI do not have an output I am just updating the assessmentpoint tablebecause I get enrolled student from xml every day and I update the enrollment table so I need also to update the assessmentpoint tableI am using declare @temptable a lot in my stored procedures I found that the sql memory usage is high so I am trying to improve itI will send you another one that produces an output declare @tempenrollment table (sectionid int,studentid int,parentsection int) insert into @tempenrollment (sectionid,studentid,parentsection)(select S.sectionid,E.studentid,S.parentsection from sp.section s inner join sp.sectionsemester ss on s.sectionid=ss.sectionid inner join enrollment.enrollment e on e.sectionid=s.sectionid where semesterid =@semesterid and enrollmentstatus=1)update @tempenrollmentset sectionid=parentsection where parentsection !=0declare @TempAssessmentPoint table(AssessmentID int,studentid int,Points decimal,updatedby int)insert into @TempAssessmentPoint(AssessmentID,studentid,Points,updatedby)(select AssessmentID,StudentID ,Null,1 from GradeBook.Assessment A inner join @tempenrollment E on A.SectionID=E.SectionID) merge into GradeBook.AssessmentPoint TUSING @TempAssessmentPoint S ON (T.AssessmentID=S.AssessmentID and T.StudentID=S.StudentID)WHEN NOT MATCHED THEN INSERT (AssessmentID,StudentID,Points,Updatedby)VALUES (S.AssessmentID,S.StudentID,S.Points,S.Updatedby);sarah |
 |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-02-05 : 10:42:03
|
sorry your procedure doent make much senseIn merge you're trying to insert AssessmentID,StudentID,Points,Updatedby into GradeBook.AssessmentPoint table whereas as per definition it has only below columnsassessmentid,studentid,scoreso I'm not sure how this is going to work------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
sarahmfr
Posting Yak Master
214 Posts |
Posted - 2012-02-05 : 10:43:39
|
sorry I forgot to add the column update in my post but it is in my stored procedure and it is working.I was trying to abbreviatesarah |
 |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-02-05 : 10:47:33
|
can section parent child relationship continue to multiple levels? like parent section,its child, chil's child etc?------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
sarahmfr
Posting Yak Master
214 Posts |
Posted - 2012-02-05 : 10:52:33
|
No it is only one level The enrollment table is huge and also the assessmentpoint tableI am looking to reserve memory and keep good performancthank you so so much sarah |
 |
|
sarahmfr
Posting Yak Master
214 Posts |
Posted - 2012-02-05 : 11:01:18
|
I forgot to mentionthere is another table because we might have seen sectionsemester in the stored proceduresectionseemester (sectionid,semesterid,sectionstartdate,sectionenddate)semester(semesterid,semestername,etc)We did that because we have different types of section section that have semesters and other public section which don't comply to semester Thank you againHope I did not confuse youSo I am doing the above for all sections in a given semesteri.e the semesterid is a parameter for the first stored proceduresarah |
 |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-02-05 : 11:01:23
|
[code]insert into assessmentpointselect a.assessmentid,e.studentid,nullfrom enrollment einner join section son s.sectionid = e.sectionidinner join assessment aon a.sectionid = e.sectionidleft join assessmentpoint apon ap.assessmentid = a.assessmentidand ap.studentid = e.studentidwhere s.sectionname='primary'and ap.assessmentid is null[/code]------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-02-05 : 11:02:58
|
quote: Originally posted by sarahmfr I forgot to mentionthere is another table because we might have seen sectionsemester in the stored proceduresectionseemester (sectionid,semesterid,sectionstartdate,sectionenddate)semester(semesterid,semestername,etc)We did that because we have different types of section section that have semesters and other public section which don't comply to semester Thank you againHope I did not confuse yousarah
as per your earlier definition i cant see what relevance it has on your output------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
sarahmfr
Posting Yak Master
214 Posts |
Posted - 2012-02-05 : 11:07:19
|
I can not use what you mentioned I am mergingLet me make it simpleIf I want to pull all students enrolled in a section and its children if it has any and pull all assessmentid for this sectionthen use the result of both to do a merge how can I do that without declaring a temporary tablessarah |
 |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
|
sarahmfr
Posting Yak Master
214 Posts |
Posted - 2012-02-05 : 11:21:38
|
Sorry for that.I will do that.I am using table variables to do that but it is taking out memorySo is it better to use cte note the enrollment table and assessment tables are huge and if cte is better how to do thatsarah |
 |
|
sarahmfr
Posting Yak Master
214 Posts |
Posted - 2012-02-05 : 11:42:04
|
First part of the questionsection(sectionid,sectionname,parentsection)example(1,first,0)(2,second,1)(3,third,0)this means first has a child secondand third has no childrenenrollment(sectionid,studentid,enrollmentstatus)(1,1,1)(1,2,1)(2,3,1)(2,4,1)(3,5,1)student(studentid,firstname,lastname)when i am in section=firstI want to see students enrolled in section=first and its child=secondI want to see the following students1,2,3,4second portiononly parent sections have assessmentsassessment table (assessmentid,sectionid,maxpoints)(1,1,10)(2,2,10)assessmentpoint table (will have assessmentid,studentid, score)this will include assessments of the parent and students of both the parent and the childreneach student enrolled in either parent or child should have a record in the assessmentpoint table for all assessments of the parentif a new student is enrolled to either parent or child I need to add a record for him for each assessment of the parentusing mergeThank youthankssarah |
 |
|
|