Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Effective Date Year < Year in a Different Table but Year is based on the 1st 3 Chars in String, e

Status
Not open for further replies.

kernal

Technical User
Feb 27, 2001
415
US
Are the results needed toward the bottom of this post, even possible? I hope I've explained it so it makes sense.

TABLES:

MAIN TABLE

ID TITLE EFFECTIVE_DATE (This is in the table as string and not date)
1 Placement Testing 10/01/2015
1 Testing 10/01/2014
1 Test 10/01/2012


JOIN TABLE

ID TERM (1st character is for 2000 and characters in position 2 and 3 is the year)
1 1175 (2017)
1 1145 (2014)
1 1135 (2013)

Result Needed (I only want the MAIN TABLE's last effective date year title < JOIN TABLE'S term year)

ID TERM TITLE
1 1175 (2017) Placement Testing - 2015 is the MAIN TABLE's last effective date year < 1175 (2017)
1 1145 (2014) Test - 2012 is the MAIN TABLE's last effective date year < 1145 (2014)
1 1135 (2013) Test - 2012 is the MAIN TABLE's last effective date year < 1135 (2013)

Any help is greatly appreciated!
 
The character string of year can be calculated as:
SELECT '20' || SUBSTR(JoinTable.TERM, 2, 2) FROM JoinTable;
The character string should be converted using TO_DATE for date range calculations.


==================================
advanced cognitive capabilities and other marketing buzzwords explained with sarcastic simplicity


 
SELECT A.Id, MAX(TO_DATE(A.Effective_date, 'MM/DD/YYYY'))
FROM Join_table B, Main_table A
WHERE A.Id = B.Id
AND TO_DATE(A.Effective_date, 'MM/DD/YYYY')
< TO_DATE('01/01/20' || SUBSTR(B.Term,2,2),'MM/DD/YYYY')
GROUP BY A.Id

Bill
Lead Application Developer
New York State, USA
 
Hi Bill,

It works great if I don't have the TITLE as a field. When I add it then I get the error "ORA-00979: not a GROUP BY expression."

Thanks for your help!
 
Show your Select statement with the "TITLE as a field".
Did you add it to the SELECT and the GROUP BY part of your statement?


---- Andy

There is a great need for a sarcasm font.
 
Adding the title is easy, just add it to the group by



Code:
SELECT A.Id,a.Title MAX(TO_DATE(A.Effective_date, 'MM/DD/YYYY'))
FROM Join_table B, Main_table A
WHERE A.Id = B.Id
AND TO_DATE(A.Effective_date, 'MM/DD/YYYY')
< TO_DATE('01/01/20' || SUBSTR(B.Term,2,2),'MM/DD/YYYY')
GROUP BY A.Id,a.Title;

When you are using aggregates in your select like MAX, MIN, SUM, AVG,... any items from the select that are not aggregated must be in a group by.

Bill
Lead Application Developer
New York State, USA
 
When I don't have Title as a field and in the GROUP BY

ID Term Effective_Date(Year)
1 1175 2015
1 1145 2012
1 1135 2012

When I add Title as field and GROUP BY

ID Term Effective_Date(Year) Title
1 1175 2015  Placement Testing (I just want this title in effective date's year 2015 since 2015 is just prior to the year in this Term year 2017)
1 1175 2014 Testing
1 1175 2012 Test

1 1145 2012 Test

1 1135 2012 Test

Thanks for everyone's help. I really appreciate it!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top