Question

I wasn't too sure how to phrase this question, so here are the details. I'm using a trick to compute the hamming distance between two bitstrings. Here's the query:

select length(replace(x::text,'0',''))
from (
    select code # '000111101101001010' as x
    from codeTable
) as foo

Essentially it computes the xor between the two strings, removes all 0's, then returns the length. This is functionally equivalent to the hamming distance between two bitstrings. Unfortunately, this only returns the hamming distance, and nothing else. In the codeTable table, there is also a column called person_id. I want to be able to return the minimum hamming distance and the id associated with that. Returning the minimum hamming distance is simple enough, just add a min() around the 'length' part.

select min(length(replace(x::text,'0','')))
from (
    select code # '000111101101001010' as x
    from codeTable
) as foo

This is fine, however, it only returns the hamming distance, not the person_id. I have no idea what I would need to do to return the person_id associated with that hamming distance.

Does anybody have any idea on how to do this?

Was it helpful?

Solution

Am I missing something? Why the subquery? Looks to me like the following should work to:

select length(replace((code # '000111101101001010')::text,'0',''))
from codeTable

Going from there I get:

select person_id,length(replace((code # '000111101101001010')::text,'0','')) as x
from codeTable
order by x
limit 1

I replaced the min with an order by and limit 1 because there is no direct way of getting the corresponding person_id for the value returned by the min function. In general postgres will be smart enough not to sort the whole intermediate result but just scan it for the row with the lowest value it has to return.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top