Skip to content
Advertisement

how can I select a rows that repets two atributtes

I have a table with Destino and Tronco, Fecha and I need to group the Destino and Tronco that repeats

SELECT Destino
    ,Tronco
    ,Count(*) AS Countrows
FROM Hist_LDS
WHERE Fecha = '2020-10-28'
GROUP BY Destino
    ,Tronco
HAVING COUNT(*) > 1

enter image description here

The result is empty but if I Select without the counting this goes out

SELECT Destino
    ,Tronco
FROM Hist_LDS
WHERE Fecha = '2020-10-28'

enter image description here

Advertisement

Answer

Do you mean that EITHER the Destino or Tronco repeat? What you’re currently doing is looking for a repeating of the combination of the two of them – which doesn’t exist in your sample data list.

If so, see if the following returns what you are after.

If not, please provide sample data including records you think should appear in you initial query.

--Identify duplicated Tronco values
select Tronco, Count(*) As Countrows
INTO #TRONCO
From Hist_LDS
Where Fecha='2020-10-28' 
GROUP BY Tronco
HAVING COUNT (*)>1

--Identify duplicated Destino values
select Destino, Count(*) As Countrows
INTO #DESTINO
From Hist_LDS
Where Fecha='2020-10-28' 
GROUP BY DESTINO
HAVING COUNT (*)>1


--Identify records from initial table where Destino or Tronco were repeated
select Destino, Tronco, Count(*) As Countrows
From Hist_LDS
Where Fecha='2020-10-28' 
AND (Destino in (SELECT destino from #DESTINO) OR Tronco in (SELECT tronco from #TRONCO))
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement