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