If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > MySQL > Branching between an Expression and IS NULL

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 04-20-10, 10:23
Ikviens Ikviens is offline
Registered User
 
Join Date: Mar 2006
Posts: 55
Branching between an Expression and IS NULL

Hello again,

Code:
DROP TABLE IF EXISTS temp_codes;
CREATE TABLE temp_codes (end_year INT(4), code_value INT(1));
INSERT INTO temp_codes VALUES (1990,1),(1991,2),(1992,3),(1994,4),(1998,5),(2005,6),(null,7);
I have this table that keeps code values eligible until certain times. If the field end_year IS NULL, that means the record is alive or eligible for the current time.

The SQL statement for retrieving the current code value is:
Code:
SELECT code_value 
  FROM temp_codes 
 WHERE end_year IS NULL;
For historical values, take Year 1993 e.g., the relevant value is retrieved by looking for the least upper bound of 1993:
Code:
SELECT code_value 
  FROM temp_codes T1
 WHERE end_year =  (SELECT MIN(end_year)
                      FROM temp_codes T2
                     WHERE T2.end_year >= 1993);
I am having trouble combining the two conditions in the WHERE clause to create a SELECT statement that works regardless of the current / historical distinction of the input year.

Using the CASE construction looked promising but all formulations I tried just keep failing. Among them is the following that tries to find a value on the assumption that the input year is historical then, if this fails, look for the current one.
Code:
SELECT code_value 
  FROM temp_codes T1 
 WHERE end_year CASE WHEN EXISTS (SELECT * FROM temp_codes T2 WHERE end_year >= 1993)
                     THEN (SELECT MIN(end_year) FROM temp_codes T2 WHERE end_year >= 1993)
            ELSE NULL END;
TIA
Ik
Reply With Quote
  #2 (permalink)  
Old 04-20-10, 11:14
dav1mo dav1mo is offline
Registered User
 
Join Date: Dec 2007
Location: Richmond, VA
Posts: 782
Code:
SELECT code_value 
  FROM temp_codes T1
 WHERE end_year =  (SELECT MIN(end_year)
                      FROM temp_codes T2
                     WHERE T2.end_year >= 1993)
        OR end_year IS NULL;
Dave
Reply With Quote
  #3 (permalink)  
Old 04-20-10, 11:27
Ikviens Ikviens is offline
Registered User
 
Join Date: Mar 2006
Posts: 55
Hi, Dave.

Sorry but the script does not work because it retrieves not one but two rows (code_value = 4, 7; where only the former is relevant).
Reply With Quote
  #4 (permalink)  
Old 04-21-10, 06:07
it-iss.com it-iss.com is offline
Registered User
 
Join Date: Sep 2009
Location: San Sebastian, Spain
Posts: 620
SELECT code_value
FROM temp_codes T1
WHERE end_year = (SELECT MIN(end_year)
FROM temp_codes T2
WHERE T2.end_year >= 1993)
OR end_year IS NULL
ORDER BY T1.end_year DESC
LIMIT 1;
__________________
Ronan Cashell
Senior Oracle/MySQL DBA
http://www.it-iss.com
Reply With Quote
  #5 (permalink)  
Old 04-21-10, 09:11
Ikviens Ikviens is offline
Registered User
 
Join Date: Mar 2006
Posts: 55
It's messy but it does the work. Thank you, Ronan.

Actually my question is motivated by a comment in this Blog entry by Tom Kyte (he discussed a lot of Oracle-specific things so I am trying to bring issues about the Standard here).

Quote:
Originally Posted by jimmyb
I don't fear nulls. I fear developers that use default values in place of nulls. Such as, 31-DEC-9999 as a replacement for nulls.
Okay, it looks like I should keep to the old 'expression BETWEEN effective_from AND effective_to' with a dummy date for the alive rows...... :-(
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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

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