Calculating percent in a SQL Query

Results 1 to 2 of 2

Thread: Calculating percent in a SQL Query

  1. #1
    Join Date
    Dec 1969

    Default Calculating percent in a SQL Query

    This should be simple but I can&#039;t seem to get it. I have a table with two fields: browser and ID. I want to calculate the percentages of browsers by counting the ID field. I&#039;ve tried several different ways using count and sum but it doesn&#039;t seem to work. I&#039;ve even tried just hardcoding in the number to divide by. Something like: <BR><BR>SELECT browser, COUNT(ID) as countbrowser, COUNT(ID)/ 30 AS percent<BR>FROM web_visitors<BR>GROUP BY browser<BR><BR>And that doesn&#039;t work either. Any help will be GREATLY appreciated!

  2. #2
    Join Date
    Dec 1969

    Default It's not at all simple...

    The problem is you need *both* the count of each browser type *and* the count of all records.<BR><BR>You can&#039;t do that in a single SELECT.<BR><BR>You *might* be able to do something like this:<BR><BR>SELECT browser, COUNT(browser) as CountBrowser, <BR>&nbsp; &nbsp; 100.0 * COUNT(browser) / ( SELECT Count(*) FROM web_visitors ) AS Percent<BR>FROM web_visitors<BR>GROUP BY browser<BR><BR>See that? The subsidiary select gets you the value you need to divide by.<BR><BR>Untried! Untested! Let me know if it works.<BR><BR>

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts