sqlcoldfusionqoq

Get max value of an query column


i am trying to loop through a query, to get the highest createdTime. The column instanceId is a GUID (string) what gives me this error: enter image description here

The reference 396B3850 is the beginning of an instanceId

<cfquery datasource="hostmanager" name="licenses">
SELECT *
FROM licenses
</cfquery>

<cfloop query="licenses">
    <cfquery name="getHighestCreatedTime" dbtype="query">
    SELECT MAX(CREATEDTIME)
    FROM licenses
    WHERE instanceId = #licenses.instanceId#
    AND startDate = #licenses.startDate#
    </cfquery>
</cfloop>

Solution

  • I'm not too familiar with ColdFusion, but can you not just run a query for the max value? For example

    SELECT TOP 1 CREATEDTIME 
    FROM   licenses 
    WHERE  {any conditions you want} 
    ORDER BY CREATEDTIME DESC