My task is to display the total number of students in the school and the number of students in a filtered class (like class 1, class 2, class 3 and so on..). Here is my stored procedure to do the job:
USE [My_DB] GO /****** Object: StoredProcedure [dbo].[CurrentUnitRoster] Script Date: 7/7/2017 2:56:58 PM ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO -- ============================================= -- Author: <OC> -- Create date: <Create Date,,> -- Description: <Description,,> -- ============================================= ALTER PROCEDURE [dbo].[CurrentUnitRoster] ( -- Add the parameters for the stored procedure here @UIC INT ) AS BEGIN --IF (@SearchParameter='All' OR @SearchParameter IS NULL OR @SearchParameter ='' OR @searchValue = 'All') BEGIN -- SET NOCOUNT ON added to prevent extra result sets from -- interfering with SELECT statements. SET NOCOUNT ON; WITH MyTest as ( SELECT P.UICID, P.ParentUIC_ID, CAST(P.UICID AS VarChar(Max)) as Level FROM RefSwcsUIC P WHERE P.UICID = @UIC UNION ALL SELECT P1.UICID, P1.ParentUIC_ID, CAST(P1.UICID AS VarChar(Max)) + ', ' + M.Level FROM RefSwcsUIC P1 INNER JOIN MyTest M ON M.UICID = P1.ParentUIC_ID ) --If "ALL" is selected from training unit --BEGIN SELECT COUNT (DISTINCT sn.studentid) FROM studentnames sn LEFT OUTER JOIN RefSuffix c ON sn.SuffixID = c.SuffixID INNER JOIN studentcompcascade scc ON sn.studentid = scc.studentid LEFT OUTER JOIN Students s ON sn.studentid = s.studentid LEFT OUTER JOIN StudentIDs IDs ON sn.studentid = IDs.studentid LEFT OUTER JOIN studentarrdep sa ON sn.studentid = sa.studentid LEFT OUTER JOIN studentpipeline sp ON sn.studentid = sp.studentid LEFT OUTER JOIN refswcsuic rs ON sa.arrswcsunit = rs.uicid LEFT OUTER JOIN studentclearances sc ON sn.studentid = sc.studentid LEFT OUTER JOIN RefIDTypes b ON IDs.IDTypeID = b.IDTypeID --newly added to display course ID LEFT OUTER JOIN RefComp rc ON scc.CompID = rc.CompID LEFT OUTER JOIN studentclass scl ON scl.studentid = scc.studentid LEFT OUTER JOIN classes cl ON scl.classid = cl.classid AND GETDATE() BETWEEN cl.StartDate AND cl.EndDate LEFT OUTER JOIN RefCourseCode cc on cl.CourseCodeID = cc.CourseCodeID LEFT OUTER JOIN courses c2 on c2.CourseCodeID = cc.CourseCodeID AND (cl.StartDate BETWEEN c2.EffDate AND ISNULL(c2.ObsDate, '1/1/9999')) LEFT OUTER JOIN RefRank ON scc.RankID = RefRank.NatSvcRankID LEFT OUTER JOIN NavyRatingsView f ON scc.PMOSID = f.MOS_ID AND scc.RankID = f.NatSvcRankID LEFT OUTER JOIN RefPipelines g on scc.PipelineID = g.PipelineID LEFT OUTER JOIN RefDutyStatus ON sa.ArrDutyStatus = RefDutyStatus.DutyStatusID LEFT OUTER JOIN RefTrainingStatus ON sp.TrainingStatusID = RefTrainingStatus.TrainingStatusID LEFT OUTER JOIN RefTrainingMOS ON scc.TrainingMOSID = RefTrainingMOS.TrainingMOSID LEFT OUTER JOIN RefLanguage ON sp.TrainingLanguageID = RefLanguage.LanguageID LEFT OUTER JOIN RefClearanceTypes ON sc.ClearanceTypeID = RefClearanceTypes.ClearanceTypeID LEFT OUTER JOIN RefMOS e ON scc.PMOSID = e.MOS_ID WHERE (rs.uicid IN (SELECT UICID FROM MyTest)) AND sn.ObsDate IS NULL AND sp.ObsDate IS NULL AND IDs.ObsDate IS NULL AND scc.ObsDate IS NULL AND sa.ObsDate IS NULL END END
When I run the program I get the total number of students:
protected void sdsTACStudents_Selected(object sender, SqlDataSourceStatusEventArgs e)
{
if (txtSearchByName.Text == "")
{
int rowCount = e.AffectedRows;
txtRecordCount.Text = rowCount.ToString();
}
else if ((ddlSearchBy.Text == "LName") || (ddlSearchBy.Text == "FName") || (ddlSearchBy.Text == "SSN"))
{
using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["STARS_DB_ConnectionString"].ConnectionString))
{
conn.Open();
SqlCommand cmd = new SqlCommand("CurrentUnitTACRoster", conn);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@UIC", SqlDbType.Int);
cmd.Parameters["@UIC"].Value = Session["UIC"].ToString();
try
{
string Total = cmd.ExecuteScalar().ToString(); // Total number of students
int rowCount = e.AffectedRows;
lblUnitRecordCount.Text = rowCount.ToString(); // Gives me the number of student on the page, I need the number in the class.
}
catch
{
}
conn.Close();
}
}
}The question is how do I get the count of students in a class (I use a filter to display the class group)?