SQL statement percent

Source: Internet
Author: User

This SQL statement consists of two aggregate functions that do a percentage of division, and retain two decimal places, as a percentage of the direct output string. And the application of the case to the aggregation function.

SELECTSS. SS_NAME,SS_ID,COUNT(EA.) EA_ID) asEacounts,--single-digit exchangeCOUNT(EB. EB_ID) asEbcounts,--Exchange Single numberCOUNT(ps.ps_id) asPscounts,--sign a single numberCOUNT(sw_id) asSwcounts,--Number of WaybillCOUNT( Case  whenPs_status=1  Then 'ps_id' END) asPsscounts,--single number of successful sign -offCAST(CONVERT(DECIMAL( -,2),COUNT(EA.) EA_ID)/CAST(ISNULL(Nullif(COUNT(eb_id),0),1) as FLOAT)* -) as VARCHAR(Ten))+'%'  asReturnper,--The rate of the slipsCAST(CONVERT(DECIMAL( -,2),COUNT(ps.ps_id)/CAST(ISNULL(Nullif(COUNT(sw_id),0),1) as FLOAT)* -) as VARCHAR(Ten))+'%'  asSignper,--sign -off rateCAST(CONVERT(DECIMAL( -,2),COUNT( Case  whenPs_status=1  Then 'ps_id' END)/CAST(ISNULL(Nullif(COUNT(ps_id),0),1) as FLOAT)* -) as VARCHAR(Ten))+'%'  asSignsucper--sign -off success fromdbo. Sbd_waybill SW Left JOINDbo. Pda_sininfo PS onsw.sw_id=ps.ps_sw_id Left JOINDbo. Sbd_exchange_return_bill ea onEa. ea_sw_id=sw.sw_id Left JOINDbo. Sbd_exchange_bill EB onEb. eb_sw_id=sw.sw_id Left JOINDbo. Sbd_station SS onSs. ss_id=sw.sw_destinationWHERE 1=1 GROUP  bySs_id,ss_nameORDER  bySs_name

SQL statement percent

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.