Search code examples
sqloledbvisual-foxpro

How can I disable default ordering temporarily in Visual FoxPro?


I am doing a search where I want to keep the natural order in order to maintain ranking. Here's my code query:

SELECT buyer_id, first, last, ALLTRIM(address) AS address1, city, state FROM BUYERMAST.DBF WHERE first = '{0}' AND last = '{1}' UNION 
SELECT buyer_id, first, last, ALLTRIM(address) AS address1, city, state FROM BUYERMAST.DBF WHERE phone = '{6}' UNION 
SELECT buyer_id, first, last, ALLTRIM(address) AS address1, city, state FROM BUYERMAST.DBF WHERE first LIKE '{0}' AND last LIKE '{1}' UNION 
SELECT buyer_id, first, last, ALLTRIM(address) AS address1, city, state FROM BUYERMAST.DBF WHERE address LIKE '{2}' AND city LIKE '{3}' UNION 
SELECT buyer_id, first, last, ALLTRIM(address) AS address1, city, state FROM BUYERMAST.DBF WHERE first = '{0}' OR last = '{1}' OR address = '{2}' OR city = '{3}' OR state = '{4}' OR zip = '{5}' OR phone = '{6}' or email = '{7}'"

The first select in the union returns me an exact match whereas the last select returns a very general match. The problem is that the results are always returned sorted by buyer_id. I cannot alter the existing tables/indexes, and I see that there are several indexes with the buyer_id being the first one. I would like to "disable" the index order if possible so that the results from the first select are returned first and the subsequent selects are actually appended to the previous. Is this possible?

P.S. The unions, by default, are distinct, and if I add a "rank" column to each select and order by that, it will leave duplicate results because the rank column is distinct between each select query.


Solution

  • How about a SINGLE query that has all the qualifiers in ONE query, but additional columns as flags WHERE they qualified... Since your last union has "OR" values across the board, just use that as the WHERE, but the specific criteria as FLAGs on HOW it was qualified, then you can keep a single entry per person but have it flagged as "howeverMatched" to analyze through.

    SELECT 
          buyer_id, 
          first, 
          last, 
          address AS address1, 
          city, 
          state,
          ( first = '{0}' AND last = '{1}' ) as ExactNameMatch,
          ( phone = '{6}' ) as PhoneMatch,
          ( LIKE( first, '{0}' ) AND LIKE( last, '{1}' )) as LikeNameMatch,
          ( LIKE( address, '{2}' ) AND LIKE( city, '{3}' )) as LikeAddressMatch
       FROM 
          BuyerMast
       WHERE 
             first = '{0}' 
          OR last = '{1}' 
          OR address = '{2}' 
          OR city = '{3}' 
          OR state = '{4}' 
          OR zip = '{5}' 
          OR phone = '{6}' 
          OR email = '{7}'
          OR ( first LIKE '{0}' AND last LIKE '{1}' )
          OR ( address LIKE '{2}' AND city LIKE '{3}' )
    

    Its not liking the "LIKE" in the name match qualifiers... so the LikeNameMatch and LikeAddressMatch are the culprits... HOWEVER, VFP does have a FUNCTION, so I've changed them

    from   first LIKE '{0}'
    to     LIKE( first, '{0}' )
    

    similar with the rest.