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 |
|
collie
Constraint Violating Yak Guru
400 Posts |
Posted - 2005-02-07 : 09:17:03
|
| Hi,I wrote a sp that returns the number of students per school for a particular region.The result that I get is similiar to the followingTotal School Region23 XYZ 2812 LBS 2810 CDE 3210 GHK 32However, now I have to calculate the % for each school. So if Region 28 the overall total is 35 I need to calculate what percentage total 23 (school XYZ) is of 35.I am not sure how to calculate the percentage in sql.Below is part of my sp :SELECT * INTO #tR FROM ( select learningstuds.sid as lsid, learningstuds.misgeret as misgeret, learningstuds.hasamaguf as hasamaguf, learningstuds.mid as mid, learningstuds.departure as departure, student.family as familyStat, student.region as region, student.sOrigin as Sorigin, student.studyState as studyState, student.attitude as attitude, student.behave as behave, student.enrolltype as enrolltype, tblmisgeret.sugmosad as sugmosad, tblguf.Name as gufname from learningstuds inner join student on student.sid=learningstuds.sid inner join tblguf on pnimcode=hasamagufinner join tblmisgeret on misgeretcode=misgeret ) t wheredeparture is null IF @param=1Begin select count ( *) as total,valuedesc,hasamaguf,gufname from #tR INNER JOIN tblddlbvalue ON familyStat= tblddlbvalue.valuecode group by valuedesc,hasamaguf,gufname order by hasamaguf EndThanks in advance for the help. |
|
|
spirit1
Cybernetic Yak Master
11752 Posts |
Posted - 2005-02-07 : 11:31:13
|
try something like this:select t1.total, t1.total/(select sum(total) from MyTable group By Region where school = t1.school) as Percentage, ...from MyTable t1where...Go with the flow & have fun! Else fight the flow |
 |
|
|
|
|
|
|
|