MS SQL Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsDatabasesMS SQL Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
Generate data entry and reporting .NET Web apps in minutes, straight from your database. Read our FREE whitepaper “Build Web 2.0 Applications Without Hand-Coding” Download now!
  #1  
Old February 28th, 2004, 07:09 PM
OskarK OskarK is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2004
Posts: 3 OskarK User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Question SUM function

Hi,

I am trying to write a stored procedure that would return a percentage vote instead of numerical.
The db contains five fields: first name, last name, votes, office, class. It would be an easy procedure if not the last two, because sum function suppose to add votes of candidates only if they are similar office and class. Is there an easy way out of this mess. I can write a function from asp.net but it would mean that I have to connect to db over and over. Not very efficient. I tried GROUP BY but had no luck.

Thanks,

Oskar


CREATE PROCEDURE Results
AS
DECLARE @TotalVotes FLOAT

SELECT @TotalVotes = SUM(votes)
FROM Candidates
GROUP BY office, class

IF NOT @TotalVotes = 0
SELECT firstname, lastname, ROUND((votes*100)/@TotalVotes,1) AS votes
FROM Candidates
ORDER BY votes DESC
ELSE
SELECT firstname, lastname, votes
FROM Candidates
ORDER BY votes DESC
GO

Reply With Quote
  #2  
Old February 28th, 2004, 07:54 PM
r937's Avatar
r937 r937 is offline
SQL Consultant
Dev Shed God 25th Plane (17000 - 17499 posts)
 
Join Date: Feb 2003
Location: Toronto Canada
Posts: 17,331 r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level) 
Time spent in forums: 1 Month 3 Weeks 5 Days 7 h 21 m 33 sec
Reputation Power: 891
use a correlated subquery:
Code:
select firstname
     , lastname
     , round(
         (votes*100)
        /(select sum(votes)
            from candidates
           where office = X.office
             and class  = X.class)
         ,1) as votes
  from candidates X
__________________
r937.com | rudy.ca

Reply With Quote
  #3  
Old February 28th, 2004, 10:01 PM
OskarK OskarK is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2004
Posts: 3 OskarK User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Thanks a lot. Works like a charm.

Just one thought. Any idea how to avoid division by zero error in this subquerry?

Reply With Quote
  #4  
Old February 28th, 2004, 11:22 PM
r937's Avatar
r937 r937 is offline
SQL Consultant
Dev Shed God 25th Plane (17000 - 17499 posts)
 
Join Date: Feb 2003
Location: Toronto Canada
Posts: 17,331 r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level) 
Time spent in forums: 1 Month 3 Weeks 5 Days 7 h 21 m 33 sec
Reputation Power: 891
Code:
select firstname
     , lastname
     , case when 0 =
         (select sum(votes)
            from candidates
           where office = X.office
             and class  = X.class)
        then 0 else      
       round(
         (votes*100)
        /(select sum(votes)
            from candidates
           where office = X.office
             and class  = X.class)
         ,1) 
              end as votes
  from candidates X

Reply With Quote
  #5  
Old February 28th, 2004, 11:30 PM
OskarK OskarK is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2004
Posts: 3 OskarK User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
I am speechless. Thank you very much.

Reply With Quote
Reply

Viewing: Dev Shed ForumsDatabasesMS SQL Development > SUM function


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway