Question

I am building an application in cold fusion which has a SQL Server database connection. I need group records and only return the first in the group. I wrote the following query in coldfusion.

SELECT FIRST(ID)
FROM table
GROUP BY NAME

Which is returning the following error:

[Macromedia][SQLServer JDBC Driver][SQLServer]'first' is not a recognized built-in function name.

Is the a way use the first function in a coldfusion query?

Is there an alternative way to accomplishment this?

*I do not have direct access to the database. Just a access to the cold fusion data connection

Was it helpful?

Solution

FIRST is not valid in SQL Server (you must be thinking of Access). Maybe you meant:

SELECT NAME, MIN(ID)
FROM dbo.table
GROUP BY NAME;

In SQL Server "Denali" you will be able to use FIRST_VALUE/LAST_VALUE in conjunction with windowing functions.

OTHER TIPS

FIrst and Last do not exist in Sql Server 2005 or 2008, but in Sql Server 2012 there is a FirstValue, LastValue function. I tried to implement the aggregate First and Last for Sql Server 2005 and came to the obstacle that sql server does guarantee the calculation of the aggregate in a defined order. (See attribute SqlUserDefinedAggregateAttribute.IsInvariantToOrder Property, which is not implemented.) This might be because the query analyser tries to execute the calculation of the aggregate on multiple threads and combine the results, which speeds up the execution, but does not guarantee an order in which elements are aggregated.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top