SQL query to return percentage

Hi Experts,
The script below returns the number of a certain product from my Products table.
I would like to display the percentage instead. How can this script be modified in order to achieve that?
Thank you for your help.
<%
Set sqlCount = Server.CreateObject("ADODB.Command")
sqlCount.ActiveConnection=Conn
sqlCount.Prepared = true
sqlCount.CommandText = "SELECT Count(ID) AS Countid FROM Products where myProduct = 'Beer'"
set objRs = sqlCount.Execute
CountID = int(objRs("CountID"))
%>
<%=CountID%>
<%
ObjRs.Close
Set ObjRs = Nothing
%>

Open in new window

romsomIT DeveloperAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Jim HornMicrosoft SQL Server Developer, Architect, and AuthorCommented:
You'll want to spell out 'percentage'.  If you mean the count of 'Beer' over the count of everything else, replace the SELECT statement in line 5 with..
SELECT COUNT(id) / CAST((SELECT COUNT(*) FROM Products) as numeric(19,4)) as avg
FROM Products
WHERE myProduct = 'Beer'

Open in new window

0
Big MontySenior Web Developer / CEO of ExchangeTree.org Commented:
try:

SELECT ( count( ( id ) * 100  / ( SELECT count( * ) FROM Products ) ) as avg
FROM Products
WHERE myProduct = 'Beer'
0
romsomIT DeveloperAuthor Commented:
The_Big_Daddy: I get an error message saying "Incorrect syntax near the keyword 'as'."

JimHorn: The error message for line 7 is "Item cannot be found in the collection corresponding to the requested name or ordinal."
0
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

Jim HornMicrosoft SQL Server Developer, Architect, and AuthorCommented:
>JimHorn: The error message for line 7 is..
>CountID = int(objRs("CountID"))

In my example I used the alias avg, and not CountID.   Since we want the average, it would make sense to name it average instead of CountID.   So change line 7 to..
CountID = int(objRs("avg"))

Open in new window

0
Big MontySenior Web Developer / CEO of ExchangeTree.org Commented:
sorry, had a typo:

SELECT ( count( id ) * 100  / ( SELECT count( * ) FROM Products ) ) as avg
FROM Products
WHERE myProduct = 'Beer'

you'll also need to change this line

CountID = int(objRs("CountID"))

to

CountID = objRs("avg")
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
romsomIT DeveloperAuthor Commented:
I've changed line 7 and now the error message is the following: "Variable uses an Automation type not supported in VBScript"
I'm using classic ASP, maybe that's the reason
0
romsomIT DeveloperAuthor Commented:
Thank you very much both of you; it worked this time!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.