Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Retrieve total and subtotal
Message
From
30/10/2000 17:45:32
 
 
General information
Forum:
Visual FoxPro
Category:
Databases,Tables, Views, Indexing and SQL syntax
Miscellaneous
Thread ID:
00436113
Message ID:
00436131
Views:
18
>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.
Previous
Reply
Map
View

Click here to load this message in the networking platform