473,769 Members | 7,923 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

ORDERING by a FUNCTION() with simple-integer

An ORDER BY a simple-integer inside a FUNCTION, results in SQL0440N,
unless the FUNCTION expects an INTEGER as its parameter. For example:

DECLARE GLOBAL TEMPORARY TABLE A(A CHAR(1))
INSERT INTO SESSION.A VALUES ('a'), ('b')
CREATE FUNCTION A(A char(1)) RETURNS char(1) DETERMINISTIC NO EXTERNAL
ACTION RETURN A
SELECT A FROM SESSION.A ORDER BY A(1)
DROP FUNCTION A
DROP TABLE SESSION.A

Is there a way to pass a simple-integer inside a FUNCTION of an ORDER
BY clause?

B.

Dec 12 '05
20 2501
>You will find that the numeric value is a "simple-integer" only. When you
use the function, you are automatically in the branch "sort-key-expression"
and that doesn't handle simple-integers at all.
OK, i see it, thanx. I didn't realize db2 limited itself like that.

Basically, a FUNCTION cannot be used on a ORDER BY in a UNION query.
I'd have to CREATE a VIEW on the UNION first.
No, it doesn't have to.


I see. You are correct.

My misconception was based on when i made a simple FUNCTION:

CREATE FUNCTION A(A char(1)) RETURNS char(1) RETURN A

And got:

SQL0583N The use of routine "<schema>.A " is invalid because it is not
deterministic or has an external action. SQLSTATE=42845

So i assumed DETRMINISTIC was required.

B.

Dec 13 '05 #11
I like it, because it works well with UNIONs. In fact, that is
basically the only time i use it.

BTW, i think the worst decision was the JOIN of the FROM clause. Every
other part of SQL is intuitive to me. The join clause just seems
backwards. Probably because it is goal-oriented (the TABLEs are being
joined in the end) whereas i see SQL as process-oriented (the WHERE
clauses decided how the join is done).

B.

Dec 13 '05 #12
>Actually, it is quite obvious:

I appreciate the example, and i will bear it in mind should i ever need
to use it. Though, i still find it confusing. As in a "this is not
needed, why would i need to do this backwards thing" sort of way.
You can simply give the function in the ORDER BY the parameter that it
expects, i.e. values of type CHAR(1) that are stored in the column:


The actual case is a UNION query from two different TABLEs, so that is
not possible.

B.

Dec 13 '05 #13
Brian Tkatch wrote:
You will find that the numeric value is a "simple-integer" only. When you
use the function, you are automatically in the branch
"sort-key-expression" and that doesn't handle simple-integers at all.


OK, i see it, thanx. I didn't realize db2 limited itself like that.

Basically, a FUNCTION cannot be used on a ORDER BY in a UNION query.
I'd have to CREATE a VIEW on the UNION first.


Simple nest the union into a sub-select as this gives you the opportunity to
assign new column names:

SELECT ...
FROM ( SELECT ... FROM ...
UNION
SELECT ... FROM ... ) AS t(col1, col2, ...)
ORDER BY fnc(colX)

--
Knut Stolze
DB2 Information Integration Development
IBM Germany
Dec 13 '05 #14
Brian Tkatch wrote:
I like it, because it works well with UNIONs. In fact, that is
basically the only time i use it.

BTW, i think the worst decision was the JOIN of the FROM clause. Every
other part of SQL is intuitive to me. The join clause just seems
backwards. Probably because it is goal-oriented (the TABLEs are being
joined in the end) whereas i see SQL as process-oriented (the WHERE
clauses decided how the join is done).


A join condition describes how the join is to be done. And the join is done
in the FROM clause. I was taught that the join condition should always be
in the FROM clause as this is the place where it belongs. The WHERE clause
is just for restricting the rows from the (joined) table. This is a
different thing than the join condition, and both should not be mixed.

On the side: from an implementation perspective it is much easier if the
join condition is in the from clause as you can push-down the predicate(s)
right away. If the join conditions are in the WHERE clause, you have a
much harder time figuring this out. That's what I learned when we
implemented our own database system at school. Granted, DB2 uses a
cost-based optimizer so things are quite different anyways.

--
Knut Stolze
DB2 Information Integration Development
IBM Germany
Dec 13 '05 #15
>A join condition describes how the join is to be done. And the join is done
in the FROM clause. I was taught that the join condition should always be
in the FROM clause as this is the place where it belongs. The WHERE clause
is just for restricting the rows from the (joined) table. This is a
different thing than the join condition, and both should not be mixed.
Ah, but the FROM clause itself is what joins the TABLE, albeit
Cartesian. Everything past that *is* a restriction, to only get useful
records.
On the side: from an implementation perspective it is much easier if the
join condition is in the from clause as you can push-down the predicate(s)
right away. If the join conditions are in the WHERE clause, you have a
much harder time figuring this out.


That makes a lot of sense.

I like WHERE clauses though for readability as well. Then again, i
have little experience with the join clause anyway, so i see them as a
jumble, as opposed to recognizing a familiar friend.

B.

Dec 13 '05 #16
Thanx for the reply!

I still think it's backwards, but at least its doable.

B.

Dec 13 '05 #17
Brian Tkatch wrote:
A join condition describes how the join is to be done. And the join is
done
in the FROM clause. I was taught that the join condition should always be
in the FROM clause as this is the place where it belongs. The WHERE
clause
is just for restricting the rows from the (joined) table. This is a
different thing than the join condition, and both should not be mixed.


Ah, but the FROM clause itself is what joins the TABLE, albeit
Cartesian. Everything past that *is* a restriction, to only get useful
records.


Well, inner joins with a join condition don't give the cartesian product of
all rows that needs to be further restricted. Instead, they already
produce the "right" join. Semantically, both things give the same result.
But conceptually, we're talking apples and oranges here!

--
Knut Stolze
DB2 Information Integration Development
IBM Germany
Dec 13 '05 #18
>> ...which is why databases added a clause called ORDER BY. So that ordering is possible. <<

NO. That is why the ORDER BY clause is defined for cursors, never on
SQL statements. Cursors are for getting Relational data out of sets
and into host (i.e. non-SQL, procedural) languages)
Being I want it ORDER BY-ed the output of the FUNCTION, it makes a great deal of sense. <<


Not in RDBMS and Standard SQL! Get **any** book on the basics of RDBMS
and read it. You have missed the foundations and are still thinking
in terms of sequential file structure. Learn to think in terms of sets
and not sequences.

Dec 13 '05 #19
Brian Tkatch wrote:
The actual case is a UNION query from two different TABLEs, so that is
not possible.

Sorry for coming late to the party....
ORDER BY in SQL is attached to SELECT.
UNION (ALL) is not a SELECT.
Ironically we had to actively "disable" ORDER BY on UNION to adhere to
these rules. DB2 was originally happily eating it.
The solution of pushing the UNION into a derived table has been
provided, so no harm is done.
I agree with Knut that the decision to standardize ORDER BY with
constant numbers to mean column positions was a bad idea. It originated
from the limitatiosn of products at the time to match the expression in
teh select list with an an identical expressions in the ORDER BY clause.
Using correlation names from the select list in the order by clause
would have been a much cleaner solution.. too late.

Obviosuly ORDER BY in to sort the result set of a cursor is a good idea.
Also obviosuly ORDER BY in an OLAP expression (which may benested inside
a query) is a useful thing.
So what about a nested ORDER BY?
Well Knut as brought up one important point: Orthogonality.
E.g. if a tool like "Query Patroller" needs to take a user submitted
query and store it away for later retrieval of teh result set having an
orthoginal language is beneficial. So for reasons of orthogonality alone
ORDER BY in subquery was deemed necessary.
But there is another usage:
Combination of ORDER BY with FETCH FIRST (aka. TOP/LIMIT).
What this gives us is a "truncating " select operator. Truncating based
on an ORDER is nothing bad.
The trick is that the ORDER does NOT leak. The derived table is NOT ordered.
There is abolsutley nothing wrong with that from a relational point of view.
Arguing that such things are evil is like arguing that any other scalar
operation thnt PLUS and MINUS are evil. There are logical extensions
with new operators which one can make without breaking the model: MULT,
DIV, MOD, .... same with relational.

Cheers
Serge
--
Serge Rielau
DB2 SQL Compiler Development
IBM Toronto Lab
Dec 13 '05 #20

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
2248
by: Jeff Thies | last post by:
I have 10 select boxes, they have select values of 1 to 10 (one of each value). I'd like to be able to change one of the select boxes and have the rest reorder themselves accordingly. I thought this would be simple, as the values of the other select boxes are either incremented up (if going from a smaller value to a larger) or incremented down over the range of the old value to (and including) the new value.
15
1748
by: Kay Schluehr | last post by:
Here might be an interesting puzzle for people who like sorting algorithms ( and no I'm not a student anymore and the problem is not a students 'homework' but a particular question associated with a computer algebra system in Python I'm currently developing in my sparetime ). For motivation lets define some expression class first: class Expr: def __init__(self, name=""):
6
6488
by: Brendan.Collins | last post by:
Hi I have a javascript problem that has been annoying me for two days now and thought that a javascript expert might have the magic solution. I am populating a table dynamically from the database and I am trying to allow the user to order the rows in the table using up and down arrows. The function I am using is:
2
2564
by: D. Dante Lorenso | last post by:
First I created a function that selected the next available pin code from a table of pre-defined pin codes: CREATE FUNCTION "public"."get_next_pin_code" () RETURNS varchar AS' DECLARE my_pin_code VARCHAR; BEGIN ... /* this is the pincode we just fetched */ RETURN (my_pin_code);
9
1630
by: Project2501a | last post by:
hey guys, Question about the internal workings of Access. How are controls added in the Form.Controls collection? I mean, in which order? the order place them on the form? is there a way to re-arrange them as i please? My solution is to remove the objects from the Form.Controls collection, place them in a tmpCollection, arrange tmpCollection as I please, and then re-add them back into the Form.Controls collection. thanks
3
1937
by: Ryan | last post by:
My project uses the /ORDER specifier to order functions as specified in file containing the decorated names of packaged functions (COMDATs). I am in the process of upgrading from VC6 to VC.NET 2003. When linking a release build of my project VC.NET 2003, the following warning occurs for each function that is being ordered: SUTL_X86OrderComdats.dat : warning LNK4065: '?SUTL_SomeFunction@@YAXXZ' cannot be ordered; ignored The help for...
3
1440
by: marc | last post by:
I want to create a strongly typed collection and use DictionaryBase I add a number of objects but when i loop through the list the data is ordered in a strange maner. How is this possible? Can anybody tell my why the order gets changed? Thanks Marc '***************** OUTPUT **************************
23
11322
by: illegal.prime | last post by:
Hi all, is there a key value collection in .Net that maintains the ordering in which I add items. I'm finding that I'm creating a number of classes that contain a name and then some object. I would prefer just to use some collection that maintains the ordering in which I add things (like ArrayList), but that maps a key to a value. I thought NameValueCollection was the right one - but it doesn't guarantee ordering.
36
2086
by: dspfun | last post by:
Hi! Is there any way in C that one can guarantee that instructions are executed in the same order as they are written in the program? For example, say I have the following: instruction1; instruction2; instruction3;
20
2466
by: Mr.SpOOn | last post by:
Hi, I need a structure to represent a set of integers. I also need to perform on this set some basic set operations, such as adding or removing elements, joining with other sets and checking for the presence of specific elements. I think that using Python sets would be the best choice, but I also need integers to be ordered inside the set and I've just found out that, instead, Python sets are unordered collections.
0
9589
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10049
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9997
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9865
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8873
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7413
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6675
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5310
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3565
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.