首页 > 代码库 > sql语句求百分比
sql语句求百分比
此sql语句包括了两个聚合函数做除法求百分比,并保留两位小数,直接输出字符串形式的百分比。以及对case when在聚合函数的应用。
SELECT ss.SS_NAME,SS_ID,COUNT(ea.EA_ID) AS EACounts,--回单交换单个数COUNT(eb.EB_ID) AS EBCounts,--交换单个数COUNT(ps.PS_ID) AS PSCounts,--签收单个数COUNT(SW_ID) AS SWCounts,--运单数个数COUNT(CASE WHEN PS_STATUS=1 THEN ‘PS_ID‘ END) AS PSSCounts,--签收成功单个数CAST(CONVERT( DECIMAL(18,2),COUNT(ea.EA_ID)/CAST(ISNULL(NULLIF(COUNT(EB_ID),0),1) AS FLOAT)*100)AS VARCHAR(10))+‘%‘ AS ReturnPer,--回单率CAST(CONVERT(DECIMAL(18,2),COUNT(ps.PS_ID)/CAST(ISNULL(NULLIF(COUNT(SW_ID),0),1) AS FLOAT)*100)AS VARCHAR(10))+‘%‘ AS SignPer ,--签收率CAST(CONVERT(DECIMAL(18,2),COUNT(CASE WHEN PS_STATUS=1 THEN ‘PS_ID‘ END)/CAST(ISNULL(NULLIF(COUNT(PS_ID),0),1) AS FLOAT)*100 ) AS VARCHAR(10))+‘%‘ AS SignSucPer --签收成功率FROM dbo.SBD_WAYBILL swLEFT JOIN dbo.PDA_SININFO ps ON sw.SW_ID=ps.PS_SW_IDLEFT JOIN dbo.SBD_EXCHANGE_RETURN_BILL ea ON ea.EA_SW_ID=sw.SW_IDLEFT JOIN dbo.SBD_EXCHANGE_BILL eb ON eb.EB_SW_ID=sw.SW_IDLEFT JOIN dbo.SBD_STATION ss ON ss.SS_ID=sw.SW_DESTINATIONWHERE 1=1 GROUP BY SS_ID,SS_NAMEORDER BY SS_NAME
sql语句求百分比
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。