SQL Recipes
A FREE cookbook for SQL queries and examples
Register FAQ Search Today's Posts Mark Forums Read

SQL - Questions and Answers Have a SQL question? Post it here. First do a search to see if someone hasn't already answered it.

Go Back   SQL Recipes a FREE cookbook of SQL queries and examples > SQL queries and examples > SQL - Questions and Answers

Reply
 
LinkBack Thread Tools Search this Thread
  #1 (permalink)  
Old 05-15-2008, 04:42 PM
sriramna sriramna is offline
Junior Member
 
Join Date: May 2008
Posts: 2
sriramna is on a distinguished road
Default ANY dialect question:

"Condition table" query


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

HTML Code:
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 Code:
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...
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

ANSWER(S):

  #2 (permalink)  
Old 05-16-2008, 06:32 PM
Unregistered
 
Posts: n/a
Default ANY answer. Re: "Condition table" query

You might consider breaking up this and using the UNION to compile your results.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 05-17-2008, 06:06 AM
Unregistered
 
Posts: n/a
Default ANY answer. Re: "Condition table" query

With four fields, each of which could have a "wildcard" specification, this would result in over 16 union queries...
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 05-20-2008, 05:02 PM
Unregistered
 
Posts: n/a
Default ANY answer. Re: "Condition table" query

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.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 05-25-2008, 08:00 AM
sriramna sriramna is offline
Junior Member
 
Join Date: May 2008
Posts: 2
sriramna is on a distinguished road
Default ANY answer. Re: "Condition table" query

Shouldn't something along the following lines work?
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]
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #6 (permalink)  
Old 05-28-2008, 01:02 PM
Unregistered
 
Posts: n/a
Default ANY answer. Re: "Condition table" query

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)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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

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



All times are GMT. The time now is 07:49 AM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0
Copyright (c) 2006-2008 SQL Recipes

1 2 3 4 5 6 7 8