INF: COMPUTE and the Microsoft SQL Server ODBC driverID: Q157800
|
The COMPUTE BY clause in a SQL Server statement generates subtotals within result sets, whereas the COMPUTE clause generates a total at the end of the result set. The Microsoft SQL Server ODBC driver presents this total back to the calling application by generating multiple result sets for the SELECT.
The following example from the Transact-SQL Reference uses COMPUTE BY and
COMPUTE to generate subtotals and the total:
SELECT title = CONVERT(char(20), title), type, price, advance
FROM titles
WHERE ytd_sales IS NOT NULL
ORDER BY type DESC
COMPUTE AVG(price), SUM(advance) BY type
COMPUTE SUM(price), SUM(advance)
"title", "type", "price", "advance"
"Onions, Leeks, and G", "trad_cook ", 20.9500, 7000.0000
"Fifty Years in Bucki", "trad_cook ", 11.9500, 4000.0000
"Sushi, Anyone? ", "trad_cook ", 14.9900, 8000.0000
3 rows fetched from 4 columns.
"AVG", "SUM"
15.9633, 19000.0000
1 row fetched from 2 columns.
"title", "type", "price", "advance"
"Computer Phobic AND ", "psychology ", 21.5900, 7000.0000
"Is Anger the Enemy? ", "psychology ", 10.9500, 2275.0000
"Life Without Fear ", "psychology ", 7.0000, 6000.0000
"Prolonged Data Depri", "psychology ", 19.9900, 2000.0000
"Emotional Security: ", "psychology ", 7.9900, 4000.0000
5 rows fetched from 4 columns.
"AVG", "SUM"
13.5040, 21275.0000
1 row fetched from 2 columns.
"title", "type", "price", "advance"
"But Is It User Frien", "popular_comp", 22.9500, 7000.0000
"Secrets of Silicon V", "popular_comp", 20.0000, 8000.0000
2 rows fetched from 4 columns.
"AVG", "SUM"
21.4750, 15000.0000
1 row fetched from 2 columns.
"title", "type", "price", "advance"
"Silicon Valley Gastr", "mod_cook ", 19.9900, .0000
"The Gourmet Microwav", "mod_cook ", 2.9900, 15000.0000
2 rows fetched from 4 columns.
"AVG", "SUM"
11.4900, 15000.0000
1 row fetched from 2 columns2 .
"title", "type", "price", "advance"
"The Busy Executive's", "business ", 19.9900, 5000.0000
"Cooking with Compute", "business ", 11.9500, 5000.0000
"You Can Combat Compu", "business ", 2.9900, 10125.0000
"Straight Talk About ", "business ", 19.9900, 5000.0000
4 rows fetched from 4 columns.
"AVG", "SUM"
13.7300, 25125.0000
1 row fetched from 2 columns.
"SUM", "SUM"
236.2600, 95400.0000
1 row fetched from 2 columns.
"Sushi, Anyone? ", "trad_cook ", 14.9900, 8000.0000
"Onions, Leeks, and G", "trad_cook ", 20.9500, 7000.0000
"Fifty Years in Bucki", "trad_cook ", 11.9500, 4000.0000
"Sushi, Anyone? ", "trad_cook ", 14.9900, 8000.0000
Keywords : kbinterop kbprg kbusage SSrvProg
Version : 2.5 2.65 4.2x 6.0 6.5
Platform : WINDOWS
Issue type :
Last Reviewed: April 2, 1999