Not able to Group table on percentage Column - sql

Edit : Database - SQL server 2014
I am trying to Calculate Daily Percentage Of Student "Present" like this :
But I Getting Result like this :
Only while Calculating Percentage I am not able to group Rows based on Class.
With Out percentage Calculation , Result are grouping Fine
My Student Attendance table..
This is My Query...
SELECT CM.ClassName ,SB.SubjectName ,
count(sa.Day14) Total,
sum(case when sa.Day14 = 'P' then 1 else 0 end) Present,
sum(case when sa.Day14 = 'A' then 1 else 0 end) Absent,
(SELECT CAST(
CASE
WHEN count(sa.Day14) = 0
THEN 0
ELSE (sum(case when Day14 = 'P' then 1 else 0 end)* 100)/(count(Day14))
END AS nvarchar(100))) as [Present%],
AttendanceDate from studentattendance SA
inner join studentmaster SM on SA.StudentID = SM.ID
join ProfessorMaster p on SA.ProfessorID = p.Id
join Classmaster CM on SA.ClassID = CM.ID
inner join SubjectMaster SB on SA.SubjectID = SB.ID
where
sa.ProfessorID = '36' and sa.AttendanceDate = '2015-09' and SA.AdminId ='29'
group by CM.ClassName, SB.SubjectName, SA.AttendanceDate,sa.Day14
What I am Missing ?

The problem with your query is that you are doing a count on Day14 group by Day14. With this you can't do sum in the same query as you specified a group by already. So you have to split your query.
Lets say your attendance table looks like this for a particular subject/class on a particular day.
StudentID Day14
46 A
47 P
48 P
If you are using Oracle,then create a CTE. Else create a view to count 'A' and 'P'.
select distinct(Day14), count(*) as AorP from yourTable group by Day14
Now create another CTE/view to get the total sum and percent like below
select DAY14, AORP*100/(select sum(AORP) as yourView from tbl2) as percentage from tbl2
This will give you result like
Day14 PERCENTAGE
P 66.67
A 33.33
Now join this view to your main query to get PERCENTAGE based for Present or Absent. You can also use this view as an inline query (like
select col1,(select Day14 as AbsentPercent from newView where id=something and Day14='A')
Note: This view is just for example, You have to add at least 1 unique identifier to join to your main table later. To capture missing entries, use appropriate outer join.

I got much Smaller Solution :) ...
Calculate Percentage in Datatable[Or any ResultSet in Code] ...
My Query (Not calculating Percentage )
SELECT CM.ClassName ,SB.SubjectName ,
count(sa.Day14) Total,
sum(case when sa.Day14 = 'P' then 1 else 0 end) Present,
sum(case when sa.Day14 = 'A' then 1 else 0 end) Absent,
'' [Present%] ,
AttendanceDate from studentattendance SA
inner join studentmaster SM on SA.StudentID = SM.ID
join ProfessorMaster p on SA.ProfessorID = p.Id
join Classmaster CM on SA.ClassID = CM.ID
inner join SubjectMaster SB on SA.SubjectID = SB.ID
where
sa.ProfessorID = '36' and sa.AttendanceDate = '2015-09' and SA.AdminId ='29'
group by CM.ClassName, SB.SubjectName, SA.AttendanceDate,sa.Day14
and in DataTable ......
foreach (DataRow dr in ds.Tables[0].Rows)
{
var Total = Convert.ToInt32(Convert.ToDouble(dr["Total"]));
var Present = Convert.ToInt32(Convert.ToDouble(dr["Present"]));
dr["Present%"] = (Present * 100) / Total;
}

Related

SQL Server - Exclude records from query result set based on condition

Using the below query i am trying to get Average number of FTE Students in each department, Total Students in each department divided by Total FTE students in that department.
In few departments there will be no FTE students and for that i used case statement to consider average as 0 in that case there are no FTE students (CASE U.[IsFTEStudent] WHEN 1 THEN 1 END)=0 THEN 0
SELECT D.[DepartmentId],
CASE WHEN COUNT(CASE U.[IsFTEStudent] WHEN 1 THEN 1 END)=0 THEN 0
ELSE COUNT(S.[StudentId])/COUNT(CASE U.[IsFTEStudent] WHEN 1 THEN 1 END) END AS 'AverageOfFTEStudents'
FROM dbo.[Student] S
INNER JOIN [dbo].User U
ON S.[StudentId] = U.[UserId]
INNER JOIN dbo.[Department] D
ON D.DepartmentId = S.[DepartmentId]
WHERE D.CollegeId = 5
GROUP BY S.DepartmentId
The above query gives result even with average 0, now i want to exclude those departments who have average of 0 FTE Students, which means there are no FTE students.
In short the departments which have 'AverageOfFTEStudents' as 0 should be excluded from my result
I think you want a having clause; and you can simplify the computation logic with AVG():
SELECT S.[DepartmentId],
AVG(CASE WHEN [IsFTEStudent] = 1 THEN 1.0 ELSE 0 END) AS [AverageOfFTEStudents]
FROM dbo.[Student] S
INNER JOIN [dbo].User U ON ON S.[StudentId] = U.[UserId]
INNER JOIN dbo.[Department] D ON D.DepartmentId = S.[DepartmentId]
WHERE D.CollegeId = 5
GROUP BY S.DepartmentId
HAVING SUM(CASE WHEN [IsFTEStudent] = 1 THEN 1 ELSE 0 END) > 0
Depending on the actual datatype and values of IsFTEStudent, we might be able to simplify the aggregate expressions a little. If it's an integer with 0/1 values for example, then:
SELECT S.[DepartmentId],
AVG([IsFTEStudent] * 1.0) AS [AverageOfFTEStudents]
FROM dbo.[Student] S
INNER JOIN [dbo].User U ON ON S.[StudentId] = U.[UserId]
INNER JOIN dbo.[Department] D ON D.DepartmentId = S.[DepartmentId]
WHERE D.CollegeId = 5
GROUP BY S.DepartmentId
HAVING SUM([IsFTEStudent]) > 0

SQL : Percentage Completed

I need to have a SQL query to calculate the percentage of courses completed by location which are different SQL tables.
Courses table has a Status = 'C' (Completed status).
select Locations.Name, ( ??? ) as PercentCompleted
from Locations inner join Candidates ON Locations.Id = Candidates.SpecifiedLocation
inner join Courses on Candidates.Id = Courses.CandidateId
Group By Locations.Name
I want the results to be:
Location PercentCompleted
Loc1 10
Loc2 50
Loc3 75
where 10, 50 and 75 are percentages of courses completed per location.
Can this be achieved with a single SQL query?
If I understand correctly, I think you can do:
select l.Name,
avg(case when co.status = 'C' then 100.0 else 0 end) as PercentCompleted
from Locations l inner join
Candidates c
on l.Id = c.SpecifiedLocation inner join
Courses co
on c.Id = co.CandidateId
group by l.name;
try like below
select Locations.Name, (sum(case when Status = 'C' then 1 else 0 end)/(select count(*)
from Candidates c where c.SpecifiedLocation=Locations.Id))*100
as PercentCompleted
from Locations inner join Candidates ON Locations.Id = Candidates.SpecifiedLocation
inner join Courses on Candidates.Id = Courses.CandidateId
Group By Locations.Name

How to get multiple counts in one sql query across multiple tables

I have 2 tables
Company & products
I need to get 2 counts. One is the total count of products and the secondly count of products for sale_flg=1
This SQL does not seem to work..Tried several other ways..not able to get the expected results
SELECT A.COMPANY_NAME, COUNT(B.PRODUCT_ID) AS TOTAL_COUNT_OF_PRODUCTS,
(CASE WHEN B.SALEFLG =1 THEN 1 END) AS COUNT_OF_SALES
FROM COMPANY A LEFT JOIN
PRODUCT B
ON B.COMPANY_ID = A.COMPANY_ID
GROUP BY A.COMPANY_NAME
I think you just need a sum for the case:
SELECT C.COMPANY_NAME, COUNT(P.PRODUCT_ID) AS TOTAL_COUNT_OF_PRODUCTS,
SUM(CASE WHEN P.SALEFLG = 1 THEN 1 ELSE 0 END) AS COUNT_OF_SALES
FROM COMPANY C LEFT JOIN
PRODUCT P
ON P.COMPANY_ID = C.COMPANY_ID
GROUP BY C.COMPANY_NAME ;
If you have B.SALEFLG = 1 or 0 for you may try
Sum(B.SALEFLG) AS COUNT_OF_SALES
Or use UNION
If you use count then in else you should consider null because null is not consider in count aggregation and if you have B.SALEFLG =1 or 0 then use sum aggregation.
You can try below code:
SELECT A.COMPANY_NAME, COUNT(B.PRODUCT_ID) AS TOTAL_COUNT_OF_PRODUCTS,
count(CASE WHEN B.SALEFLG =1 THEN 1 else null END) AS COUNT_OF_SALES
FROM COMPANY A LEFT JOIN
PRODUCT B
ON B.COMPANY_ID = A.COMPANY_ID
GROUP BY A.COMPANY_NAME
OR try this:
SELECT A.COMPANY_NAME, COUNT(B.PRODUCT_ID) AS TOTAL_COUNT_OF_PRODUCTS,
sum(B.SALEFLG) AS COUNT_OF_SALES
FROM COMPANY A LEFT JOIN
PRODUCT B
ON B.COMPANY_ID = A.COMPANY_ID
GROUP BY A.COMPANY_NAME
This SQL server query is working.
Company Table and Product Table
Company Table CompanyID is join with Product table and sales_flg add in product table .
sales_flg = 1 record display in CntSalesflg
select Comp.CompID as CompID, COUNT(Pro.ProductID) as CntProdustID,
SUM(CASE WHEN Pro.SalesflagID = 1 THEN 1 ELSE 0 END) as CntSalesflg
from Product as Pro
inner join Company as Comp on Pro.CompID = Comp.CompID
GROUP by Comp.CompID

Sum of resulting set of rows in SQL

I've got the following query:
SELECT DISTINCT CU.permit_id, CU.month, /*CU.year,*/ M.material_id, M.material_name, /*MC.chemical_id, C.chemical_name,
C.precursor_organic_compound, C.non_precursor_organic_compound,*/
/*MC.chemical_percentage,*/
POC_emissions =
CASE
WHEN (C.precursor_organic_compound = 'true')
THEN (CU.chemical_usage_lbs / CU.material_density) * M.VOC
ELSE 0
END,
NON_POC_emissions =
CASE
WHEN (C.non_precursor_organic_compound = 'true')
THEN CU.chemical_usage_lbs * (MC.chemical_percentage / 100)
ELSE 0
END
FROM material M
LEFT OUTER JOIN material_chemical MC ON MC.material_id = M.material_id
LEFT OUTER JOIN chemical_usage CU ON CU.material_id = MC.material_id
LEFT OUTER JOIN chemical C ON C.chemical_id = MC.chemical_id
WHERE (CU.month >=1 AND CU.month <= 2)
AND CU.year = 2013
AND M.material_id = 52
--AND CU.permit_id = 2118
--GROUP BY CU.permit_id, M.material_id, M.material_name, CU.month, MC.chemical_id, MC.chemical_id, C.chemical_name, C.precursor_organic_compound, C.non_precursor_organic_compound
--ORDER BY C.chemical_name ASC
Which returns:
But what I need is to return one row per month per material adding up the values of POC per month and NON_POC per month.
So, I should end up with something like:
Month material_id material_name POC NON_POC
1 52 Krylon... 0.107581 0.074108687
2 52 Krylon... 0.143437 0.0988125
I tried using SUM but it sums up the same result multiple times:
SELECT /*DISTINCT*/ CU.permit_id, CU.month, /*CU.year,*/ M.material_id, M.material_name, /*MC.chemical_id, C.chemical_name,
C.precursor_organic_compound, C.non_precursor_organic_compound,*/
--MC.chemical_percentage,
POC_emissions = SUM(
CASE
WHEN (C.precursor_organic_compound = 'true')
THEN (CU.chemical_usage_lbs / CU.material_density) * M.VOC
ELSE 0
END),
NON_POC_emissions = SUM(
CASE
WHEN (C.non_precursor_organic_compound = 'true')
THEN CU.chemical_usage_lbs * (MC.chemical_percentage / 100)
ELSE 0
END)
FROM material M
LEFT OUTER JOIN material_chemical MC ON MC.material_id = M.material_id
LEFT OUTER JOIN chemical_usage CU ON CU.material_id = MC.material_id
LEFT OUTER JOIN chemical C ON C.chemical_id = MC.chemical_id
WHERE M.material_id = 52
--AND CU.permit_id = 187
AND (CU.month >=1 AND CU.month <= 2)
AND CU.year = 2013
GROUP BY CU.permit_id, M.material_id, M.material_name, CU.month/*, CU.year, MC.chemical_id, C.chemical_name, C.precursor_organic_compound, C.non_precursor_organic_compound*/
--ORDER BY C.chemical_name ASC
The first query has a DISTINCT clause. What is the output without the DISTINCT clause. I suspect you have more rows than shows in your screenshot.
Regardless, you could try something like this to get the desired result.
select permit_id, month, material_id, material_name,
sum(poc_emissions), sum(non_poc_emissions)
from (
SELECT DISTINCT CU.permit_id, CU.month, M.material_id, M.material_name,
POC_emissions =
CASE
WHEN (C.precursor_organic_compound = 'true')
THEN (CU.chemical_usage_lbs / CU.material_density) * M.VOC
ELSE 0
END,
NON_POC_emissions =
CASE
WHEN (C.non_precursor_organic_compound = 'true')
THEN CU.chemical_usage_lbs * (MC.chemical_percentage / 100)
ELSE 0
END
FROM material M
LEFT OUTER JOIN material_chemical MC ON MC.material_id = M.material_id
LEFT OUTER JOIN chemical_usage CU ON CU.material_id = MC.material_id
LEFT OUTER JOIN chemical C ON C.chemical_id = MC.chemical_id
WHERE (CU.month >=1 AND CU.month <= 2)
AND CU.year = 2013
AND M.material_id = 52
) main
group by permit_id, month, material_id, material_name
Explanation
Since the results you retrieved by doing a DISTINCT was consider source-of-truth, I created an in-memory table by making it a sub-query. However, this subquery must have a name of some kind...whatever name. I gave it a name main. Subqueries look like this:
select ... from (sub-query) <give-it-a-table-name>
Simple Example:
select * from (select userid, username from user) user_temp
Advanced Example:
select * from (select userid, username from user) user_temp
inner join (select userid, sum(debits) as totaldebits from debittable) debit
on debit.userid = user_temp.userid
Notice how user_temp alias for the subquery can be used as if the sub-query was a real table.
Use above query in subquery and group by (month) and select sum(POC_emissions) and sum(NON_POC_emissions )

SQL Multiple CASE statements return 0 instead of NULL

I Have the following SQL Case statement which works perfectly as long as the Years match MF.Date & M.MemberCurrentYear. There will be scenarios where the years do not match and this returns a Balance of NULL. I would like it to return a Zero.
SELECT SUM(CASE WHEN Type = 1 THEN Amount ELSE Amount * - 1 END) AS Balance
FROM dbo.MemberFinancials AS MF
INNER JOIN dbo.Members AS M ON MF.MemberID = M.MemberID
AND DATEPART(yyyy, MF.Date) = M.MemberCurrentYear
INNER JOIN dbo.FinancialTypes AS FT ON MF.FinancialTypeID = FT.FinancialTypeID
Thanks
SELECT
ISNULL(SUM(CASE WHEN Type = 1 THEN Amount ELSE Amount * - 1 END),0) AS Balance
FROM dbo.MemberFinancials AS MF INNER JOIN
dbo.Members AS M ON MF.MemberID = M.MemberID AND DATEPART(yyyy, MF.Date) = M.MemberCurrentYear INNER JOIN
dbo.FinancialTypes AS FT ON MF.FinancialTypeID = FT.FinancialTypeID
Use ISNULL (Or COALESCE if you prefer...)
SELECT ISNULL( SUM(CASE WHEN Type = 1 THEN Amount ELSE Amount * - 1 END), 0) AS Balance
SELECT
COALESCE(
SUM(CASE WHEN Type = 1 THEN Amount ELSE Amount * - 1 END)
, 0) AS Balance