Existe alguna función para tablas pivot para TDolphin o Mysql nativa?
Hice una adaptación de las funciones de ADO pero no quiero reinventar la rueda.
Agradezco comentarios
Dejo aquí el ejemplo
- Code: Select all Expand view
- *
#include "FiveWin.Ch"
#include "ord.ch"
#include "xbrowse.ch"
#include "tdolphin.ch"
//----------------------------------------------------------------------------//
function Main()
local oQry, oCn
local cServer := 'localhost', cUser := 'root', cPassword := 'secret', cDBName := 'test', nPort := "3306"
FWCONNECT oCn HOST cServer USER cUser PASSWORD cPassword DB cDBName PORT VAL(nPort)
oQry := PivotDolphin( oCn, 'PVTDATA', 'REGION', 'PRODUCT', 'SALES', 'COUNT')
xbrowse(oQry)
return nil
//----------------------------------------------------------------------------//
function PivotDolphin(oCn, cTable, cRowFld, cColFld, cValFld, cAggrFunc )
local cSql, n, oQry
oQry := oCn:Query("SELECT DISTINCT " + cColFld + " as col FROM " + cTable + " DST")
if ( n := At( " AS ", cRowFld ) ) > 0
cRowFld := Left( cRowFld, n + 3 ) + ;
FW_QuotedColSQL( AllTrim( SubStr( cRowFld, n + 4 ) ) )
else
cRowFld := FW_QuotedColSQL( cRowFld )
endif
if ( n := At( " AS ", cColFld ) ) > 0
cColFld := Trim( Left( cColFld, n - 1 ) )
endif
cSql := "SELECT " + cRowFld
do while !oQry:Eof()
cSql += ", " + cAggrFunc + "( CASE WHEN " + cColFld + " = " + ClipValue2Sql( oQry:col ) + " THEN " + cValFld + " ELSE 0 END ) AS " + ;
If( ValType( oQry:col ) == 'C', FW_QuotedColSQL( oQry:col ), CharRem( "-/.", "COL_" + cValToChar( oQry:col ) ) )
oQry:Skip()
enddo
cSql += " FROM " + cTable + " PVTTBL GROUP BY " + ;
If( ( n := At( " AS ", Upper( cRowFld ) ) ) > 0, Left( cRowFld, n - 1 ), cRowFld )
return oCn:Query(cSql)