Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Can I do This with Select - SQL ?
Message
Information générale
Forum:
Visual FoxPro
Catégorie:
Codage, syntaxe et commandes
Divers
Thread ID:
00223157
Message ID:
00223396
Vues:
19
>>>>hi all :
>>>>
>>>>Hi was trying to make a column with the total's of the other two and paste the result to the next row .... more or less like this:
>>>>
>>>>Col a - Col b = total
>>>>10 0 10
>>>>0 15 -5
>>>>
>>>>Etc...
>>>>Can i do This with a SQL ? I tryed to use :
>>>>SELECT a,b, SUM(a+b) as total
>>>>but it always gives me just one record (with -5)!
>>>>
>>>>Thanks in Advance !
>>>
>>>Don't use the SUM(), SUM() will always go an aggregate based on either ALL RECORDS, or a given group by...
>>>
>>>select a, b, a-b as total from mytable...
>>
>>
>>First , Thanks for your reply .
>>
>>But That only give me (a-b) for current record .
>>What i would like to get is (a-b) from the previous record plus (a-b) from the current record.
>>EX:
>>10-1 = 9
>>10-5 (= 5+9) = 14
>
>It's better to do it with SCAN:
>select mytable
>nTotal=0
>scan
> nTotal=nTotal+mytable.field1-mytable.field2
> Replace mytable.total with nTotal
>endscan

Does he wish to accumulate nTotal through the whole table? If not, this code is easily modified, but a SQL solution would also be possible, something like:

SELECT field1, field2, RECNO() AS numfield FROM mytable INTO CURSOR mytable1 ORDER BY whatever

SELECT mytable1.numfield, mytable1.field1, mytable1.field2, (mytable2.field1 - mytable2.field2) + (mytable1.field1 - mytable1.field2) AS twolinetotal FROM mytable1 INNER JOIN mytable1 AS mytable2 ON mytable1.numfield = mytable2.numfield + 1 INTO CURSOR results ORDER BY 1

Is there any reason this would be slower than SCAN?
Précédent
Répondre
Fil
Voir

Click here to load this message in the networking platform