Group or Count SQL Server repeated values

0

This query brings me the information correctly with the difference that it brings me the duplicates, so what I want to do is another column where you tell me how many times the same number is repeated in this case the repeated ones are here (E.N_PARTE) example of what it returns

E.N_PARTE
AAA
AAA
BBB
CCC
BBB

What I want is to add another column where

Repetidos E.N_PARTE
   2      AAA
   2      BBB
   1      CCC


SELECT E.NOM_ALMACENISTA, E.N_PARTE, E.DESCRIPCION, E.INV_MINIMO, 
E.INV_MAXIMO, E.INV_EXISTE, E.COS_UNIDLLS,HT.FCH_REGISTRO
FROM ELENTRADAS AS E LEFT OUTER JOIN 
HISTORIAL AS HT ON HT.NUM_PART = E.N_PARTE 
WHERE (HT.FCH_REGISTRO BETWEEN '2018-08-24' AND '2018-08-24') AND (E.DEPART 
= 'Departamento1' )
order by HT.FCH_REGISTRO ASC

Thanks for your help.

    
asked by Manny 29.08.2018 в 21:34
source

2 answers

0

What you have to do is group by the field you want and then count that field and it will give you the number of repeated records of that Field

SELECT E.N_PARTE, COUNT(E.N_PARTE) AS REPETIDOS
FROM ELENTRADAS AS E LEFT OUTER JOIN 
HISTORIAL AS HT ON HT.NUM_PART = E.N_PARTE 
WHERE (HT.FCH_REGISTRO BETWEEN '2018-08-24' AND '2018-08-24') AND (E.DEPART = 'Departamento1' )
GROUP BY E.N_PARTE
order by HT.FCH_REGISTRO ASC
    
answered by 29.08.2018 / 21:53
source
2

You have to add a group by at the end as in the following example

SELECT COUNT(E.*) as Repetidos, E.N_PARTE
FROM ELENTRADAS AS E 
GROUP BY E.N_PARTE

You also have to take into account that the arguments that you put in the select do not generate conflict with the group by.

    
answered by 29.08.2018 в 21:48