Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 SQL Server Development (2000)
 FIRST, LAST aggregate functions

Author  Topic 

AskSQLTeam
Ask SQLTeam Question

0 Posts

Posted - 2005-03-21 : 08:29:11
Donna writes "I have searched high and low to find a SQL counterpart to the MS Access FIRST and LAST function.

This is what I am trying to do:

SELECT Field1, Field2, ..., Field5, SUM(Field7), SUM(Field8),
FIRST(DateField9), FIRST(Field10), ..., FIRST(Field15)
FROM ...
GROUP BY Field1, Field2, ..., Field5
ORDER BY Field1, Field2, ..., Field5, DateField9 ASC

I am trying to group on 5 key fields, sum two fields across the group and then select 6 fields in the group associated with the earliest date.

I am trying to accomplish this with the fewest number of subqueries/joins because of the large number of records involved. I am currently using the Crystal report to perform the groupings but I don't like the fact that I am returning over 150,000 records over the network to have the report display a couple of thousand entries.

I appreciate any help you can give me."

robvolk
Most Valuable Yak

15732 Posts

Posted - 2005-03-21 : 08:30:00
No such thing as FIRST and LAST, use MIN or MAX instead. In fact, they are more appropriate than FIRST and LAST are for what you're trying to do.
Go to Top of Page
   

- Advertisement -