Percentage in Procedure SQL

Asked

Viewed 31 times

1

Good morning, you guys, I need to create a rule within a precedent, where I first need to get 100% of the ids marked with AP. And then of these 100%, I need to get 15% of the status ids = 10.

But I don’t know how to "stack these ids." Would anyone have any hints?

IF EXISTS ( SELECT TOP 1 1 FROM dg.Registros  
            WHERE tipo = 'ap' and idregistro = @idregistro
           )
                            
    
                
--Regra - 15% dos registros
IF EXISTS (SELECT TOP 1 1 FROM dg.Registros
            WHERE idregistro = @idregistro
            AND idregistro IN (
            SELECT t.total * 15/100 as perc FROM dg.Registros q,
                ( 
                  SELECT count(idregistro) as total FROM dg.Registros
                  WHERE  tipo = 'ap'
                  AND idregistroStatus = 10
                  AND idregistro = @idregistro
                ) t
                  GROUP BY total
            )
            )
  • I don’t understand your explanation of what it is to do nor its logic but probably the HAVING clause will help you. Certainly this IN is being misused.

  • Procedures are written in specific languages of the respective DBMS, inform the DBMS, version etc..

  • Good morning, I have already solved. SQL version 2017. Thanks for the return.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.