![]() | |
#1
| |||
| |||
|
#2
| ||||||
| ||||||
|
|
I have the following data: |
|
I want to get back the max EffectiveDate that is less than [sic: earlier than] 1999-01-01 for those rows whose tax_name is cs080. |
|
If I want to get back the max EffectiveDate that is less than [sic: earlier than] 1999-01-01 for those rows whose tax_name is cs080 and |
|
If I change the gender to female, I thought I would get the one female that is in there |
|
I thought the outer clause (including its WHERE) would get processed and then each resultant row would be used to determine the |
|
Sorry if I'm way off base, but every time I see something about correlated subqueries, only one column is ever shown in the outer |
#3
| |||
| |||
|
#4
| |||
| |||
|
|
Suzanne Dorman (suzanne.dorman (AT) segllc (DOT) com) wrote: : I'm trying to understand how correlated subqueries are processed. It : seems especially confusing when there are multiple conditions in the : outer where clause. : I have the following data: : TaxName Gender State BandAmount EffectiveDate : ----------------------------------------------------- : cs080 Male CT 28000 1961/01/01 : cs080 Female DE 35000 1950/01/01 : tax Male CT 37000 1980/01/01 : cs080 Male CT 28000 1981/01/01 : cs080 Male FL 28000 2001/01/01 : I want to get back the max EffectiveDate that is less than 1999/01/01 : for those rows whose TaxName is cs080. : SELECT * : FROM Table2 t1 : WHERE TaxName = 'cs080' : AND EffectiveDate = (SELECT MAX(t2.EffectiveDate) FROM Table2 t2 : WHERE t2.TaxName = t1.TaxName AND : t2.EffectiveDate <= '1999/01/01') : This works fine, returning: : cs080 Male CT 28000 1981/01/01 : If I want to get back the max EffectiveDate that is less than : 1999/01/01 for those rows whose TaxName is cs080 and whose Gender is : male: : SELECT * : FROM Table2 t1 : WHERE TaxName = 'cs080' : AND Gender = 'Male' : AND EffectiveDate = (SELECT MAX(t2.EffectiveDate) FROM Table2 t2 : WHERE t2.TaxName = t1.TaxName AND : t2.EffectiveDate <= '1999/01/01') : This works fine too, returning the same line: : cs080 Male CT 28000 1981/01/01 : If I change the gender to female, I thought I would get the one female : that is in there : SELECT * : FROM Table2 t1 : WHERE TaxName = 'cs080' : AND Gender = 'Female' : AND EffectiveDate = (SELECT MAX(t2.EffectiveDate) FROM Table2 t2 : WHERE t2.TaxName = t1.TaxName AND : t2.EffectiveDate <= '1999/01/01') : But I get nothing! : I thought the outer clause (including its WHERE) would get processed : and then each resultant row would be used to determine the max : effective date. As you see, it doesn't. The inner query will always select all the rows from the entire table, subject to the conditions you provide. In this case the only condition is that the inner query must select rows that have the same Taxname as the "potentially selected" row from outer query. There are lots of rows with the same taxname as the female's row , but the max effective date of those rows belongs to a Male. Therefore his date is selected in the inner query, and then her row is only selected if her date happens to be the same as his, which it isn't, so her row is not selected. Apparently this isn't what you want, but there is nothing inherently wrong with this. You might very well want only those females who have dates later than anyone else, male or female. |
#5
| |||
| |||
|
|
As you see, it doesn't. The inner query will always select all the rows from the entire table, subject to the conditions you provide. In this case the only condition is that the inner query must select rows that have the same Taxname as the "potentially selected" row from outer query. There are lots of rows with the same taxname as the female's row , but the max effective date of those rows belongs to a Male. Therefore his date is selected in the inner query, and then her row is only selected if her date happens to be the same as his, which it isn't, so her row is not selected. Apparently this isn't what you want, but there is nothing inherently wrong with this. You might very well want only those females who have dates later than anyone else, male or female. Thanks. Using the table and not the results of the outer select is quite different than my understanding. So how would I do what I want? I want to select all rows that have a TaxName of cs080 and a Gender of Female and then, from that set, I want it to only return the row (or rows) with the maximum effective date. Do I have to write 2 separate queries? If I use a correlated subquery, do I have to specify every variable in the outer WHERE as part of the inner WHERE? Obviously, I have supplied a very simple test case. My table and my query are more complicate with the query having 8 conditions in the WHERE which must be met before it determines the max effective date (which is why the correlated subquery seemed so messy - and slow!) How about to add a condition in subquery? |
![]() |
| Thread Tools | |
| Display Modes | |
| |