Tilted Forum Project Discussion Community  

Go Back   Tilted Forum Project Discussion Community > Interests > Tilted Technology


 
 
LinkBack Thread Tools
Old 08-31-2004, 10:54 AM   #1 (permalink)
Banned from being Banned
 
Location: Donkey
SQL Server: COMPUTE clause... pointless?

I'm selecting a result set from SQL Server from a dynamically built stored proc that has n columns (created by a function during runtime).

I need to sum up the data from specific numeric columns, so I'm using the COMPUTE clause which returns a separate set of data w/ all the summed up columns.

The problem is... Microsoft's developers are seriously retarded. Why? Because I found out from various Google searches that you CAN NOT alias the columns returned by COMPUTE!! This, essentially, renders any COMPUTE uses other than a BASIC "select * from ... " query totally useless.

Why?

It names all your fields "sum" if doing a sum ("avg" if doing averages, etc..). For this one set of data, I have 15 columns returned in this compute, all are named 'sum'. SQL Server complains if you try to alias these fields.

This wouldn't be a problem if I made my columns static, but they aren't... and the result set has to now be accessed via column indexes, which is impossible.

The stored proc I'm calling from is dynamic in that there are n amount of columns based on user defined data. On each of those nth field numeric columns, I need to have a compute clause ran. However, where my columns are called things like "Sales" or "Downloads" (defined by user), there's no way I can reference their counter part in the compute by without writing a function to strip away all non-numeric fields and keep an array of which name has what index in the computed result set. I'd like to name the COMPUTE result set for a "Sales" column "TotalSales" .. and for a "Download" column "TotalDownload".. that way when I loop through the result set (since I don't know how many fields it contains), I can reference a specific totals column like

...
int curTotal = row[i]["Total" + column].Value;
...

All of this is insanely ridiculous. There's nothing in the help files about aliasing these tables, and Like I said, even reading through various posts on google groups has revealed that you can't alias these columns (not to mention that trying to do a google search on "sql server compute clause alias" returns pretty much nothing CLOSE to what I'm trying to find out).

An excerpt from a summary about COMPUTE:

Quote:
The COMPUTE clause cannot be used with INTO and cannot contain aliases
for column names, although aliases can be used in the select_list.
The COMPUTE keyword can be used without BY to generate grand totals,
grand counts, and so on. The ORDER BY clause is optional only if you
use the COMPUTE keyword without BY.
Sorry, but that's crazy. There's absolutely no reason not to let us alias those columns.

Short of selecting everything from the initial set into a temp table, then immediately calling a "select * from #temp" (to get my first result set) and "select sum(col1), Sum(col2)... from #temp", is there any way to do this?

Wow, I didn't think it would be hard to do something so common...
__________________
I love lamp.

Last edited by Stompy; 08-31-2004 at 11:01 AM..
Stompy is offline  
Old 09-03-2004, 06:40 PM   #2 (permalink)
Tilted
 
COMPUTE is for backwards compatibility. Perhaps ROLLUP will do what you want? Instead of returning the data in a seperate resultset, it'll be part of the normal resultset -- you can identify the ROLLUP rows by checking GROUPING(<field you're grouping by>) = 1. ROLLUP will only work as long as you're not wanting to group any calculations -- it doesn't like that too much, and if that's your intention, you're probably just best off writing your own extra query to do so.

Here's an example from the Northwind db:

Code:
SELECT CASE WHEN GROUPING(CustomerID) = 1 THEN 'ALL' ELSE CustomerID END AS CustomerID, CASE WHEN GROUPING(LastName) = 1 THEN 'ALL' ELSE LastName END AS Employee, COUNT(*)
FROM Orders
INNER JOIN Employees ON Orders.EmployeeID = Employees.EmployeeID
GROUP BY CustomerID, LastName WITH ROLLUP
magua is offline  
 

Tags
clause, compute, pointless, server, sql


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -8. The time now is 04:07 PM.

Tilted Forum Project

Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
Search Engine Optimization by vBSEO 3.6.0 PL2
© 2002-2012 Tilted Forum Project

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54