Wednesday, March 21, 2012

Interesting Query


I have four lookup tables that have identical structure. I have to
write a query to check if a particlaur string (code) exists in any of
the four lookups. What is the best way of dealing with this please?
1. Write one query with four corelated subqueries (one for each
lookup).
2. Write 4 separate queries and execute them one after the other.
If there is better way to store the lookups to make writing queries
like the one I have mentioned, easy, then I can change the lookup
tables.
ThanksIf all you have to test is existence, rather than return any value,
you could try something like:
SELECT CASE WHEN EXISTS(<query table 1> ) THEN 1
WHEN EXISTS(<query table 2> ) THEN 2
WHEN EXISTS(<query table 3> ) THEN 3
WHEN EXISTS(<query table 4> ) THEN 4
ELSE 0
END as Matched
Roy Harvey
Beacon Falls, CT
On 21 Apr 2006 03:43:14 -0700, "S Chapman" <s_chapman47@.hotmail.co.uk>
wrote:

>
>I have four lookup tables that have identical structure. I have to
>write a query to check if a particlaur string (code) exists in any of
>the four lookups. What is the best way of dealing with this please?
>1. Write one query with four corelated subqueries (one for each
>lookup).
>2. Write 4 separate queries and execute them one after the other.
>If there is better way to store the lookups to make writing queries
>like the one I have mentioned, easy, then I can change the lookup
>tables.
>Thanks|||Hi,
I'd rather create four left joins. Usually you need to return a value.
Tomasz B.
"Roy Harvey" wrote:

> If all you have to test is existence, rather than return any value,
> you could try something like:
> SELECT CASE WHEN EXISTS(<query table 1> ) THEN 1
> WHEN EXISTS(<query table 2> ) THEN 2
> WHEN EXISTS(<query table 3> ) THEN 3
> WHEN EXISTS(<query table 4> ) THEN 4
> ELSE 0
> END as Matched
> Roy Harvey
> Beacon Falls, CT
>
> On 21 Apr 2006 03:43:14 -0700, "S Chapman" <s_chapman47@.hotmail.co.uk>
> wrote:
>
>|||>> I have four lookup tables that have identical structure. I have to write
a query to check if a particular string (code) exists in any of the four loo
kups. What is the best way of dealing with this please? <<
The best way is not to split the encoding over four tables. Do you
also keep a personnel table for each employee's weight class?
This is a common design flaw called attribute splitting. You can build
a UNION-ed view and use it.
That view will also help show you that you have the same code with
different definitions in your data model (do you know the Patent Office
story?). Think you don't have this problem? Just wait. Or get the
extra overhead of prevetning it with triggers or other procedural,
proprietary code.
You will also have redundant data (look up a series of articles by Tom
Johnston on non-normal form redundancies).

No comments:

Post a Comment