| Author |
Topic |
|
frank.svs
Constraint Violating Yak Guru
368 Posts |
Posted - 2009-08-18 : 15:26:07
|
| Hi Pals,Need urgent help.I have below source table from Excl fle.drop table test create table test(dist varchar(10), school varchar(10), history varchar(100), science varchar(100), commerce varchar(100) ) DELETE from testGOinsert test(dist,school,history,science,commerce) values('D1','S1','100','400',null)insert test(dist,school,history,science,commerce) values('D1','S1','300','504','305')insert test(dist,school,history,science,commerce) values('D1','S1','358','650',null)GOupdate statistics testGOselect * from test dist school history science commerceD1 S1 100 400 D1 S1 300 504 305D1 S1 358 650 clearly from the above output it is clear the 3 students have written "History" exam3 students written science exam but only 1 student has written commerce exam.Note : Looking at the source data, i can say total 3 students have written the exams and if iMart_Graduation take the averages of each subject i will get the below results.After pivoting also i need to able to get back the total no of students.and if iMart_Graduation calculate the averages then i should get back the original values.dist school history science commerceD1 S1 100 400 D1 S1 300 504 305D1 S1 358 650 ---- --- --- 252.666 518 305 ----- --- ------ After unpivoting on 3 subjects, am storing data into table "stg"Hi Pals,Need help! create table stg(dist varchar(10), school varchar(10), subjct varchar(50), student_cnt int, score varchar(10) ) insert into stgselect dist, school, subjct, 0 as student_cnt, scorefrom (select dist,school,case when history IS null then 0 else history end as history ,case when science IS null then 0 else science end as science, case when commerce is null then 0 else commerce end as commerce from test) p unpivot (score for subjct in (history,science,commerce) ) AS Unpvtselect * from stg dist school subjct student_cnt scoreD1 S1 history 0 100D1 S1 science 0 400D1 S1 commerce 0 0D1 S1 history 0 300D1 S1 science 0 504D1 S1 commerce 0 305D1 S1 history 0 358D1 S1 science 0 650D1 S1 commerce 0 0now from the staging am pulling data to dataware house as follows.drop table trg create table trg(dist varchar(10),school varchar(10),subjct varchar(20),student_cnt int,avg_score float )truncate table trg insert into trgselect dist, school, subjct, COUNT(*) as student_cnt, AVG(convert(decimal(9,2),score)) avg_scorefrom stggroup by dist,school,subjctselect * from trg dist school subjct student_cnt avg_scoreD1 S1 commerce 3 101.666666666667D1 S1 history 3 252.666666666667D1 S1 science 3 518 Original Values from Source dist school history science commerceD1 S1 100 400 D1 S1 300 504 305D1 S1 358 650 ---- --- --- 252.666 518 305 ----- --- ---- It is very much clear above 2 result sets that , avg for history , science is correct but the average of commerce is wrong.It has to be 305/1 which is 305. instead it is performing 305/3 =>101.6666But using this data i can get back by no of students taken the exams i.e select SUM(student_cnt)/3 from trg--3but my average is wrong!!!!!!!!!!For this purpose, what i have done is, am ignoring the students not given the respective exam so that my averages will always be corrct.This is what is have done truncate table stg insert into stgselect * from (select dist, school, subjct, 0 as student_cnt, scorefrom (select dist,school,history,science,commerce from test) p unpivot (score for subjct in (history,science,commerce) ) AS Unpvt) a where len(a.score)>0-- 9 recs truncate table trgselect * from stg/*dist school subjct student_cnt scoreD1 S1 history 0 100D1 S1 science 0 400D1 S1 history 0 300D1 S1 science 0 504D1 S1 commerce 0 305D1 S1 history 0 358D1 S1 science 0 650*/-- only 7 recs insert into trgselect dist, school, subjct, COUNT(*) as student_cnt, AVG(convert(decimal(9,2),score)) avg_scorefrom stggroup by dist,school,subjctselect * from trg dist school subjct student_cnt avg_scoreD1 S1 commerce 1 305D1 S1 history 3 252.666666D1 S1 science 3 518This time am getting the exact values Original Values from Source dist school history science commerceD1 S1 100 400 D1 S1 300 504 305D1 S1 358 650 ---- --- --- 252.666 518 305 ----- --- ----But now again problem, using this logic am able to get the Averages correctly but now suppose i want to display the no of students attended for the Exam i.e. i need to get "3" as total.How to get it???????????Please suggest me the correct way so that i can get no of students from trg as well as averages correct.It is urgent.Thanks in advance! |
|
|
TG
Master Smack Fu Yak Hacker
6065 Posts |
Posted - 2009-08-18 : 15:38:55
|
| sorry you lost me. Could you just post what you are starting with (DDL/DML) and what your desired result output based on the sample DML?Be One with the OptimizerTG |
 |
|
|
frank.svs
Constraint Violating Yak Guru
368 Posts |
Posted - 2009-08-18 : 21:22:11
|
| This is my source data.Now my ddl / dml will be as follows DROP TABLE #test create table #test( yr int,dist varchar(10),school varchar(10),history int,science int,commerce int)GOinsert #test(yr,dist,school,history,science,commerce) values(1991,'D1','S1','100','400',null)insert #test(yr,dist,school,history,science,commerce) values(1991,'D1','S1','300','504','305')insert #test(yr,dist,school,history,science,commerce) values(1991,'D1','S1','358','650',null)insert #test(yr,dist,school,history,science,commerce) values(1992,'D1','S1','100','400',null)insert #test(yr,dist,school,history,science,commerce) values(1992,'D1','S1','300','504','305')insert #test(yr,dist,school,history,science,commerce) values(1992,'D1','S1','358','650',null)insert #test(yr,dist,school,history,science,commerce) values(1993,'D1','S1','100','400',null)insert #test(yr,dist,school,history,science,commerce) values(1993,'D1','S1','300','504','305')insert #test(yr,dist,school,history,science,commerce) values(1993,'D1','S1','358','650',null)insert #test(yr,dist,school,history,science,commerce) values(1994,'D1','S1','100','400',null)insert #test(yr,dist,school,history,science,commerce) values(1994,'D1','S1','300','504','305')insert #test(yr,dist,school,history,science,commerce) values(1994,'D1','S1','358','650',null)GOSELECT *FROM #test; WITH stg AS ( select yr, dist, school, subjct, studentID, 0 as student_cnt, score from ( select yr, dist , school , history , science , commerce , Row_Number() OVER ( ORDER BY Dist ) AS StudentID from #test) p unpivot (score for subjct in (history,science,commerce) ) AS Unpvt)SELECT yr,dist, school, subjct, Count(studentID) as studentcnt, Avg(score) avgscoreFROM stgGROUP BY yr,dist, school, subjctThis should be my final output Final Report------------Year NofExaminees History Science Commerce1991 3 252 518 3051992 3 252 518 3051993 3 252 518 3051994 3 252 518 305Note : The Values inside the History,Science,Commerce are averages.No of Examinees , u can find it in #test table for each year. |
 |
|
|
Sachin.Nand
2937 Posts |
Posted - 2009-08-19 : 05:25:26
|
| select yr as Year,COUNT(dist)as NoOfExaminee,AVG(history)as History,AVG(science) as Science,AVG(commerce)as Commercefrom #testgroup by yrPBUH |
 |
|
|
|
|
|