Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 Transact-SQL (2000)
 Can anybody optimize this code?

Author  Topic 

perels
Starting Member

23 Posts

Posted - 2005-06-23 : 04:56:13
Hi all,

I wonder if there is anyway to optimize below code e.g. writing it all in one sentences?


select @dM = count(MostValue) from WordSelections WHERE EmployeeID = @EmployeeID AND MostValue = 'd'
select @iM = count(MostValue) from WordSelections WHERE EmployeeID = @EmployeeID AND MostValue = 'i'
select @sM = count(MostValue) from WordSelections WHERE EmployeeID = @EmployeeID AND MostValue = 's'
select @cM = count(MostValue) from WordSelections WHERE EmployeeID = @EmployeeID AND MostValue = 'c'

select @dL = count(LessValue) from WordSelections WHERE EmployeeID = @EmployeeID AND LessValue = 'd'
select @iL = count(LessValue) from WordSelections WHERE EmployeeID = @EmployeeID AND LessValue = 'i'
select @sL = count(LessValue) from WordSelections WHERE EmployeeID = @EmployeeID AND LessValue = 's'
select @cL = count(LessValue) from WordSelections WHERE EmployeeID = @EmployeeID AND LessValue = 'c'


Thanks

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2005-06-23 : 05:08:19
Something like

Select @dM=sum(case when MostValue='d' then 1 else 0 end),
@iM=sum(case when MostValue='i' then 1 else 0 end),...
from WordSelections WHERE EmployeeID = @EmployeeID

Otherwise post table structure, sample data and expected result

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

perels
Starting Member

23 Posts

Posted - 2005-06-23 : 05:21:52
Madhivanan - that was exactly what I needed! Thank you for replying and so quickly! :-)
Go to Top of Page
   

- Advertisement -