Dear Experts,
I am using the follow statement to auto generate Horizontal Rows in vertical.
Declare @SC Varchar(100)Declare @Cyear Varchar(100)Set @Sc='02'Set @Cyear='1314'--Create Data Temp TableDrop Table #ResultCreate Table #Result (Transno Varchar(200), ExamCode Varchar(100), SC Varchar(100), SN Varchar(100), ObtMarks Numeric(15,2), d int)Insert Into #Result(Transno, ExamCode, SC, SN, ObtMarks,d)select a.Transno, ExamCode=a.examcode+'-1', a.Sc, b.Sn, a.Om, d=1from result_det a Join Subject b on a.sc=b.subjectidJoin result_mst c on a.transno=c.transno Join Examduration d on a.examcode=d.examcode where c.Cyear=@CyearUnion Allselect a.Transno, ExamCode=a.examcode+'-2', a.Sc, b.Sn, a.Om1, d=2from result_det a Join Subject b on a.sc=b.subjectidJoin result_mst c on a.transno=c.transno Join Examduration d on a.examcode=d.examcode where c.Cyear=@CyearOrder by ExamCode, transno,a.ScCreate Unique Index XI_Result On #Result (Transno, examCode, Sc, sn,ObtMarks, d)Select * from #Result where ObtMarks >0---Create Column DetailDrop Table #ColumnCreate Table #Column (Ident Int Identity(0,1), Sn Varchar(100))Insert Into #Column(SN)Select SN from #Result Group by sn,sc --(INDEX(XI_TransportCost), NOLOCK)order by ScCreate Unique Index XI_Column on #Column (Ident, Sn)Select * from #Column --Create Row DetailDrop Table #RowCreate Table #Row (Transno Varchar(200), ExamCode varchar(20))Insert Into #Row(TRansNo,ExamCode)SELECT TransNo, ExamCode=ExamCode FROM #Result --(INDEX(XI_TransportCost), NOLOCK)GRoup by TransNo,ExamCode,dorder by TransNo,ExamCode,dCreate Unique Index XI_Row on #Row(TransNo, ExamCode)--Select * From #RowDeclare @ColumnIndex IntDeclare @MaxColumn IntDeclare @ColumnText Varchar(100)Declare @Sql varchar(8000)Select @ColumnIndex=0, @MaxColumn=Max(Ident)from #Column---print @MaxColumnWhile @ColumnIndex <= @MaxColumnBegin Select @ColumnText=Sn From #Column where @ColumnIndex = IdentSelect @SQL= 'ALTER TABLE #Row ADD ' + QUOTENAME(@ColumnText) + ' Numeric(15,2) Default 0'Exec (@SQL) SELECT @SQL = 'UPDATE #Row SET ' + QUOTENAME(@ColumnText) + ' = #Result.ObtMarks FROM #Result (INDEX(XI_Result), NOLOCK), #Column WHERE #Row.TransNo = #Result.TransNo and #Row.ExamCode = #Result.ExamCode AND #Column.SN = #Result.SN AND #Column.Ident = ' + CAST(@ColumnIndex AS VARCHAR(12)) EXEC (@SQL)--Print @SQL--Select * from #RowSelect @ColumnIndex = @ColumnIndex + 1EndDROP TABLE #ColumnDROP TABLE #ResultSELECT #Row.*FROM #RowORDER BY #Row.TransNo, #Row.ExamCode
#Result table result are as follows:
#Column table result are as follows:
#Row table result are as follows:
The last statement is showing error:
and also showing result but like this (without ObtMarks)
Please help me to fix this error and get final result with ObtMarks.
Sir,
If you see I am using two different columns in union
Om & Om1
Om is carrying test obtain marks
Om1 is carrying result obtain marks
in first row I am displaying text obtain marks of same exam
in the second row displaying exam obtain marks
Please guide how to handle this?
Mehram
PortletPaul
SELECT a.Transno , ExamCode = a.examcode + cj.suffix , a.Sc , b.Sn , case when cj.d =1 then a.Om else a.Om1 end , cj.dFROM result_det a JOIN Subject b ON a.sc = b.subjectid JOIN result_mst c ON a.transno = c.transno JOIN Examduration d ON a.examcode = d.examcodeCROSS JOIN ( SELECT '-1' AS suffix, 1 as d UNION ALL SELECT '-2' AS suffix, 2 as d ) AS cjWHERE c.Cyear = @Cyear;
If you see I am using two different columns in union
Om & Om1
Om is carrying test obtain marks
Om1 is carrying result obtain marks
in first row I am displaying text obtain marks of same exam
in the second row displaying exam obtain marks
Please guide how to handle this?
Mehram