Hi I am working on a query that is using the below schema to find out how often users are doing searches by company (the app basically allowed you to do a search by company)
SEARCH_LOG
----------
date_of_search (DATETIME)
company_id (INT)
COMPANIES
---------
id (INT)
company_name (VARCHAR)
(there are more columns but these are the relevant ones)
So I am running the following query:
SELECT company_name,COUNT(*) FROM companies LEFT OUTER JOIN search_log ON search_log.company_id=companies.id GROUP BY companies.id
This is great as it returns each company and the number of search performed, however I want to express these numbers as a percentage. My knee jerk reaction was to just run the following query seperately:
SELECT COUNT(*) FROM search_log
Grab that result and just do the division on the application side, however this seems really inefficient and I'd like to do it all in one query if possible (preferably without the use of a subquery) but have no idea how to get that information.
Any help or guidance would be appreciated.
EDIT: Maybe I wasn't totally clear about what I'm after. Instead of getting results like:
COMPANY_NAME | COUNT(*)
-----------------------
CompanyA | 1
CompanyB | 3
I'd rather see:
COMPANY_NAME | COUNT(*)
-----------------------
CompanyA | 25%
CompanyB | 75%
Obviously the formatting isn't super important as 25,25%,0.25 are all usable.