Select Duplicates with distinct/DIFFERENT names

70 Views Asked by At

I have Articles base:

INDEKS_KATALOGOWy ID_MAGAZYNU Nazwa
Opa000001 1 Węgiel
Opa000001 6 Węgiel
Opa000001 7 Węgiel
Opa000002 1 Węgiel brunatny
Opa000002 6 Węgiel brunatny
Opa000002 7 Węgiel brunatny
PRO000001 1 Pasta 10 ml
PRO000001 6 Pasta 10 ml
PRO000001 7 Pasta 10 ml
PRO000001 8 PASTA 10 ml
ROL000001 1 Artykuły rolnicze
ROL000001 6 Artykuły rolnicze
ROL000001 7 Artykuły rolnicze

This is selection by query:

select indeks_katalogowy, ID_MAGAZYNU, Nazwa
from artykul

where indeks_katalogowy in (Select INDEKS_KATALOGOWY 
    from ARTYKUL
    Group by INDEKS_KATALOGOWY, NAZWA
    having count(*) >1)

And I have to display ONLY articles with DIFFERENT Names so I want to only show:

INDEKS_KATALOGOWy ID_MAGAZYNU Nazwa
PRO000001 1 Pasta 10 ml
PRO000001 6 Pasta 10 ml
PRO000001 7 Pasta 10 ml
PRO000001 8 PASTA 10 ml

because Pasta 10ml and PASTA 10 ml are different

2

There are 2 best solutions below

4
ORA-01017 On BEST ANSWER

You can use exists as follows:

select indeks_katalogowy , ID_MAGAZYNU, Nazwa
from artykul t
 Where exists
  (Select 1 from artykul tt
    Where t.indeks_katalogowy = tt.indeks_katalogowy
      And t.Nazwa != tt.Nazwa )
0
JMabee On

If Pasta 10ML and PASTA 10ML are different then you would need to change to Collation on the column to reflect this. You also would only need to group by INDEKS_KATALOGOWY

select indeks_katalogowy, ID_MAGAZYNU, Nazwa
from artykul

where indeks_katalogowy in (Select INDEKS_KATALOGOWY 
    from ARTYKUL
    Group by INDEKS_KATALOGOWY
    having count(DISTINCT Nazwa COLLATE SQL_Latin1_General_CP1_CS_AS) >1)