SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 case and sum is the following bad for performance
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

sarahmfr
Posting Yak Master

214 Posts

Posted - 06/08/2012 :  15:35:49  Show Profile  Reply with Quote
i want to know how may are A and how many are P and how many are F
I can not use pivot because this a function called for each section in another bigger stored procedure
USE [SP3]
GO
/****** Object: UserDefinedFunction [dbo].[fn_NewSLO_SectionSLO_Achieve_bySLO_Inline] Script Date: 06/08/2012 11:40:51 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO


ALTER FUNCTION [dbo].[fn_NewSLO_SectionSLO_Achieve_bySLO_Inline]
(@SectionID INT,
@SLOID int,
@FullyAchieved int,
@PartiallyAchieved int) RETURNS TABLE
AS RETURN (
select sum(T.Fully)as
'FullyAchieved',
sum (T.Partially) as 'PartiallyAchieved',
sum(T.Failed) as 'Failedtoachieve'

from
(SELECT
e.StudentID,
(case when
sum ((isnull(AP.Points,0)*1.0/a.points)*asl.[weight])
over (partition by e.studentid) >= @fullyachieved
then 1 else 0 end) as 'Fully',

(case
when
sum ((isnull(AP.Points,0)*1.0/a.points)*asl.[weight])
over (partition by e.studentid) < @fullyachieved
and
sum ((isnull(AP.Points,0)*1.0/a.points)*asl.[weight])
over (partition by e.studentid) >= @partiallyAchieved
then 1 else 0 end) as 'Partially',

(case when
sum ((isnull(AP.Points,0)*1.0/a.points)*asl.[weight])
over (partition by e.studentid) < @Partiallyachieved
then 1 else 0 end) as 'Failed'
FROM GradeBook.AssessmentPoint ap
inner join
gradebook.assessment a
on
ap.assessmentid=a.assessmentid
inner join
slo.assessmentslo asl
on
asl.assessmentid=a.assessmentid
inner join
dbo.fn_Enrolled_Parent_Child_inline (@sectionid) e
on
ap.StudentID=e.studentid
where
asl.sloid=@sloid
and
asl.sloid=@sloid
and
a.deleted=0
and a.points >0
and [weight]>0

)T
);



Edited by - sarahmfr on 06/08/2012 15:56:52

visakh16
Very Important crosS Applying yaK Herder

India
47157 Posts

Posted - 06/08/2012 :  16:19:05  Show Profile  Reply with Quote
why are you using partition by inside case when? can you explain with some sample data what exactly you're trying to achieve?


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

Nader
Starting Member

41 Posts

Posted - 06/08/2012 :  17:47:09  Show Profile  Reply with Quote
Thank you I found what i was doing wrong.
I found a solution for that.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
47157 Posts

Posted - 06/09/2012 :  02:08:33  Show Profile  Reply with Quote
ok...would you mind posting what final solution is?

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.06 seconds. Powered By: Snitz Forums 2000