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 05:37 AM.

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 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360