views:

2460

answers:

4

I'm trying to make the following code smaller. Is this possible?

select a.*
from table1 a
WHERE a."cola1" = 'valuea1'
UNION ALL
select a.*
from tablea1 a
inner join tablea2 b on a."cola2" = b."colb2"
WHERE a."cola1" = 'valuea2'
  and b."colb3" = 'valueb3'

In effect I'm looking for records from table1 for value1 or value2, but for records matching value2 I want to apply 1 extra condition which involves a join to a 2nd table Can this be done without a UNION clause?

A skeleton or what I'm trying to code is below....but it's not working naturally.

select a.*
from table1 a
inner join table2 b on a."cola1" = b."COLb1"
WHERE a."cola2" IN ('valuea1','valuea2')
  and 
  CASE 
    WHEN a."cola2" = 'valuea2' THEN b."colb1" = 'valueb3'
    ELSE 1=1
  END CASE
+4  A: 

I think CASE statements work in join conditions, but I'm not sure. But would this work for you?

select *
  from table1 a
 where a.cola1 = 'valuea1'
    or (a.cola1 = 'valuea2'
        and Exists(select 1
                     from table2 b
                    where a.cola2 = b.colb2
                      and b.colb3 = 'valueb3'
                   )
        )


Edit: Wouldn't this simply work?

select a.*
  from table1 a
  Left Outer Join table2 b On (a.cola2 = b.colb2)
 where a.cola1 = 'valuea1'
    or (a.cola1 = 'valuea2' and b.colb3 = 'valueb3')
Hosam Aly
A: 

OP: I've got a mini-workaround which goes close (This may only work given this is an inner join.)

select a.* from table1 a inner join table2 b on a."cola1" = b."COLb1" WHERE (a."cola2" = 'valuea1') OR (a."cola2" = 'valuea2' and b."colb1" = 'valueb3')

Sometimes writing code out can prompt some alternative thinking. Self-Therapy sort of. Thanks for your input.

This doesn't achieve the same objective. This query won't produce values where a.cola2 = 'valuea1' unless the inner join succeeds. Check my solution for some alternatives.
Hosam Aly
A: 

OP: Again

Hosam: Part 2 is Same angle I though of myself....thanks. Part 1 may solve the optional aka left join problems.

I hadn't thought of the subquery angle.

No, part 2 is inherently different from yours. Notice the LEFT OUTER JOIN part. My query would produce results when the join fails, but yours will not produce them in such a case.
Hosam Aly
And by the way, you could use comments instead of writing answers. :)
Hosam Aly
+3  A: 

Overall you should follow Hosam's suggestion of rewriting the predicate entirely. But to explain your original problem further, the issue is that in SQL, CASE .. END is an expression and can only be used where any other expression could be used. A condition like "a=b" is two expressions connected by a logical operator. You may want to think of it as a boolean expression but that's not the way SQL views it.

You could accomplish what you want with CASE by using it as one of the two expressions in the condition, like so:

WHERE a."cola2" IN ('valuea1','valuea2')
  and 
  b."colb1" = CASE 
                WHEN a."cola2" = 'valuea2' THEN 'valueb3'
                ELSE b."colb1"
              END CASE

(If it is possible for colb1 to include NULLs you would need to modify to handle that.)

Dave Costa