IWETHEY v. 0.3.0 | TODO
1,095 registered users | 0 active users | 0 LpH | Statistics
Login | Create New User
IWETHEY Banner

Welcome to IWETHEY!

New group-by on calculated columns not direct
I cannot get Oracle to directly group-by on calculated columns, such as to_char() or truncate() on dates. My workaround is to have a nested query where the inner query does the needed calculation, but that is kind of ugly because it repeats most of the column names. Someone found out that it will take it if you don't alias the calculated column, but that leaves weird result set columns names. Oracle does not like equivalency very much it seems. This is not a show-stopper because of the double-select solution, but I wonder if there is not an easier way.
________________
oop.ismad.com
New The other way to do it...
...is to repeat the computation in the GROUP BY clause:
\nSELECT\n   mycalc = TO_CHAR(acolumn),\n   count(*)\nFROM mytable\nGROUP BY\n   TO_CHAR(acolumn)\n
New Another way to put it, is...
...the trick is, NOT to repeat the column alias in the GROUP BY clause.

The Critter's
\nSELECT\n  mycalc = TO_CHAR(acolumn),\n  count(*)\nFROM mytable\nGROUP BY\n  TO_CHAR(acolumn)\n

is (AFAICS?) equivalent to:
\nSELECT\n  TO_CHAR(acolumn) mycalc,\n  count(*) freq\nFROM mytable\nGROUP BY\n  TO_CHAR(acolumn)\n
, which is equivalent to:
\nSELECT\n  acolumnwithsomedamnlongstupidname myshortname,\n  count(*) freq\nFROM mytable\nGROUP BY\n  acolumnwithsomedamnlongstupidname\n
-- this works, whereas
\nSELECT\n  acolumnwithsomedamnlongstupidname myshortname,\n  count(*) freq\nFROM mytable\nGROUP BY\n  acolumnwithsomedamnlongstupidname myshortname\n
or
\nSELECT\n  acolumnwithsomedamnlongstupidname myshortname,\n  count(*) freq\nFROM mytable\nGROUP BY\n  myshortname\n
don't work, IIRC.

So, you could say that what makes it work or not, is that in the GROUP BY clause, you have to have "the original form" of what you are selecting -- whether that be "somedamnlongstupidcolumnname" or a calculation -- but NOT the alias you've specified for it in the SELECT clause.

(Though I must admit I'm not sure if this really holds for Critter's result_or_alias = calculation[_or_originalname?] form; I always use the originalname_or_calculation alias style, myself. But I think it's rather probable that this works the exact same way, because I'm guessing it is the exact same thing, only written differently.)

HTH!


   [link|mailto:MyUserId@MyISP.CountryCode|Christian R. Conrad]
(I live in Finland, and my e-mail in-box is at the Saunalahti company.)
Your lies are of Microsoftian Scale and boring to boot. Your 'depression' may be the closest you ever come to recognizing truth: you have no 'inferiority complex', you are inferior - and something inside you recognizes this. - [link|http://z.iwethey.org/forums/render/content/show?contentid=71575|Ashton Brown]
New Don't think that was the problem
He wasn't aliasing the column just to change the name, and grouping on the alias. He was actually naming a calculated value, and trying to group on that. There is no original column that has the value he wants to group on.
===

Implicitly condoning stupidity since 2001.
New Same thing; a calculation IS a "virtual column".
At least in this context: Bryce's problem was -- Chris thought, and I'll bet -- that *what he selected* in the first part of the select statement, the "column list" clause, he later -- in the Group By clause -- referred to by the alias he had defined for it, when he should have used "the original form". And it doesn't matter *what* exactly "what he selected" *is*; it can be a simple native column (aliased just for brevity/clarity), *or* a complex calculation involving gazillions of columns and googleplexes of function calls... To Oracle, it's all the same, as long as it *knows* it's the same thing you mean. (And it knows this if-and-only-if you refer to it in the same way you first *defined*, not alias-named, it.)

"It's the same"... As in, Oracle treats the result of the calculation exactly *as if it were* a column; to all intents and purposes, a "virtual column" -- that Oracle *does* know how to group on, original column or no -- has been born from his calculation. At least that's the smartest way for Bryce to think about it, because that allows him to see the obvious parallel with the simpler (this *is* for *Bryce's* benefit, after all!) case where there *is* an original column, that he is just renaming with an alias.

Now don't go protesting my paedagogical tricks -- at least, not until you recognise them for what they are -- any more, ya bastidge! :-)


[Edit:] And BTW, another way to put this -- i.e, all of the above -- would be, "What part of whether that be 'somedamnlongstupidcolumnname' or a calculation didn't you understand, DrooK?"


   [link|mailto:MyUserId@MyISP.CountryCode|Christian R. Conrad]
(I live in Finland, and my e-mail in-box is at the Saunalahti company.)
Your lies are of Microsoftian Scale and boring to boot. Your 'depression' may be the closest you ever come to recognizing truth: you have no 'inferiority complex', you are inferior - and something inside you recognizes this. - [link|http://z.iwethey.org/forums/render/content/show?contentid=71575|Ashton Brown]
Expand Edited by CRConrad Sept. 24, 2003, 05:58:54 PM EDT
New Ahh, ok
In the misty recesses of my memory I seem to recall seeing that if you deined the calculation twice in the same query it had to execute the query twice, and that aliasing it and referring to the alias was better. But now I'm thinking that involved HAVING clauses.

[Edit]: And BTW I was reading the code closely, the explanation not so much. So I missed that. Though since you didn't notice until you went back a third time, I'll say it wasn't that obvious anyway. :-P
===

Implicitly condoning stupidity since 2001.
Expand Edited by drewk Sept. 25, 2003, 12:02:00 PM EDT
New I got wrong alias format
Too much SQLServer, even though I'm working with Oracle at the moment. The form of "alias=value" is SQLServer specific, whereas the ANSI is "value alias". Actually i like the form where you can put the alias on the left hand side, since it conforms to most programming languages (excluding Beta).

Anyhow, I did verify the code on Oracle, I just didn't put the alias on it till I typed my message. Other than that, your explanation is much more thorough than mine. :-)
New Hmmmm. Thot I tried that
That works now in my simple test, but it complained in the more complicated orginal. My collegue also tried that I thought because the same thing worked without the alias at the top. As soon as he put an alias in the top, but nowhere else, it failed. Odd, futher investigation is warrented.....

Anyhow, thanx for the anti-sanity check.
________________
oop.ismad.com
New The proviso being....
...that the expression in the GROUP BY must exactly match the expression in the SELECT being grouped. Since some CASE and DECODE statements can get rather long-winded, it can be a pain to keep them in sync when you start tweaking the stuff.
New That is less of a pain if you build the query dynamically
Then you define the chunk in a variable and interpolate it in twice. No more need to manually synchronize.

Cheers,
Ben
"good ideas and bad code build communities, the other three combinations do not"
- [link|http://archives.real-time.com/pipermail/cocoon-devel/2000-October/003023.html|Stefano Mazzocchi]
New Or, if you do it manually, good ol' copy-and-paste.
New That would presume Dynamic SQL
In PL/SQL and T-SQL, variables can not be used to construct SQL on the fly without the performance overhead of dynamic recompilation.

I don't know if Tablizer is actually building these queries on the client or server, and whether he is using a programming language or some form based environment.
New I think Ben meant, and Bryce would use, a programming...
...language (or IDE environment) on the client.

In Ben's case, Perl (naturally!); in Bryce's, [ObCheapShot:] either "L" or "TOP", of course! :-)


   [link|mailto:MyUserId@MyISP.CountryCode|Christian R. Conrad]
(I live in Finland, and my e-mail in-box is at the Saunalahti company.)
Your lies are of Microsoftian Scale and boring to boot. Your 'depression' may be the closest you ever come to recognizing truth: you have no 'inferiority complex', you are inferior - and something inside you recognizes this. - [link|http://z.iwethey.org/forums/render/content/show?contentid=71575|Ashton Brown]
New Right, that or...
define some kind of "make world" where you build your views from templates and then load the output into the database.

For the record I like maintaining definitions of things in the database outside of it in a filesystem where you can use your standard revision control mechanisms to track things.

Cheers,
Ben
"good ideas and bad code build communities, the other three combinations do not"
- [link|http://archives.real-time.com/pipermail/cocoon-devel/2000-October/003023.html|Stefano Mazzocchi]
New Or, get rid of SQL
and replace it with a BS12-like alternative where you can reference similar stuff by name. SQL was chosen by IBM over BS12 because they thought managers would relate to its more English-like grammar. IOW, SQL is the COBOL of relational languages.

Anyhow, I still don't know why the simple one worked but the more complex one did not. If I get time, I will incrimentally strip the bigger one piece by piece until it works. So much for pair programming. (Or if I get too fat: *pear* programming.)
________________
oop.ismad.com
New update

It appears we could repeat the calculation over and over again, but we also refer to the calculated part in several parts of the WHERE clause. Thus, it is actually less wordy and better factored to have a double select instead, which allows one to use the alias.
________________
oop.ismad.com
New The other alternative is...
...to factor the calculation into a pl/sql function. Of course, you'd still have to have the call to the function, but it makes for more reusability in the long run.
New I asked for a view to be created, but was immed. rejected
Asking for a new function is not likely to get a better reception.
________________
oop.ismad.com
     group-by on calculated columns not direct - (tablizer) - (17)
         The other way to do it... - (ChrisR) - (16)
             Another way to put it, is... - (CRConrad) - (4)
                 Don't think that was the problem - (drewk) - (2)
                     Same thing; a calculation IS a "virtual column". - (CRConrad) - (1)
                         Ahh, ok - (drewk)
                 I got wrong alias format - (ChrisR)
             Hmmmm. Thot I tried that - (tablizer) - (10)
                 The proviso being.... - (ChrisR) - (9)
                     That is less of a pain if you build the query dynamically - (ben_tilly) - (8)
                         Or, if you do it manually, good ol' copy-and-paste. -NT - (CRConrad)
                         That would presume Dynamic SQL - (ChrisR) - (6)
                             I think Ben meant, and Bryce would use, a programming... - (CRConrad) - (5)
                                 Right, that or... - (ben_tilly) - (4)
                                     Or, get rid of SQL - (tablizer) - (3)
                                         update - (tablizer) - (2)
                                             The other alternative is... - (ChrisR) - (1)
                                                 I asked for a view to be created, but was immed. rejected - (tablizer)

Just a little peril?
217 ms