If this is your first visit, be sure to check out the FAQ by clicking the link above.
You may have to register before you can post: click the register link above to proceed.
To start viewing messages, select the forum that you want to visit from the selection below.
I have been puzzled by this over the last 2 days, maybe an SQL expert here can help me out.
I have the following table:
==============
Pref
==============
UID int
SMOKE bit
LIVESMOKE bit
PET bit
LIVEPET bit
==============
I have a record with the following preferences:
smoke: 1
live with smoker: 1
pet: 0
live with pet: 0
how can I write an SQL statement to pull up all rows in the table sorted by the number of matching preferences to the row indicated above?
My initial thought was to xnor the pref which will return 1 on each matching pref but then I still need to count the number of on bit on the xnor result.
Have anyone done this before? any idea or suggestion?
order
by case when SMOKE = 1 then 1 else 0 end
+case when LIVESMOKE = 1 then 1 else 0 end
+case when PET = 0 then 1 else 0 end
+case when LIVEPET = 0 then 1 else 0 end
desc