Hi all - My SQL expertise is amateur at best, so hope this is documented correctly.
QUERY:
select financial_statement.acct_nbr, financial_statement.stmt_dt
from account, financial_statement
where account.acct_nbr *= financial_statement.acct_nbr
and account.acct_nbr = '123456'
and (stmt_dt = (select MAX(stmt_dt) from financial_statement))
PROBLEM: Returns NULL values.
There are 2 financial statement records for acct_nbr 123456 => one is 2001-08-09 and the other is 2000-09-30.
If I change the above query to use MIN instead of MAX, it does return the 2000-09-30. However, I am looking for the most recent statement for this particular record which would be the 2001-08-09 statement date.
I also tried this query:
select financial_statement.acct_nbr, financial_statement.stmt_dt
from account, financial_statement
where account.acct_nbr *= financial_statement.acct_nbr
and account.acct_nbr = '123456'
and (stmt_dt = (select top 1(stmt_dt) from financial_statement order by stmt_dt desc))
...which returns NULLS as well. If I remove the "desc" from order by which would be asc, I do get the 2000-09-30.
Ultimately, this query will actually be part of a larger query involving several other tables and will eventually be used in Crystal Reports 8.0 but this section should be sufficient for test purposes.
I did find an article from Microsoft knowledge base #310472 titled: "Incorrect Results with Uncorrelated Subquery If You Use an Aggregate Function and NULL Values in the WHERE Clause". The resolution was to apply latest MS SQL 2k service pack.
I applied Sp3a and rebooted but issue still exists.
Appreciate any/all advice in advance!
QUERY:
select financial_statement.acct_nbr, financial_statement.stmt_dt
from account, financial_statement
where account.acct_nbr *= financial_statement.acct_nbr
and account.acct_nbr = '123456'
and (stmt_dt = (select MAX(stmt_dt) from financial_statement))
PROBLEM: Returns NULL values.
There are 2 financial statement records for acct_nbr 123456 => one is 2001-08-09 and the other is 2000-09-30.
If I change the above query to use MIN instead of MAX, it does return the 2000-09-30. However, I am looking for the most recent statement for this particular record which would be the 2001-08-09 statement date.
I also tried this query:
select financial_statement.acct_nbr, financial_statement.stmt_dt
from account, financial_statement
where account.acct_nbr *= financial_statement.acct_nbr
and account.acct_nbr = '123456'
and (stmt_dt = (select top 1(stmt_dt) from financial_statement order by stmt_dt desc))
...which returns NULLS as well. If I remove the "desc" from order by which would be asc, I do get the 2000-09-30.
Ultimately, this query will actually be part of a larger query involving several other tables and will eventually be used in Crystal Reports 8.0 but this section should be sufficient for test purposes.
I did find an article from Microsoft knowledge base #310472 titled: "Incorrect Results with Uncorrelated Subquery If You Use an Aggregate Function and NULL Values in the WHERE Clause". The resolution was to apply latest MS SQL 2k service pack.
I applied Sp3a and rebooted but issue still exists.
Appreciate any/all advice in advance!