solime.blogg.se

Mysql count number with percentage
Mysql count number with percentage









mysql count number with percentage mysql count number with percentage

On p.id = pe.participant_id) as percentage , 100*count(p.id)/(SELECT count(p.id) FROM participants as p JOIN participants_events as pe So i.signori answer will be : SELECT count(p.id) as count TO SELECT count(p.id) as count, round(100*count(p.id)/(SELECT count(p.id) FROM participants as p JOIN participants_events as pe on p.id = pe.participant_id),1) as percentage, p.Nationality So modify your initial query from : SELECT count(p.id) as count, round(100*count(p.id)/(SELECT count(p.id) FROM participants as p),1) as percentage, p.Nationality I have a table named PERSON (In the live system I expect several hundred thousand records or more). productid INT productname VARCHAR price DECIMAL categoryid INT (FOREIGN KEY id from CategoryDetails table) 2. I am using MySQL and I need help using COUNT() for a range of values within a table. Stores details of various products in a store. Perhaps the next option will be more to your liking. Unfortunately, that's not easily done using the over () clause. Please check in your initial query to use the same sets: We would be using the following tables and data for the examples for the MySQL COUNT function. Here's the formula to obtain a percentage: count () 100.0 / sum (count ()) over () Adding the above SQL to our original query produces the following results: Looks good, but some rounding wouldn't hurt. HAVING round(100*count(p.id)/(SELECT count(p.id) FROM participants as p),1) <= 1 HAVING round(100*count(p.id)/(SELECT count(p.id) FROM participants as p),1) > 1įor the second part, try to combine the results of the first with those of the sum of percentages less than 1. , round(100*count(p.id)/(SELECT count(p.id) FROM participants as p),1) as percentage Try using Having clause: SELECT count(p.id) as count Example Code: SELECT RepresentativeName, Sale, round( ( (Sale 100) / temp. So with the new query the results will be: +-+-+-+ To find the percentage of the Sale field, we can CROSS JOIN the SUM () function of the Sale attribute with the original relation (table). I want to modify the query in order to don't show any rows with percentage less than 1% but summarize their number and add is as 'Other' with the new percentage. JOIN participants_events as pe on p.id = pe.participant_id So again, the above statement will count only the rows where neither col1 nor col2 is null.This is the query I am using to get the percentage per country for participants in events: SELECT count(p.id) as count, round(100*count(p.id)/(SELECT count(p.id) FROM participants as p),1) as percentage, p.Nationality Adding up ( SUM) the 1s is equivalent to counting the truths. With only 1s and 0s the * operator works as an equivalent of the logical AND: the final result will be 1 (True) only if each operand is 1 otherwise the result will be 0 (False). In the context of the arithmetic operator * the logical result of the IS NOT NULL operator is implicitly converted to a number, 1 for True, 0 for False. Please show an example of the desired output if you want us to give a meaningful answer. SUM( (col1 IS NOT NULL) * (col2 IS NOT NULL) ) 1 The two queries aren't compatible, as they're currently written: One returns a single record with a single column, the other will return many columns in X rows. In particular, you could replace the COUNT with SUM and treat the predicates as numbers (1/0) in an arithmetic expression: SELECT It is possible to shorten the expression somewhat by choosing a less clear syntax. The obvious flaw of this workaround (either variation) is that it is clearly rather unwieldy and will become ridiculously long very quickly as you add more columns to account for. A row will be counted only if neither col1 nor col2 is null. Where instead of the 1 you can put any non-null constant. Or the MySQL-specific IF function: SELECTĬOUNT(IF(col1 IS NOT NULL AND col2 IS NOT NULL, 1, NULL)) It could be either the standard-compliant CASE: SELECTĬOUNT(CASE WHEN col1 IS NOT NULL AND col2 IS NOT NULL THEN 1 END) , successful, 10, 50, failed, 5, 25, refunded, 5, 25, successful, 14, 58.3, failed, 4, 16. If that last aspect of the behaviour is what you are trying to achieve, you could emulate it using a conditional inside COUNT. However, one other important point is that a tuple is counted only if none of the individual values in the tuple is null. Obviously, COUNT(DISTINCT) with multiple columns counts unique combinations of the specified columns' values. SELECT Category, COUNT () AS Total, (COUNT () / (SELECT COUNT () FROM Item WHERE Department'Popular')) 100 AS 'Percentage to all items', FROM Item WHERE Department'Popular' GROUP BY Category I'm not sure of the MySql syntax, but you can use a sub-query as shown.











Mysql count number with percentage