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.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > ANSI SQL > Help needed to generate a "sort code"

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 10-25-04, 14:07
kenicheema kenicheema is offline
Registered User
 
Join Date: Dec 2003
Posts: 3
Help needed to generate a "sort code"

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?
Reply With Quote
  #2 (permalink)  
Old 10-25-04, 18:07
r937 r937 is online now
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
Code:
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
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #3 (permalink)  
Old 10-26-04, 05:35
andrewst andrewst is offline
Moderator.
 
Join Date: Sep 2002
Location: UK
Posts: 5,171
I'm not used to the BIT datatype. So you can't do this then?

order by SMOKE+LIVESMOKE+PET+LIVEPET

i.e. BITs are not implicitly converted to INTEGERS by an expression like that?
__________________
Tony Andrews
http://tinyurl.com/tonyandrews
Reply With Quote
  #4 (permalink)  
Old 10-26-04, 07:36
r937 r937 is online now
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
not implicitly, no, you actually have to use CAST on them

however, that won't help here, as the problem was to find rows with most matches to the particular combination 1, 1, 0, 0
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #5 (permalink)  
Old 10-26-04, 07:39
andrewst andrewst is offline
Moderator.
 
Join Date: Sep 2002
Location: UK
Posts: 5,171
I should have read more carefully!
__________________
Tony Andrews
http://tinyurl.com/tonyandrews
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On