Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
SQL aggregate
Message
 
 
To
26/03/2010 08:36:32
Lutz Scheffler
Lutz Scheffler Software Ingenieurbüro
Dresden, Germany
General information
Forum:
Visual FoxPro
Category:
Databases,Tables, Views, Indexing and SQL syntax
Title:
Environment versions
Visual FoxPro:
VFP 9 SP2
Database:
Visual FoxPro
Miscellaneous
Thread ID:
01457370
Message ID:
01457416
Views:
96
This message has been marked as the solution to the initial question of the thread.
I'm not sure why you would need a subquery
CREATE CURSOR crsTest (pk i autoinc, grp i, bitfld i)

FOR i=1 TO 5
	FOR j= 1 TO 4
		INSERT INTO crsTest (grp, bitfld) VALUES (i,j* 10^i)
	ENDFOR
ENDFOR

pnResult = 0
pnGroup = 0

SELECT grp, MAX(BitorSql(grp, bitfld)) AS BitField ;
	FROM crsTest ;
	GROUP BY grp
RETURN


FUNCTION BitorSql(tnGroup, tnVal)
IF tnGroup <> pnGroup 
	pnResult = 0
ENDIF	
pnResult = BITOR(pnResult, tnVal)
RETURN pnResult
>
>To me it looks like
>
>SELECT ..., BITOR(0,myfield) ..
>
>
>It would need to store the intermediate in pnResult like
>
>pnResult = 0
>
>SELECT
>
>FUNCTION BITORSQL(tnVal)
>pnResult =BITOR(pnResult , tnVal)
>RETURN pnResult
>
>
>and to reset pnResult for each unifier (e.g. group), see below
>
>
>But anyway BITORSQL is no aggregate function so i end up with error 1807"GROUP BY clause is invalid "
>
>If I SET ENGINEBEHAVIOR 70 it aggregates, but does not, reset, so i change
>(asuming i1 is unifier and i2 is pattern)
>
>pnResult = 0
>pnLastUni = 0
>
>SELECT i1,BITORSQL(i2,i1) FROM muh GROUP BY 1
>
>FUNCTION BITORSQL(tnVal,tnUni)  
> IF tnUni#pnLastUni THEN
>  pnLastUni = tnUni
>  pnResult  = 0
> ENDIF &&tnUni#pnLastUni
> pnResult =BITOR(pnResult , tnVal)
> RETURN pnResult
>ENDFUNC &&BITORSQL(tnVal,tnUni) 
>
>I get something, bot a closer look shows that the function will not be called for every record in the source
>
>so, after a closer look at subqueries, I have tried
>
>pnResult = 0
>pnLastUni = 0
>
>SELECT;
> Cur1.i1,;
> (SELECT MAX(BITORSQL(cur2.i2,cur2.i1)) AS i2 FROM muh AS cur2 WHERE cur2.i1=cur1.i1) AS i2;
> FROM muh AS cur1;
> GROUP BY 1
>
>
>FUNCTION BITORSQL(tnVal,tnUni)
> IF tnUni#pnLastUni THEN
>  pnLastUni = tnUni
>  pnResult  = 0
> ENDIF &&tnUni#pnLastUni
> pnResult =BITOR(pnResult , tnVal)
> RETURN pnResult
>ENDFUNC &&BITORSQL(tnVal,tnUni)
>
>
>and unbelievably it works. MAX is ok, I will not use bit 31 anyway.
>
>So now a bit of cleanup for better variable handling
>
>LOCAL;
> lnResult,;
> lnLastUni
>
>STORE 0 TO;
> lnResult,;
> lnLastUni
>
>SELECT;
> Cur1.i1,;
> (SELECT MAX(BITORSQL(cur2.i2,cur2.i1,@lnResult,@lnLastUni)) AS i2 FROM muh AS cur2 WHERE cur2.i1=Cur1.i1) AS i2,;
> sum(i2);
> FROM muh AS Cur1;
> GROUP BY 1
>
>
>FUNCTION BITORSQL(tnVal,tnUni,tnResult,tnLastUni)
> IF tnUni#tnLastUni THEN
>  tnLastUni = tnUni
>  tnResult  = 0
> ENDIF &&tnUni#tnLastUni
> tnResult =BITOR(tnResult , tnVal)
> RETURN tnResult
>ENDFUNC &&BITORSQL(tnVal,tnUni)
>
>
>BTW the subquery seems to be identified as aggregate. I have SET ENGINEBEHAVIOR 80 and GROUP BY is only row 1, but all runs fine. Mayby not because GROUP BY 1,2 changes nothing.
>
>Thank you
>Agnes
>
>>Hi Agnes,
>>
>>Would this work for you?
>>
>>pnResult = 0
>>
>>SELECT ..., BITORSQL(myfield) ...
>>
>>FUNCTION BITORSQL(tnVal)
>>RETURN BITOR(pnResult , tnVal)
>>
>>
>>>
>>>I have a small problem to aggregate a field.
>>>
>>>The field is a integer holding bit patterns.
>>>
>>>I like to aggregate like a BITOR, IOW all bits set should remain.
>>>
--sb--
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform