1. We have moved to a new forum! There may be a few things not working properly so please let us know if you find a bug. Remember to use the bbCode [ sql ] tag for SQL statements.

"Condition table" query

Discussion in 'SQL - Questions and Answers' started by sriramna, May 15, 2008.

  1. sriramna New Member

    I have a reference table 'LOOKUP' with 4 fields and sample values as follows:

    HTML:
    Plant  Group  WorkCentre  WageType   CalcType
    1234   1002   W001           9Y10           A
    1234   *      W002           9Y11           C
    1234   1005   W001           *              D
    1234   1002   W003           *              C
    1234   1007   *              *              C
    1234   1002   *              9Y32           C
    etc.

    There is another table with transaction values:
    HTML:
    Plant  Group  WorkCentre  WageType   Date
    1234   1002    W003      9Y13        15/05/08
    1234   1005    W001      9Y13        15/05/08
    
    etc.

    I need a query which will return all records in the transaction table which find a match for a given CalcType in the Lookup table, with * considered as a wildcard in the lookup table (i.e., any value is OK in the transaction table as long as the other specific values match in the lookup table).

    The problem is that more than one field value could be a '*' in the lookup table, as shown above.

    Is it at all possible via SQL to retrieve the transactional records which find any match for a given CalcType in the lookup table? Or am I constrained to use procedural logic, examining each transaction against every record in the lookup table until I find a match or run to the end?

    Would appreciate any ideas...
  2. Unregistered Guest

    You might consider breaking up this and using the UNION to compile your results.
  3. Unregistered Guest

    With four fields, each of which could have a "wildcard" specification, this would result in over 16 union queries...
  4. Unregistered Guest

    Perhaps create a (normalized) view that eliminates the wildcards and explicitly names every combo in the "Condition table" -> then join to the view. It sounds like the wildcards are there to make data entry easier, but they're making the table difficult to use.
  5. sriramna New Member

    Shouldn't something along the following lines work?
    SQL:

    select t.*
    from trans_table t join lookup_table l
    on l.plant in ('*', t.plant)
    and l.group in ('*', t.group)
    and l.workcentre in ('*', t.workcentre)
    and l.wagetype in ('*', t.wagetype)
    where t.calctype = [SomeCalcType_Value]
  6. Unregistered Guest

    SQL:
    select t.* , l.calctype 
    from trans_table t join lookup_table l
    on l.plant in ('*', t.plant)
    and l.group in ('*', t.group)
    and l.workcentre in ('*', t.workcentre)
    and l.wagetype in ('*', t.wagetype)

Share This Page