Note: I have no control over the design of the original table.Problem: Get all records where the total score is greater than or equal to 120.ddl and sample data:Create table #scores(conid varchar(11),sc1 varchar(5),sc2 varchar(5),sc3 varchar(5),sc4 varchar(5),sc5 varchar(5),sc6 varchar(5),sc7 varchar(5),sc8 varchar(5))Insert into #scores (conid, sc1, sc2, sc3, sc4, sc5, sc6, sc7, sc8)select '00000031959', '30.00', '20.00', '00.00', '20.00', '20.00', '00.00', '00.00', '00.00' union allselect '00000006499', '30.00', '20.00', '30.00', '20.00', '00.00', '00.00', '00.00', '20.00' union allselect '00000027416', '20.00', '20.00', '10.00', '20.00', '10.00', '00.00', '00.00', '10.00' union allselect '00225532601', '30.00', '20.00', '20.00', '20.00', '20.00', '00.00', '10.00', '00.00' union allselect '00000023042', '20.00', '20.00', '20.00', '20.00', '10.00', '00.00', '10.00', '00.00' union allselect '00000005850', '20.00', '20.00', '00.00', '20.00', '30.00', '10.00', '00.00', '10.00' union allselect '00000002860', '20.00', '30.00', '20.00', '30.00', '30.00', '30.00', '00.00', '00.00' union allselect '00000511704', '30.00', '30.00', '00.00', '30.00', '20.00', '10.00', '30.00', '10.00' union allselect '00000021864', '20.00', '10.00', '00.00', '20.00', '20.00', '00.00', '00.00', '10.00' union allselect '00312048414', '30.00', '10.00', '00.00', '20.00', '20.00', '00.00', '00.00', '00.00' union allselect '0000mi35921', '20.00', '10.00', '30.00', '10.00', '20.00', '00.00', '00.00', '00.00' union allselect '00000029395', '20.00', '10.00', '00.00', '10.00', '20.00', '00.00', '00.00', '00.00' union allselect '00000032266', '20.00', '30.00', '30.00', '30.00', '20.00', '00.00', '00.00', '20.00' union allselect '00000007551', '30.00', '30.00', '30.00', '20.00', '00.00', '00.00', '10.00', '00.00' union allselect '00000026967', '20.00', '10.00', '00.00', '00.00', '00.00', '00.00', '00.00', '00.00'
I have two possible solutions (both work):Solution 1:select conid, sc1,sc2,sc3,sc4,sc5,sc6,sc7,sc8, (cast(sc1 as real) + cast(sc2 as real) + cast(sc3 as real) + cast(sc4 as real) + cast(sc5 as real) + cast(sc6 as real) + cast(sc7 as real) + cast(sc8 as real)) as tot_score from #scores where (cast(sc1 as real) + cast(sc2 as real) + cast(sc3 as real) + cast(sc4 as real) + cast(sc5 as real) + cast(sc6 as real) + cast(sc7 as real) + cast(sc8 as real)) >= 120.0
I tried replacing the where statement with where tot_score >= 120.0 but got the following error:Invalid column name 'tot_score'.Solution 2:I created a function called addScores:CREATE FUNCTION AddScores (@sc1 as varchar(5), @sc2 as varchar(5), @sc3 as varchar(5), @sc4 as varchar(5), @sc5 as varchar(5), @sc6 as varchar(5), @sc7 as varchar(5), @sc8 as varchar(5))RETURNS realASbegin return cast(@sc1 as real) + cast(@sc2 as real) + cast(@sc3 as real) + cast(@sc4 as real) + cast(@sc5 as real) + cast(@sc6 as real) + cast(@sc7 as real) + cast(@sc8 as real)end
and then wrote the select statement:select conid, sc1,sc2,sc3,sc4,sc5,sc6,sc7,sc8, dbo.addscores(sc1,sc2,sc3,sc4,sc5,sc6,sc7,sc8) as tot_score from #scores where dbo.addscores(sc1,sc2,sc3,sc4,sc5,sc6,sc7,sc8)>= 120.0
This looks cleaner but I have to call the function in both the where clause and the select clause (the >= tot_score does not work here either).Expected results: conid sc1 sc2 sc3 sc4 sc5 sc6 sc7 sc8 tot_score00000006499 30.00 20.00 30.00 20.00 00.00 00.00 00.00 20.00 120.000225532601 30.00 20.00 20.00 20.00 20.00 00.00 10.00 00.00 120.000000002860 20.00 30.00 20.00 30.00 30.00 30.00 00.00 00.00 160.000000511704 30.00 30.00 00.00 30.00 20.00 10.00 30.00 10.00 160.000000032266 20.00 30.00 30.00 30.00 20.00 00.00 00.00 20.00 150.000000007551 30.00 30.00 30.00 20.00 00.00 00.00 10.00 00.00 120.0
Thanks,LaurieP.S. There is not a front end to do this in. I will be dumping the results to an Excel file straight from Query Analyzer and must not include any records where the tot_score is less than 120.0