Quantcast
Channel: SQLServerCentral » SQL Server 2014 » Development - SQL Server 2014 » Latest topics
Viewing all articles
Browse latest Browse all 3145

CALCULO DE PORCENTAGEM COM CONDIÇÕES

$
0
0
good afternoon ,I'm hum code that brings me the values ​​of the fields Table , however When I " * 100 / " it 's all me 100 % Being que value would be the amounts of ordserv . generated / ordserv.fechadas and then multiplied by 100 to give the percentage . Below follows the code :SELECT DATEPART(MM, ORDSERV.DATPRO2) MES, DATEPART(YY, ORDSERV.DATPRO2) MES, ENGEMAN.MESABREV(ORDSERV.DATPRO2)+'/'+SUBSTRING(CAST(DATEPART(YY, ORDSERV.DATPRO2) AS VARCHAR),3,2) MES_ANO,SUM(CASE WHEN ORDSERV.STATORD='F' THEN 1 ELSE 0 END) 'REALIZADAS'FROM ORDSERVWHERE ORDSERV.DATPRO2 BETWEEN '01/03/2016' AND '30/04/2016'AND ORDSERV.STATORD='F'AND ORDSERV.CODEMP=76AND 'S' IN (SELECT REGSERV.EXECUTADO FROM REGSERV WHERE REGSERV.CODEMP=ORDSERV.CODEMP AND REGSERV.CODORD=ORDSERV.CODORD) GROUP BY DATEPART(MM, ORDSERV.DATPRO2),ENGEMAN.MESABREV(ORDSERV.DATPRO2), DATEPART(YYYY, ORDSERV.DATPRO2)ORDER BY 1,2Result: Mouth Mouth_1 mouth_year conducted--- ----- ------- ---------- 3 2016 mar/16 489 4 2016 abr/16 659 This is the result that it gives me values ​​are correct, total performed .SELECT DATEPART(MM, ORDSERV.DATPRO2) MES, DATEPART(YY, ORDSERV.DATPRO2) MES, ENGEMAN.MESABREV(ORDSERV.DATPRO2)+'/'+SUBSTRING(CAST(DATEPART(YY, ORDSERV.DATPRO2) AS VARCHAR),3,2) MES_ANO,COUNT(*) 'GERADAS'FROM ORDSERVWHERE ORDSERV.DATPRO2 BETWEEN '01/03/2016' AND '30/04/2016'AND ORDSERV.STATORD='F'AND ORDSERV.CODEMP=76 GROUP BY DATEPART(MM, ORDSERV.DATPRO2),ENGEMAN.MESABREV(ORDSERV.DATPRO2), DATEPART(YYYY, ORDSERV.DATPRO2)ORDER BY 1,2Result:Mouth Mouth_1 mouth_year conducted--- ----- ------- ------- 3 2016 mar/16 514 4 2016 abr/16 707 This is the result that it gives me values ​​is correct , total generated.When I ask him he gives me a percentage me back everything 100 % SELECT DATEPART(MM, ORDSERV.DATPRO2) MES, DATEPART(YY, ORDSERV.DATPRO2) MES, ENGEMAN.MESABREV(ORDSERV.DATPRO2)+'/'+SUBSTRING(CAST(DATEPART(YY, ORDSERV.DATPRO2) AS VARCHAR),3,2) MES_ANO,SUM(CASE WHEN ORDSERV.STATORD='F' THEN 1 ELSE 0 END)*100/COUNT(*) AS PORCENTAGEMFROM ORDSERVWHERE ORDSERV.DATPRO2 BETWEEN '01/03/2016' AND '30/04/2016'AND ORDSERV.STATORD='F'AND ORDSERV.CODEMP=76AND 'S' IN (SELECT REGSERV.EXECUTADO FROM REGSERV WHERE REGSERV.CODEMP=ORDSERV.CODEMP AND REGSERV.CODORD=ORDSERV.CODORD) GROUP BY DATEPART(MM, ORDSERV.DATPRO2),ENGEMAN.MESABREV(ORDSERV.DATPRO2), DATEPART(YYYY, ORDSERV.DATPRO2)ORDER BY 1,2Result:Mouth Mouth_1 mouth_year conducted--- ----- ------- ----------- 3 2016 mar/16 100 4 2016 abr/16 100 I think the error is in the condition "AND 'S' IN (SELECT REGSERV.EXECUTADO FROM REGSERV WHERE REGSERV.CODEMP=ORDSERV.CODEMP AND REGSERV.CODORD=ORDSERV.CODORD) " ,but it is with him that shot the table results REGSERV and bring to the ORDSERV giving me all those undertaken .I ask your help because I'm already one week trying to solve it and nothing . Any help is welcome . I thank you all.

Viewing all articles
Browse latest Browse all 3145

Trending Articles