1

I have two tables (A and B) where my query compares a calculation from table A with a range in table B and then insert a corresponding value to the range(also in table B) in the third table(table C) based on dates. However,it is a possibility that table A may not have data for everyday and for those days i want to enter the value against the second lowest range.

TABLE B
id(PK)|date| v1 | v2


TABLE A
Aid|id(FK)|MinRange|MaxRange|Value


TABLE C
Cid|b.date|id(FK)|b.v1|b.v2|A.value

I am looking for a way to embed IF EXISTS in the WHERE clause something like this:

SELECT B.value 
from TableB B 
INNER JOIN TableA A ON A.id=B.id 
WHERE B.id=4 
and (IF DATA EXISTS) B.v1+B.v2 between A.min and A.max (ELSE Choose the second lowest A.min)`

The query above is an example to explain what i am trying to do, hence, it is not a valid query. I do not want to use a subquery for obvious performance issues.

I will appreciate any help.Thanks in advance :)

4
  • 1
    Can you post the structure of the tables? It will help people to answer better to your question. Commented Sep 18, 2013 at 6:47
  • table structure added Commented Sep 18, 2013 at 7:01
  • can you use where clause for this Commented Sep 18, 2013 at 7:03
  • how?adding where b.date is not null will still skip insertion of non existent records in b. i want to include the non existent with a predefined value in table a. Commented Sep 18, 2013 at 7:06

1 Answer 1

2

What about this

UPDATE

DECLARE myvar INT;
SELECT COUNT(*) INTO myvar FROM TableB WHERE Date=p_date;
SELECT myvar;

IF(myvar>0)
    SELECT B.Date,A.Value
    from TableB B 
    left JOIN TableA A ON A.id=B.id 
    WHERE B.id=4 
ELSE
    SELECT p_date AS Date,predefinedvalue AS Value
END IF;

You have to use left join because inner join will retrieve only the matching records. Also the predefined value has to be stored in B since if there is no matching record you can't query anything from A.

SELECT CASE WHEN myvar>0 THEN B.Date
       ELSE p_date
       END AS Date,
       CASE WHEN myvar>0 THEN A.Value
       ELSE predefinedvalue
       END AS Value
from TableB B 
left JOIN TableA A ON A.id=B.id 
WHERE B.id=4 
Sign up to request clarification or add additional context in comments.

5 Comments

it wont, because it wont check the case for the dates that does not exist in table b.hence, skipping it. i have to check that if a date does not exist, the predefined value be defined with the given date parameter.
How are you checking whether date is not exist in table B? Is there another table?
date is provided as a parameter(lets say p_date), its a part of a stored procedure.
this might work, i'll mark it as a right answer if i can not do it all in a single query. do you think it can be done in a single query?
IF ELSE can be removed with a case statement as i shown in the updated answer if that what you want.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.