Use the case statement to exclude the SQL statement with a denominator of 0.

Source: Internet
Author: User
Tags case statement

An SQL statement is used in the project, such:

Select sum (price * xiaoliang)/sum (xiaoliang) price from ......

When sum (xiaoliang) = 0, the error of division by 0 will occur. What should I do? At first, I wantProgramProcessing, that is, moving the computing process to the program is obviously troublesome. Later, I found the method of using the case statement as follows:

Select (Case sum (xiaoliang) When 0 then 0 else sum (price * xiaoliang)/sum (xiaoliang) End) price from ......

This parameter is used to determine the sum (xiaoliang) value. If it is 0, the price value is set to 0. Otherwise, the value is calculated based on the formula.

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.