Percentage calculation, and then sort related issues

Source: Internet
Author: User
Percentage calculation, and then arrange the problem. Table tab field a1a2 & nbsp; PHPcodea1a2 Wang 0 Wang 1 Wang 3 Li 1 li 2 calculation a2 Li Wang & gt; the value of 0 has two values of the King. a total of three values of the 23 percentage calculation, and then the problem of sorting.
Table tab field a1 a2
PHP code
  A1 a2 Wang 0 Wang 1 Wang 3 Li 1 li 2


In a2, the value of king> 0 has two values of King. a total of three values are 2/3, and the output is 66%.
Li is 100% and then arranged in ascending order by percentage


Output result display
PHP code
  Li 100% Wang 66%


------ Solution --------------------
Select a1, sum (if (a2> 0, 0)/count (*) * 100 as num from tab group by a1 order by num desc

Formula:
Sum (if (a2>, 0) counts the number of a2 values greater than 0.
Count (*) count the total number

Number of numbers greater than 0/total number * 100
Yes
Percentage of the population greater than 0, which is a basic arithmetic operation.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.