Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Retrieve total and subtotal
Message
De
30/10/2000 17:45:32
 
 
Information générale
Forum:
Visual FoxPro
Catégorie:
Base de données, Tables, Vues, Index et syntaxe SQL
Divers
Thread ID:
00436113
Message ID:
00436131
Vues:
19
>I need to return one recordset a column for each record but also another column for subtotal of that same column. This is something like how I've done it in VFP:
>
>SELECT orderid, itemid, qty FROM itemtable WHERE orderid=123 INTO CURSOR cTItem
>
>SELECT itemid, SUM(qty) AS subtotal FROM itemtable ;
> WHERE orderid=123 GROUP BY itemid INTO CURSOR cTItem2
>
>SELECT A.orderid, A.itemid, A.qty, B.subtotal ;
> FROM cTItem A JOIN cTItem2 B ;
> ON A.itemid=B.itemid ;
> INTO CURSOR cWhatINeed
>
>If the DB is now MSSQL and the app is now VB, how would I handle something like this? TIA.

How's this:
lnHandle = SQLCONNECT("myconnection")
SQLEXEC(lnHandle, ;
  "SELECT itemid, orderid, SUM(qty) AS subtotal WHERE orderid = 123 GROUP BY itemid, orderid WITH ROLLUP", ;
  "cWhatINeed")
I put the orderid 2nd, since it's the static value.
The row with itemid = .NULL. will be the grand total for the non-null qty's.
Each row where orderid = .NULL. is the subtotal of the itemid,orderid in the previous row.

You might also want to try WITH CUBE instead of WITH ROLLUP, which will get totals by itemid and orderid.

For other examples, check out SQL Books Online and search for either COMPUTE or ROLLUP

HTH
Insanity: Doing the same thing over and over and expecting different results.
Précédent
Répondre
Fil
Voir

Click here to load this message in the networking platform