views:

2744

answers:

3

This is my SQL statement that works using datediff..............

SELECT SUM(b134_nettpay) AS Total, b134_rmcid, b134_recdate 
FROM  B134HREC 
WHERE datediff (dd, '2006-05-05', getdate()) > 90
GROUP BY b134_rmcid, b134_recdate 
ORDER BY b134_recdate DESC, b134_rmcid

I need to Replace the Hardcoded date '2006-05-05' with MAX(b134_recdate) like so.........

SELECT SUM(b134_nettpay) AS Total, b134_rmcid, b134_recdate 
FROM  B134HREC 
where datediff (dd, MAX(b134_recdate), getdate()) > 90
GROUP BY b134_rmcid, b134_recdate 
ORDER BY b134_recdate DESC, b134_rmcid

But i get this Error message...........

"An aggregate may not appear in the WHERE clause unless it is in a subquery contained in a HAVING clause or a select list, and the column being aggregated is an outer reference."

Any idea how to fix my SQL Statement?

Thanks in advanced! Etienne

A: 

Have you tried moving this:

datediff (dd, MAX(b134_recdate), getdate()) > 90

to a HAVING clause?

The sql statement would be:

SELECT SUM(b134_nettpay) AS Total, b134_rmcid, b134_recdate FROM B134HRE GROUP BY b134_rmcid, b134_recdate ORDER BY b134_recdate DESC, b134_rmcid HAVING datediff (dd, MAX(b134_recdate), getdate()) > 90

Sam
Tried this yes. Having datediff (dd, MAX(b134_recdate), getdate()) > 90.........then i get the error message "Incorrect syntax near the keyword 'GROUP' "
Etienne
"where" limits the rows that are grouped, "having" limits which groups are returned. that will not work exactly the same
KM
Putting recdate in "having" means you can't group by it.
Binary Worrier
funny, it should work... how was the entire statement then? Was the having the last clause?
Sam
+5  A: 

Try

SELECT SUM(b134_nettpay) AS Total, b134_rmcid, b134_recdate 
FROM  B134HRE 
where datediff (dd, 
      (select MAX(b134_recdate) from B134HRE)
      , getdate()) > 90
GROUP BY b134_rmcid, b134_recdate 
ORDER BY b134_recdate DESC, b134_rmcid
Binary Worrier
This addresses the "or a select list" as suggested by the error message
Binary Worrier
As it stands the query cannot take advantage of any index that might be on b134_recdate (except for the max). If it were "where B134_rec_date > DateAdd(dd, getDate(), 90)" it would use an index, if it existed. NB: Not sure if dateadd syntax is correct, but you get the idea.
Binary Worrier
A: 

This code is for sql server

I'm not sure if the MAX sould be for the entire table:

DECLARE @MaxDate  datetime
SELECT @MaxDate=MAX(b134_recdate) FROM B134HRE
/* if you don't want time you need to trim it from @maxDate*/

    SELECT
        SUM(b134_nettpay) AS Total, b134_rmcid, b134_recdate 
        FROM  B134HRE 
        where datediff (dd, @MaxDate, getdate()) > 90
        GROUP BY b134_rmcid, b134_recdate 
        ORDER BY b134_recdate DESC, b134_rmcid

or per each group:

SELECT
    SUM(b.b134_nettpay) AS Total, b.b134_rmcid, b.b134_recdate 
    FROM  B134HRE   b
        INNER JOIN (SELECT
                        b134_rmcid, b134_recdate , MAX(b134_recdate) AS MaxDate
                        FROM B134HRE
                        GROUP BY b134_rmcid, b134_recdate 
                   ) dt ON b.b134_rmcid=dt.b134_rmcid AND b.b134_recdate=dt.b134_recdate
    where datediff (dd, dt.MaxDate, getdate()) > 90
    GROUP BY b.b134_rmcid, b.b134_recdate 
    ORDER BY b.b134_recdate DESC, b.b134_rmcid

but you could give these a try

KM