Clear All Create Table xCursor ; (CheckDate d, Xclusive l, Id1 i, Id2 i, nStart i, nEnd i) Scatter Memvar For nDate = 1 To 150 For lExclusive = 0 To 1 For iid1=1 To 10 For iid2=1 To 10 InsertSet(Date()-m.nDate,m.lExclusive=1,m.iid1,m.iid2) Endfor Endfor Endfor Endfor Return FUNCTION InsertSet Lparameters tdDate, tlXclusive, tnID1, tnID2 Insert Into xCursor Values (tdDate, tlXclusive, tnID1, tnID2, 96,120) Insert Into xCursor Values (tdDate, tlXclusive, tnID1, tnID2,108,120) Insert Into xCursor Values (tdDate, tlXclusive, tnID1, tnID2,108,120) Insert Into xCursor Values (tdDate, tlXclusive, tnID1, tnID2,108,120) Insert Into xCursor Values (tdDate, tlXclusive, tnID1+1000, tnID2, 96,108) Insert Into xCursor Values (tdDate, tlXclusive, tnID1+1000, tnID2,102,120) Insert Into xCursor Values (tdDate, tlXclusive, tnID1+1000, tnID2,102,120) Insert Into xCursor Values (tdDate, tlXclusive, tnID1, tnID2+1000, 96,108) Insert Into xCursor Values (tdDate, tlXclusive, tnID1, tnID2+1000, 96,132) Insert Into xCursor Values (tdDate, tlXclusive, tnID1, tnID2+1000, 102,108) Insert Into xCursor Values (tdDate, tlXclusive, tnID1, tnID2+1000, 102,120) Insert Into xCursor Values (tdDate, tlXclusive, tnID1, tnID2+1000, 102,120) endfuncOne of my working routines :
CLEAR all clear SELECT * from xCursor ; ORDER BY CheckDate, Xclusive, Id1, Id2, nStart, nEnd ; INTO cursor TestCursor ; nofilter GetCounts('TestCursor','crsOut',.T.) Function GetCounts Lparameters tcInCursor,tcOutCursor,tlID1Check Local Array arrStruc[1] LOCAL lnStart lnStart = SECONDS() SELECT (tcInCursor) Afields(arrStruc) Create Cursor crsXTab From Array arrStruc Select CheckDate, Xclusive, Id1, Id2, nStart As nPos From (tcInCursor) ; UNION ; (Select CheckDate, Xclusive, Id1, Id2, nEnd As nPos From (tcInCursor)) ; ORDER By 1,2,3,4,5 ; INTO Cursor startPoint ; nofilter ? 'Unioned', SECONDS()-lnStart Use Dbf('StartPoint') In 0 Again Alias endPoint Set Relation To Recno()+1 Into endPoint Scan If startPoint.CheckDate = endPoint.CheckDate ; AND startPoint.Xclusive = endPoint.Xclusive ; AND startPoint.Id1 = endPoint.Id1 ; AND startPoint.Id2 = endPoint.Id2 Insert Into crsXTab ; (CheckDate, Xclusive, Id1, Id2, nStart, nEnd) ; VALUES (startPoint.CheckDate,startPoint.Xclusive,startPoint.Id1,startPoint.Id2,startPoint.nPos,endPoint.nPos) ENDIF ENDSCAN ? 'crsXtab Ready', SECONDS()-lnStart Select a.CheckDate, a.Xclusive, a.Id1, a.Id2, b.nStart, b.nEnd, Cnt(*) As cnts ; FROM (tcInCursor) a INNER Join crsXTab b ; ON a.CheckDate = b.CheckDate ; AND a.Xclusive = b.Xclusive ; AND a.Id1 = b.Id1 ; AND a.Id2 = b.Id2 ; AND a.nEnd >= b.nEnd ; AND a.nStart <= b.nStart ; GROUP By 1,2,3,4,5,6 ; INTO Cursor (tcOutCursor) ; nofilter ? 'Counts Ready', SECONDS()-lnStart EndfuncPS: Test table is named xCursor and no indexes on it. Did that way because it resembles the situation in actual code where this is needed (it's a result of SQL).