1
I have a little doubt that I am not able to solve, I looked here in the community, but I did not find exactly what I want.
My situation resembles the following: I have N product records in my database. And in these N records, I only have 3 different codes (not the primary key) for each product, which are: 4120, 5200 and 6150. Each product has a color, regardless of the code. What I need is to get the percentage of existing products with each code. Also, see if the target (a type of alert) of each code has been reached. Example(it doesn’t have to be in html, I want it in sql server):
<table>
<thead>
<tr>
<td>Código</td>
<td>Porcentagem</td>
<td>Meta</td>
</tr>
</thead>
<tbody>
<tr>
<td>4120</td>
<td>35%</td>
<td>Atingida</td>
</tr>
<tr>
<td>5200</td>
<td>45%</td>
<td>Atingida</td>
</tr>
<tr>
<td>6120</td>
<td>20%</td>
<td>Não atingida</td>
</tr>
</tbody>
</table>
I thought to do this problem as follows: take the amount of records in a select using Count.
SELECT COUNT(ID_PRODUTO) FROM PRODUTOS
In this part I started to have doubts because I would have to take the selected code and make calculations on this total of records found. And then check using case when, if the percentage of each code had reached 25%. If so, the target was reached. If not, the finish line has not been reached.
I could not solve, wanted to understand how I can do this, if possible using only a select.
// The necessary fields Table: PRODUCTS Fields: INT ID_PRODUTO, INT CODE