SQL - Select only records where field contains either a given string explicitly OR or a range of strings within which given string falls

58 views Asked by At

My data is set up as follows: MyDataFormat This is just a sample. There are hundreds of rows. This is for costing options (optional features) on a boat. My data above is a small sample of what we call an "optional parts kit". Column one contains the generic boat part number. Column 2 contains optional component part numbers that are tied to the generic boat by what we call "property conditions". These property conditions are in column 3. For instance, a boat can come with many different trolling motors or without a trolling motor at all. Property name for trolling motor here is TMP. This TMP property can have many values (each tied to a specific trolling motor). Currently, when I am costing a specific trolling motor option, say TMP='MK216', I have to manually select/find all components that are tied to this condition. In other words, select all rows where "Condition" column contains TMP(*E)='MK216' either explicitly or implicitly. By implicitly I mean that "Condition" column can specify a RANGE of trolling motor property values (i.e. (TMP(*E)>='MK214' AND TMP(*E)<='MK346') OR TMP(*E)='GM300' OR TMP(*E)='GM301' OR TMP(*E)='LW300')) that contains my value of interest 'MK216'. It is easy to find all rows containing a specific string. But I have no idea how to go about testing whether a component is tied to a RANGE of trolling motor values and whether 'MK216' falls within this range? What would be ideal is to write an SQL query that can pull all components tied to a given property/property value and then adds up the cost of each component to get the total cost of the optional feature.

Below is my code for selecting all rows where "condition" column contains a specific string. Selected fields are as follows: pspmrn = Parent Part #, pscmrn = Component Part #, imdsc = Description, pscond = Condition, numqty = Qty, and imlcos = Cost.

Code is written in Excel VBA editor and database is IBM i AS/400. Any help would be greatly appreciated. Thank you!

    Cmd.CommandText =
       "create table qtemp.testIB as (                                                                
        With temp as (                                                                                  
        SELECT *                                                                                        
        FROM k23400t.Fcpstus)                                                                           
        SELECT pspmrn,pscmrn,imdsc,pscond,numqty,imlcos FROM temp                                       
        WHERE pspmrn = '23NZ19' AND (pscond like '%MK216%' OR pscond      
        LIKE '%TMP(*E)>=% AND TMP(*E)<=%')
        ORDER BY pscond                                                                                 
        ) WITH DATA"  
0

There are 0 answers