I have the following table structure:
From which you want to get the percentage of the value that comes in the loan column grouped by number. of cuit.
I have tried this but in all cases it works 100%
SELECT
cdd.NumeroIdentificacion ,
cdd.Prestamos ,
CAST(REPLACE(cdd.Prestamos,',','.') AS FLOAT) * 100 /( SELECT
SUM(CAST(REPLACE(cdd.Prestamos,',','.') AS FLOAT))
FROM
CentralDeDeudores AS a
WHERE
a.NumeroIdentificacion = cdd.NumeroIdentificacion
GROUP BY
a.NumeroIdentificacion
) AS porcentaje
FROM
CentralDeDeudores cdd
GROUP BY
cdd.NumeroIdentificacion,cdd.Prestamos
The basic way
SQL
would be to obtain the totals ofPrestamos
perNumeroIdentificacion
in one query and integrate it later in another query with each one of the loans to arrive at their percentage. Something like that:Good day, taking into account your concern, you can look at the following example:
It is important to keep in mind that 100% is for each quantity, if we say there are 4 elements, the percentage will be divided into those 4 items that have the same identifier and so for each quantity that you have in your records, I will share an img. of how it would be:
I hope it is useful for you. Greetings.