Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Converting a number to a percentage

Status
Not open for further replies.

90111605

Programmer
Joined
Dec 22, 2004
Messages
4
Location
GB
Hi there...I got stuck with something I've got this query

SELECT Count ( Q1) as Q1, Count(Q2) as Q2 , COUNT (Q3)as Q3 FROM questionaire

it's okay but I'd like the result to be converted to percentage...any thoughts...many thanks...rodolfo
 
Percentages of what? Sum of all three counts?
 
Try
Select Cast(Count(Q1) as Percentage). [rofl]
-Karl

[red] Cursors, triggers, user-defined functions and dynamic SQL are an axis of evil![/red]
[green]Life's uncertain...eat dessert first...www.deerfieldbakery.com[/green]
 
hi donutman...it did not work..precentage is not a sql data type...cheers anyway
 
Then try vongrunts solution to your question.
-Karl

[red] Cursors, triggers, user-defined functions and dynamic SQL are an axis of evil![/red]
[green]Life's uncertain...eat dessert first...www.deerfieldbakery.com[/green]
 
try this...

SELECT Count(Q1)/COUNT(*) as Q1,
Count(Q2)/COUNT(*) as Q2,
Count(Q3)/COUNT(*) as Q3,
FROM questionaire


you may have to convert each into a float data type to be used in a percentage. ex. convert(float,count(q1))
 
hi JRtoad ....still not working...cheers
 
What are you getting and what are you expecting to get?

Questions about posting. See faq183-874
 
So you want to see, for each question, how many people answered it as a percentage of the total number of responses?

If so, JRtoad's solution is nearly right:

Code:
SELECT 100.0 * COUNT(Q1) / COUNT(*),
  100.0 * COUNT(Q2) / COUNT(*),
  100.0 * COUNT(Q3) / COUNT(*)
FROM questionaire

--James
 
Hi JamesLean ....supeeerrr...it really worked well...could u please tell how round the number in 2 decimals...many..thanks...rodolfo
 
Round(YourNumber,2)
-Karl

[red] Cursors, triggers, user-defined functions and dynamic SQL are an axis of evil![/red]
[green]Life's uncertain...eat dessert first...www.deerfieldbakery.com[/green]
 
Karl, Thank you, thank you, thank you!!!! My co-workers send their thanks, too.

Questions about posting. See faq183-874
 
So is that the best fruit cake you ever had? How can it miss, it's got lots of rum in it![elephant2]
-Karl

[red] Cursors, triggers, user-defined functions and dynamic SQL are an axis of evil![/red]
[green]Life's uncertain...eat dessert first...www.deerfieldbakery.com[/green]
 
BTW, Best Practices in eating:
serve the butter cookies and fruit cake cold.
-Karl

[red] Cursors, triggers, user-defined functions and dynamic SQL are an axis of evil![/red]
[green]Life's uncertain...eat dessert first...www.deerfieldbakery.com[/green]
 
The cookies are wonderful, was saving the rum and fruit cake for after work! My Sql Code might get a bit...odd if I had it now.

Questions about posting. See faq183-874
 
Now,Now, she gets cake and I get nothing that's not fair. Me your best friend.

I'll send you my adress in a minute.

I like a lot of choclat (belgian of course)

Sorry for invading this thread.

Christiaan Baes
Belgium

If you want to get an answer read this FAQ faq796-2540
There's no such thing as a winnable war - Sting
 
to form a proper percentage, wouldn't it be :

Cast(Sum(Field) / Count(Field))?

[thumbsup2]DreX
aKa - Robert
if all else fails, light it on fire and do the happy dance!
 
Christaan are you stalking donutman?

Hey Karl, when are you gonna come back to the group? I live in Chicago so don't make me come out there. I'll .......<saliva>doooooonuts</saliva>[sleeping]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top