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 |
|
ramoneguru
Yak Posting Veteran
69 Posts |
Posted - 2007-05-23 : 19:22:10
|
| Doing some maint on this sql implementation (not my methodology, but I'd rather not do a complete rewrite). In any event, I'm getting a row in my table that I didn't expect...here's the code.Here's the variables/table declarations:DECLARE @LanguageArtsMinCredSET @LanguageArtsMinCred = 4DECLARE @SocialStudiesMinCredSET @SocialStudiesMinCred = 1etc...DECLARE @AllocatedCredits TABLE ( PersonID INT, LanguageArts REAL, SocialStudies REAL, USHistory REAL, Economics REAL, Government REAL, Mathematics REAL, Science REAL, ComputerScience REAL, ELECTIVES REAL, WorkCredits REAL )She then proceeds to do a sum of the values from other tables within the database and store these in the @AllocatedCredits table:INSERT @AllocatedCredits SELECT PersonID,SUM(CASE WHEN (ClassMeetsRequirements = 'Language Arts') THEN EarnedCredits ELSE 0.0 END) AS LanguageArts,SUM(CASE WHEN (ClassMeetsRequirements = 'Social Studies') THEN EarnedCredits ELSE 0.0 END) AS SocialStudies,SUM(CASE WHEN (ClassMeetsRequirements = 'US History') THEN EarnedCredits ELSE 0.0 END) AS USHistory,etc....Then based on those values she adjusts the @AllocatedCredits table like so (as per requirements):DECLARE @result AS REAL--------Recalculate Language Arts Hours--------SET @result = (SELECT SUM(LanguageArts) AS LanguageArts FROM @AllocatedCredits) - @LanguageArtsMinCredIF (@result > 0)BEGIN UPDATE @AllocatedCredits SET Electives = (SELECT SUM(Electives) FROM @AllocatedCredits) + @result, LanguageArts = @LanguageArtsMinCredEND--------Recalculate SocialStudies Hours--------SET @result = (SELECT SUM(SocialStudies) FROM @AllocatedCredits) - @SocialStudiesMinCredIF (@result > 0)BEGIN UPDATE @AllocatedCredits SET Electives = (SELECT SUM(Electives) FROM @AllocatedCredits) + @result, SocialStudies = @SocialStudiesMinCredENDetc...Then I select * from @allocatedCredits and get this:L = languages = social studiesm = mathetc...L s m etc...0 0 0 0 0 0 3 0 13.5 4 1 1 0.5 0.5 4 3 1 13.5 The update inserts a row at the top that shows the value of @result if the result was 0 or the @<subject>MinCred value that was assigned at the beginning of the script.What the heck is going on, why is it inserting a new row???--Nick |
|
|
Vinnie881
Master Smack Fu Yak Hacker
1231 Posts |
Posted - 2007-05-24 : 02:22:27
|
| Post the entire query. It's too hard to try and decipher with the missing code. |
 |
|
|
ramoneguru
Yak Posting Veteran
69 Posts |
Posted - 2007-05-24 : 15:09:47
|
| set ANSI_NULLS ONset QUOTED_IDENTIFIER ONgo/****** Version 110; Updated Nov 6 2006 2:16PM ******/--ALTER PROCEDURE [dbo].[spGetPersonTranscriptAllocatedCredits] @ssn VARCHAR(50)--AS--Leave for testingDECLARE @ssn INTSET @ssn = 250895497DECLARE @LanguageArtsMinCred REAL SET @LanguageArtsMinCred = 4DECLARE @SocialStudiesMinCred REAL SET @SocialStudiesMinCred = 1DECLARE @USHistoryMinCred REAL SET @USHistoryMinCred = 1DECLARE @EconomicsMinCred REAL SET @EconomicsMinCred = 0.5DECLARE @GovernmentMinCred REAL SET @GovernmentMinCred = 0.5DECLARE @MathematicsMinCred REAL SET @MathematicsMinCred = 4DECLARE @ScienceMinCred REAL SET @ScienceMinCred = 3DECLARE @ComputerScienceMinCred REAL SET @ComputerScienceMinCred = 1 --Student Allocated Credits will contain the earned credits from linked and unlinked classesDECLARE @AllocatedCredits TABLE ( PersonID INT, LanguageArts REAL, SocialStudies REAL, USHistory REAL, Economics REAL, Government REAL, Mathematics REAL, Science REAL, ComputerScience REAL, ELECTIVES REAL, WorkCredits REAL )--this table will contain student credits and credit classificationsDECLARE @CreditsClassification TABLE( seq INT IDENTITY(1,1), PersonID INT, ClassTitle VARCHAR(150), EarnedCredits REAL, ClassMeetsRequirements VARCHAR(150), ClassMeetsOptionCode INT, ClassMeetsRequirementsCode INT, IsWorkCredit INT, AgencyID INT)-- The table contains unlinked coursesDECLARE @UnlinkedCreditsClassification TABLE( seq INT IDENTITY(1,1), PersonID INT, ClassTitle VARCHAR(150), EarnedCredits REAL, ClassMeetsRequirements VARCHAR(150), ClassMeetsOptionCode INT, ClassMeetsRequirementsCode INT, IsWorkCredit INT, AgencyID INT)INSERT @CreditsClassification (PersonID, ClassTitle, EarnedCredits, ClassMeetsRequirements, ClassMeetsOptionCode, ClassMeetsRequirementsCode, IsWorkCredit, AgencyID)SELECT dbo.ClassRegistration.PersonID AS PersonID, dbo.ClassSection.ClassSectionTitle AS ClassSectionTitle_Linked, dbo.ClassRegistration.EarnedCredits, dbo.OT_ClassMeetsRequirement.ShortDescription AS ClassMeetsRequirements, dbo.OT_ClassMeetsRequirement.OptionCode AS ClassMeetsOptionCode, dbo.ClassRegistration.ClassMeetsRequirementCode AS ClassMeetsRequirementsCode, dbo.ClassRegistration.IsWorkCredit, dbo.Tempagency.AgencyIDFROM dbo.Student WITH (READUNCOMMITTED) JOIN dbo.Person WITH (READUNCOMMITTED) ON dbo.Student.PersonID = dbo.Person.PersonID JOIN dbo.ClassRegistration WITH (READUNCOMMITTED) ON dbo.Student.PersonID = dbo.ClassRegistration.PersonID JOIN dbo.ClassSection WITH (READUNCOMMITTED) ON dbo.ClassRegistration.ClassSectionID = dbo.ClassSection.ClassSectionID LEFT OUTER JOIN dbo.PersonIdentifier ON (dbo.Student.PersonID = dbo.PersonIdentifier.PersonID) LEFT OUTER JOIN dbo.OT_RegistrationStatus WITH (READUNCOMMITTED) ON dbo.ClassRegistration.StatusCode = dbo.OT_RegistrationStatus.OptionCode AND dbo.ClassRegistration.AgencyID = dbo.OT_RegistrationStatus.AgencyID LEFT OUTER JOIN dbo.OT_ClassMeetsRequirement WITH (READUNCOMMITTED) ON dbo.ClassRegistration.ClassMeetsRequirementCode = dbo.OT_ClassMeetsRequirement.OptionCode AND dbo.ClassRegistration.AgencyID = dbo.OT_ClassMeetsRequirement.AgencyID JOIN dbo.TempAgency ON (dbo.Tempagency.AgencyID = dbo.Student.AgencyID) JOIN dbo.ReportOnSelectionTempID WITH (READUNCOMMITTED) ON (dbo.ClassRegistration.PersonID = dbo.ReportOnSelectionTempID.ID)WHERE dbo.PersonIdentifier.IdentificationCode = @ssn AND dbo.ClassRegistration.StatusCode = 103 --AND and ClassRegistration.ClassMeetsRequirementCode != 2 --(ISNUMERIC(dbo.ClassRegistration.FinalLetterGrade) = 1 -- AND dbo.ClassRegistration.FinalLetterGrade < '100' OR dbo.ClassRegistration.FinalLetterGrade > '0')ORDER BY dbo.Student.PersonIDINSERT @UnlinkedCreditsClassification (PersonID, ClassTitle, EarnedCredits, ClassMeetsRequirements, ClassMeetsOptionCode, ClassMeetsRequirementsCode, IsWorkCredit, AgencyID)SELECT t.PersonID AS PersonID, t.CourseName AS Unlined_CourseName, t.EarnedCredits, dbo.OT_ClassMeetsRequirement.ShortDescription AS ClassMeetsRequirements, dbo.OT_ClassMeetsRequirement.OptionCode AS ClassMeetsOptionCode, t.ClassMeetsRequirementCode AS ClassMeetsRequirementsCode, t.IsWorkCredit, dbo.TempAgency.AgencyID AS AgencyIDFROM dbo.Student JOIN dbo.TranscriptClassRegistration t ON (dbo.Student.PersonID = t.PersonID) LEFT OUTER JOIN dbo.PersonIdentifier ON (dbo.Student.PersonID = dbo.PersonIdentifier.PersonID) LEFT OUTER JOIN dbo.OT_RegistrationStatus ON (t.StatusCode = dbo.OT_RegistrationStatus.OptionCode AND t.AgencyID = dbo.OT_RegistrationStatus.AgencyID) LEFT OUTER JOIN dbo.OT_ClassMeetsRequirement ON (t.ClassMeetsRequirementCode = dbo.OT_ClassMeetsRequirement.OptionCode AND dbo.Student.AgencyID = dbo.OT_ClassMeetsRequirement.AgencyID) JOIN dbo.TempAgency ON (dbo.Tempagency.AgencyID = dbo.Student.AgencyID) JOIN dbo.ReportOnSelectionTempID WITH (READUNCOMMITTED) ON (t.PersonID = dbo.ReportOnSelectionTempID.ID)WHERE dbo.PersonIdentifier.IdentificationCode = @ssn AND t.StatusCode = 103 and t.ClassMeetsRequirementCode != 2 -- AND (ISNUMERIC(t.FinalLetterGrade) = 1 AND t.FinalLetterGrade < '100' OR t.FinalLetterGrade > '0')ORDER BY dbo.Student.PersonID--Union of Linked and Unlinked ClassesINSERT @AllocatedCredits SELECT PersonID,SUM(CASE WHEN (ClassMeetsRequirements = 'Language Arts') THEN EarnedCredits ELSE 0.0 END) AS LanguageArts,SUM(CASE WHEN (ClassMeetsRequirements = 'Social Studies') THEN EarnedCredits ELSE 0.0 END) AS SocialStudies,SUM(CASE WHEN (ClassMeetsRequirements = 'US History') THEN EarnedCredits ELSE 0.0 END) AS USHistory,SUM(CASE WHEN (ClassMeetsRequirements = 'Economics') THEN EarnedCredits ELSE 0.0 END) AS Economics,SUM(CASE WHEN (ClassMeetsRequirements = 'Government') THEN EarnedCredits ELSE 0.0 END) AS Government,SUM(CASE WHEN (ClassMeetsRequirements = 'Mathematics') THEN EarnedCredits ELSE 0.0 END) AS Mathematics,SUM(CASE WHEN (ClassMeetsRequirements = 'Natural Science') THEN EarnedCredits ELSE 0.0 END) AS Science,SUM(CASE WHEN (ClassMeetsRequirements IN ('Computer Science','ComputerScience', 'Keyboarding', 'Computer Applications')) THEN EarnedCredits ELSE 0.0 END) AS ComputerScience,SUM(CASE WHEN (ClassMeetsRequirements NOT IN ('Language Arts', 'Social Studies', 'US History', 'Economics', 'Government', 'Mathematics', 'Natural Science', 'ComputerScience', 'Computer Applications', 'Keyboarding' )) THEN EarnedCredits ELSE 0 END) AS Electives,SUM(CASE WHEN (IsWorkCredit=1) THEN EarnedCredits ELSE 0.0 END) AS WorkCreditsFROM @CreditsClassificationGROUP BY PersonIDUNION SELECT PersonID, SUM(CASE WHEN (ClassMeetsRequirements = 'Language Arts') THEN EarnedCredits ELSE 0.0 END) AS LanguageArts,SUM(CASE WHEN (ClassMeetsRequirements = 'Social Studies') THEN EarnedCredits ELSE 0.0 END) AS SocialStudies,SUM(CASE WHEN (ClassMeetsRequirements = 'US History') THEN EarnedCredits ELSE 0.0 END) AS USHistory,SUM(CASE WHEN (ClassMeetsRequirements = 'Economics') THEN EarnedCredits ELSE 0.0 END) AS Economics,SUM(CASE WHEN (ClassMeetsRequirements = 'Government') THEN EarnedCredits ELSE 0.0 END) AS Government,SUM(CASE WHEN (ClassMeetsRequirements = 'Mathematics') THEN EarnedCredits ELSE 0.0 END) AS Mathematics,SUM(CASE WHEN (ClassMeetsRequirements = 'Natural Science') THEN EarnedCredits ELSE 0.0 END) AS Science,SUM(CASE WHEN (ClassMeetsRequirements IN ('Computer Science','ComputerScience', 'Keyboarding', 'Computer Applications')) THEN EarnedCredits ELSE 0.0 END) AS ComputerScience,SUM(CASE WHEN (IsWorkCredit=0 AND (ClassMeetsRequirements NOT IN ('Language Arts', 'Social Studies', 'US History', 'Economics', 'Government', 'Mathematics', 'Natural Science', 'ComputerScience', 'Computer Applications', 'Keyboarding' ))) THEN EarnedCredits ELSE 0 END) AS Electives,SUM(CASE WHEN (IsWorkCredit=1) THEN EarnedCredits ELSE 0.0 END) AS WorkCreditsFROM @UnlinkedCreditsClassificationGROUP BY PersonIDDECLARE @result AS REAL--------Recalculate Language Arts Hours--------SET @result = (SELECT SUM(LanguageArts) AS LanguageArts FROM @AllocatedCredits) - @LanguageArtsMinCredIF (@result > 0)BEGIN UPDATE @AllocatedCredits SET Electives = (SELECT SUM(Electives) FROM @AllocatedCredits) + @result, LanguageArts = @LanguageArtsMinCredEND--------Recalculate SocialStudies Hours--------SET @result = (SELECT SUM(SocialStudies) FROM @AllocatedCredits) - @SocialStudiesMinCredIF (@result > 0)BEGIN UPDATE @AllocatedCredits SET Electives = (SELECT SUM(Electives) FROM @AllocatedCredits) + @result, SocialStudies = @SocialStudiesMinCredEND--------Recalculate Economics hours--------SET @result = (SELECT SUM(Economics) FROM @AllocatedCredits) - @EconomicsMinCredIF (@result > 0)BEGIN UPDATE @AllocatedCredits SET Electives = (SELECT SUM(Electives) FROM @AllocatedCredits) + @result, Economics = @EconomicsMinCredEND--------Recalculate Government Hours--------SET @result = (SELECT SUM(Government) FROM @AllocatedCredits) - @GovernmentMinCredIF (@result > 0)BEGIN UPDATE @AllocatedCredits SET Electives = (SELECT SUM(Electives) FROM @AllocatedCredits) + @result, Government = @GovernmentMinCredEND--------Recalculate Mathematics Hours--------SET @result = (SELECT SUM(Mathematics) FROM @AllocatedCredits) - @MathematicsMinCredIF (@result > 0)BEGIN UPDATE @AllocatedCredits SET Electives = (SELECT SUM(Electives) FROM @AllocatedCredits) + @result, Mathematics = @MathematicsMinCredEND--------Recalculate Science Hours--------SET @result = (SELECT SUM(Science) FROM @AllocatedCredits) - @ScienceMinCredIF (@result > 0)BEGIN UPDATE @AllocatedCredits SET Electives = (SELECT SUM(Electives) FROM @AllocatedCredits) + @result, Science = @ScienceMinCredEND--------Recalculate ComputerScience Hours--------SET @result = (SELECT SUM(ComputerScience) FROM @AllocatedCredits) - @ComputerScienceMinCredIF (@result > 0)BEGIN UPDATE @AllocatedCredits SET Electives = (SELECT SUM(Electives) FROM @AllocatedCredits) + @result, ComputerScience = @ComputerScienceMinCredEND--The above update statements for some reason add another row in the @AllocatedCredits table at the top--this affects the calculations below since it duplicates some column values--SELECT * from @AllocatedCreditsIF ((SELECT COUNT(*) FROM @AllocatedCredits ) > 1) DELETE TOP (1) PERCENT FROM @AllocatedCreditsSELECT A.*, B.TotalCredits, dbo.fnGetAttemptedCredits(A.PersonID) AS TotalCredits_WithoutWCFROM(SELECT PersonID, SUM(LanguageArts) AS LanguageArts, SUM(SocialStudies) AS SocialStudies, SUM(USHistory) AS USHistory, SUM(Economics) AS Economics, SUM(Government) AS Government, SUM(Mathematics) AS Mathematics, SUM(Science) AS Science, SUM(ComputerScience) AS ComputerScience, SUM(Electives) AS Electives, SUM(WorkCredits) AS WorkCredits FROM @AllocatedCredits GROUP BY PersonID) AS ALEFT OUTER JOIN(SELECT PersonID, SUM(LanguageArts + SocialStudies + USHistory + Economics + Government + Mathematics + Science + ComputerScience + Electives + WorkCredits) AS TotalCredits FROM @AllocatedCredits GROUP BY PersonID) AS BON A.PersonID = B.PersonID/*SELECT * FROM @AllocatedCreditsSELECTSocialStudies - @SocialStudiesMinCred,USHistory - @USHistoryMinCred,Economics - @EconomicsMinCred,Government - @GovernmentMinCred,Mathematics - @MathematicsMinCred,Science - @ScienceMinCred,ComputerScience - @ComputerScienceMinCredFROM @AllocatedCreditsDELETE ReportOnSelectionTempIDDELETE TempAgencyINSERT INTO ReportOnSelectionTempID (ID) VALUES (59918)INSERT INTO TempAgency (AgencyID) VALUES (47596716)Min credit values for these areas of study:LanguageArts (4)SocialStudies (1)USHistory (1)Economics (0.5)Government (0.5)Mathematics (4)Science (3)ComputerScience (1)Electives*/Have at it.....--Nick |
 |
|
|
Lamprey
Master Smack Fu Yak Hacker
4614 Posts |
Posted - 2007-05-24 : 16:33:54
|
| I just did a quick scan, but it appears that you the UNION on the insert into @AllocatedCredits would give you 2 rows per person. Is that what is supposed to happen? -Ryan |
 |
|
|
ramoneguru
Yak Posting Veteran
69 Posts |
Posted - 2007-05-24 : 17:02:27
|
| Yeah, you are correct Ryan. Its the update that messes up my calculations. Its updating both rows because I didn't specify which row to update (I thought only one row was in the table). Great, thanks again SQL Team!!--Nick |
 |
|
|
ramoneguru
Yak Posting Veteran
69 Posts |
Posted - 2007-05-24 : 17:22:18
|
| Heads up for a quick fix (the boss needed this asap so no time to redo the thing which is what I wanted):create a new variable table:DECLARE @ReCalcAllocatedCredits TABLE ( PersonID INT, LanguageArts REAL, SocialStudies REAL, USHistory REAL, Economics REAL, Government REAL, Mathematics REAL, Science REAL, ComputerScience REAL, ELECTIVES REAL, WorkCredits REAL, TotalCredits REAL, TotalCredits_WithoutWC REAL)Then insert the sum of both rows into new table thusly:INSERT @ReCalcAllocatedCreditsSELECT A.*, B.TotalCredits, dbo.fnGetAttemptedCredits(A.PersonID) AS TotalCredits_WithoutWCFROM(SELECTPersonID,SUM(LanguageArts) AS LanguageArts,SUM(SocialStudies) AS SocialStudies,SUM(USHistory) AS USHistory,SUM(Economics) AS Economics,SUM(Government) AS Government,SUM(Mathematics) AS Mathematics,SUM(Science) AS Science,SUM(ComputerScience) AS ComputerScience,SUM(Electives) AS Electives,SUM(WorkCredits) AS WorkCreditsFROM @AllocatedCreditsGROUP BY PersonID) AS ALEFT OUTER JOIN(SELECTPersonID,SUM(LanguageArts + SocialStudies + USHistory + Economics +Government + Mathematics + Science + ComputerScience +Electives + WorkCredits) AS TotalCreditsFROM @AllocatedCreditsGROUP BY PersonID) AS BON A.PersonID = B.PersonIDThen do recalc:DECLARE @result AS REAL--------Recalculate Language Arts Hours--------SET @result = (SELECT SUM(LanguageArts) AS LanguageArts FROM @AllocatedCredits) - @LanguageArtsMinCredIF (@result > 0)BEGINUPDATE @AllocatedCreditsSET Electives = (SELECT SUM(Electives) FROM @AllocatedCredits) + @result, LanguageArts = @LanguageArtsMinCredEND--------Recalculate SocialStudies Hours--------SET @result = (SELECT SUM(SocialStudies) FROM @AllocatedCredits) - @SocialStudiesMinCredIF (@result > 0)BEGINUPDATE @AllocatedCreditsSET Electives = (SELECT SUM(Electives) FROM @AllocatedCredits) + @result, SocialStudies = @SocialStudiesMinCredEND--------Recalculate Economics hours--------SET @result = (SELECT SUM(Economics) FROM @AllocatedCredits) - @EconomicsMinCredIF (@result > 0)BEGINUPDATE @AllocatedCreditsSET Electives = (SELECT SUM(Electives) FROM @AllocatedCredits) + @result, Economics = @EconomicsMinCredEND--------Recalculate Government Hours--------SET @result = (SELECT SUM(Government) FROM @AllocatedCredits) - @GovernmentMinCredIF (@result > 0)BEGINUPDATE @AllocatedCreditsSET Electives = (SELECT SUM(Electives) FROM @AllocatedCredits) + @result, Government = @GovernmentMinCredEND--------Recalculate Mathematics Hours--------SET @result = (SELECT SUM(Mathematics) FROM @AllocatedCredits) - @MathematicsMinCredIF (@result > 0)BEGINUPDATE @AllocatedCreditsSET Electives = (SELECT SUM(Electives) FROM @AllocatedCredits) + @result, Mathematics = @MathematicsMinCredEND--------Recalculate Science Hours--------SET @result = (SELECT SUM(Science) FROM @AllocatedCredits) - @ScienceMinCredIF (@result > 0)BEGINUPDATE @AllocatedCreditsSET Electives = (SELECT SUM(Electives) FROM @AllocatedCredits) + @result, Science = @ScienceMinCredEND--------Recalculate ComputerScience Hours--------SET @result = (SELECT SUM(ComputerScience) FROM @AllocatedCredits) - @ComputerScienceMinCredIF (@result > 0)BEGINUPDATE @AllocatedCreditsSET Electives = (SELECT SUM(Electives) FROM @AllocatedCredits) + @result, ComputerScience = @ComputerScienceMinCredENDThen select * from recalc to match with what the report needs:SELECT * FROM @ReCalcAllocatedCreditsThen rewrite entire sql statement this weekend so they don't yell at me during code review :-)--Nick |
 |
|
|
|
|
|
|
|