Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
Author |
Topic |
beatkeeper25
Starting Member
27 Posts |
Posted - 2013-08-06 : 21:46:48
|
Table1 consists of MemberID INT, CourseID INT, CompletionDate DATETIME, DueDate DATETIME.Table2 consists of CourseID and RecurrenceMonths INTTable3 consists of MemberID and UnitFunction1-gets all children of parent unit (returns table)Tasks to be completed are:1) For all users who belong to result set of function (select * from function1(unit1)), transfer CompletionDate from course1 to course2 WHERE CompletionDate for Course2 is older than CompletionDate for Course1 AND CompletionDate for Course1 less than 1 year old.2) For all those CompletionDates that are transferred to Course2, clear them for Course1.3) Update DueDate for each row that is transferred. Due date is CompletionDate + RecurrenceMonths |
|
Lamprey
Master Smack Fu Yak Hacker
4614 Posts |
Posted - 2013-08-06 : 22:25:54
|
What have you tried so far?Here are some links to help you post your questiopn so we can help you better:http://www.sqlservercentral.com/articles/Best+Practices/61537/http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspxPS - We don't do homework, but we will help guide you into a solution. |
 |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
|
beatkeeper25
Starting Member
27 Posts |
Posted - 2013-08-07 : 10:37:27
|
Yes, I did get that to work, but not sure how to incorporate these new filters. Need to perform this for only memberid that are in the result set of a function. Also need to clear the CompletionDate for Course1 after date is moved to Course2. When doing multiple tasks on a set, do you save it to a temp table or something? |
 |
|
beatkeeper25
Starting Member
27 Posts |
Posted - 2013-08-07 : 16:02:34
|
OK, I think this is everything I need. Don't want to incorporate step 2 from above at this time. I'm new to SQL coding, so let me know how to shorten this or optimize.--create temp table and populate with course 1063 dataIF OBJECT_ID('tempdb.dbo.#TempTable1063') IS NOT NULLDROP TABLE dbo.#TempTable1063;SET NOCOUNT ONCREATE TABLE #TempTable1063(MemberID INT NOT NULL PRIMARY KEY,CourseID INT NOT NULL,CompletionDate DATETIME,SourceID INT);INSERT INTO #TempTable1063(MemberID, CourseID, CompletionDate, SourceID)SELECT MemberID, CourseID, CompletionDate, SourceIDFROM dbo.MemberCourseRequirementWHERE courseID=1063--create temp table and populate with course 8779 dataIF OBJECT_ID('tempdb.dbo.#TempTable8779') IS NOT NULLDROP TABLE dbo.#TempTable8779;CREATE TABLE #TempTable8779(MemberID INT NOT NULL PRIMARY KEY,CourseID INT NOT NULL,CompletionDate DATETIME,SourceID INT);INSERT INTO #TempTable8779(MemberID, CourseID, CompletionDate, SourceID)SELECT MemberID, CourseID, CompletionDate, SourceIDFROM dbo.MemberCourseRequirementWHERE courseID=8779--combine data into #TempCombinedIF OBJECT_ID('tempdb.dbo.#TempCombined') IS NOT NULLDROP TABLE dbo.#TempCombined;SELECT a.completiondate AS '8779 Comp Date', b.completiondate AS '1063 comp date', a.memberIDINTO #TempCombinedFROM #TempTable8779 AS aJOIN #TempTable1063 AS bON a.MemberID = b.MemberIDAND a.CompletionDate < b.CompletionDateWHERE b.CompletionDate > dateadd(year, -1, getdate())ORDER BY '8779 Comp Date' DESC--create set of affected users and put into #UsersIF OBJECT_ID('tempdb.dbo.#Users') IS NOT NULLDROP TABLE dbo.#Users;select * into #Usersfrom dbo.membercourserequirementwhere memberid in (select memberidfrom vcurrentmemberwhere unitid in (select unitidfrom dbo.fn_getchildrenofparentunit(39))and isdefault = 1)and courseid = 1063and completiondate > dateadd(year, -1, getdate()); --Update Data, transfer completion dateSET NOCOUNT OFFPRINT N'Transfering Completion Dates from 1063 to 8779'UPDATE dbo.MemberCourseRequirement SET CompletionDate = s.[1063 comp date]FROM dbo.MemberCourseRequirement gINNER JOIN #TempCombined s ON g.memberid=s.memberidWHERE g.CourseID=8779 AND EXISTS(SELECT MemberID FROM #Users)--update the duedate from the completion datesUPDATE dbo.MemberCourseRequirement SET duedate=DATEADD(yy,1,ISNULL(completiondate,0)) WHERE memberid IN (SELECT memberid FROM #Users) AND courseid=8779AND NOT duedate=DATEADD(yy,1,ISNULL(completiondate,0)) AND ISNULL(completiondate,0)>0 |
 |
|
|
|
|
|
|