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
 SQL Server Development (2000)
 can you get percentages using sql

Author  Topic 

csphard
Posting Yak Master

113 Posts

Posted - 2005-05-30 : 02:44:04
I need to get percentages. I was going to get my counts using a group by and calculate my percentages. Can sql do this for me and if so how

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2005-05-30 : 02:58:10

declare @t table(no int, s char(5))
insert into @t values(1,'Test')
insert into @t values(1,'Test')
insert into @t values(2,'Test')
insert into @t values(2,'Test')
insert into @t values(2,'Test')
insert into @t values(3,'Test')
insert into @t values(3,'Test')
insert into @t values(4,'Test')

Select no,c, c*100.0/(select count(*) from @t) percentage from(
select no, count(*)as c from @t group by no
) T



Madhivanan

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

csphard
Posting Yak Master

113 Posts

Posted - 2005-05-30 : 04:23:55
For my understanding.

Create a temporary table.

Question my information and insert it.

Get my percentages.

I am looking at @t (SELECT @local_variable)

This is new to me
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2005-05-30 : 04:40:42
@t is the table variable.
Move your result of your group by query to temporary table use the query based on that table

Madhivanan

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

- Advertisement -