CFLib.org – Common Function Library Project

QueryTranspose(inputQuery[, includeHeaders])

Last updated August 24, 2005

author

Glenn Buteau

Version: 1 | Requires: CF6 | Library: DataManipulationLib

Description:
Transpose a 2D query, turning rows into columns and columns into rows. Original column headers can be turned on or off.

Return Values:
Returns a query.

Example:

<cfset myQuery = QueryTranspose(myQuery)>

Parameters:

Name Description Required
inputQuery The query to transpose. Yes
includeHeaders Determines if headers should be included as a column. Defaults to true. No

Full UDF Source:

<!---
 Transpose a query.
 
 @param inputQuery      The query to transpose. (Required)
 @param includeHeaders      Determines if headers should be included as a column. Defaults to true. (Optional)
 @return Returns a query. 
 @author Glenn Buteau (glenn.buteau@rogers.com) 
 @version 1, August 24, 2005 
--->
<cffunction name="queryTranspose" returntype="query">
    <cfargument name="inputQuery" type="query" required="true">
    <cfargument name="includeHeaders" type="boolean" default="true" required="false">
        
    <cfset var outputQuery = QueryNew("")>
    <cfset var columnsList = inputQuery.ColumnList>
    <cfset var newColumn = ArrayNew(1)>
    <cfset var row = 1>
    <cfset var zeroString = "000000">
    <cfset var padFactor = int(log10(inputQuery.recordcount)) + 1 >
    <cfset var i = "">
        
    <cfif includeHeaders>
        <cfset queryAddColumn(OutputQuery,"col_#right(zeroString & row, padFactor)#",listToArray(ColumnsList))>
        <cfset row = row + 1>
    </cfif>    

    <cfloop query="inputQuery">
        <cfloop index="i" from="1" to="#listlen(columnsList)#">
            <cfset newColumn[i] = inputQuery[ListGetAt(columnsList, i)][currentRow]>
        </cfloop>
        <cfset queryAddColumn(outputQuery,"col_#right(zeroString & row, padFactor)#",newColumn)>
        <cfset row = row + 1>
    </cfloop>
    
    <cfreturn outputQuery>
</cffunction>

Search CFLib.org


Latest Additions

Raymond Camden added
QueryDeleteRows
November 04, 2017

Leigh added
nullPad
May 11, 2016

Raymond Camden added
stripHTML
May 10, 2016

Kevin Cotton added
date2ExcelDate
May 05, 2016

Raymond Camden added
CapFirst
April 25, 2016

Created by Raymond Camden / Design by Justin Johnson