1

I have a table like

id type note
1   A   ab cd
1   B   cdef
1   A   abd
1   A   abcd

I want to get all the unique notes after removing space so my result will be like this

id type note
1   A   ab cd
1   A   abd
1   B   cdef

because after removing space ab cd and abcd will be same. I am using Postgres. Please note that id column in not unique.

Jakub Kania
  • 15,665
  • 2
  • 37
  • 47
Sabyasachi Ghosh
  • 2,765
  • 22
  • 33

2 Answers2

2

It's a classic groupwise maximum but you just group it on an expression not column

SELECT id, type, note FROM
  (SELECT DISTINCT ON(replace(note,' ',''))
     id
     ,type
     ,note
   FROM table1
   ORDER BY replace(note,' ','')
  ) AS q

sqlfiddle

Jakub Kania
  • 15,665
  • 2
  • 37
  • 47
0

Since you appear to be using Rails (according to your tags), you could also use ActiveRecord to get all of your unique instances of note, disregarding any spaces:

WhateverModel.pluck("REPLACE(note, ' ', '')").uniq
jeffdill2
  • 3,968
  • 2
  • 30
  • 47