If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > DB2 > Scalar MIN and MAX for UDB?

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 11-23-04, 08:08
hurmavi hurmavi is offline
Registered User
 
Join Date: Jan 2004
Location: Europe, Finland, Helsinki
Posts: 60
Question Scalar MIN and MAX for UDB?

Hi!

DB2 z/OS V.7 has scalar funtions MIN and MAX*). Now DB2 UDB seems not to have those. So we have problem to port application.

Any ideas? These functions can be implemented by UDF, right? Are there any freeware or commercial packages?

Cheers, Bill



*) Normal column function use this syntax: MAX( ALL/ /DISTINCT expression).
Scalar function MAX has this syntax: MAX(expression, expression...).
Reply With Quote
  #2 (permalink)  
Old 11-23-04, 08:48
J Petruk J Petruk is offline
Registered User
 
Join Date: Mar 2004
Location: Toronto, ON, Canada
Posts: 513
Quote:
Originally Posted by hurmavi
Hi!

DB2 z/OS V.7 has scalar funtions MIN and MAX*). Now DB2 UDB seems not to have those. So we have problem to port application.

Any ideas? These functions can be implemented by UDF, right? Are there any freeware or commercial packages?

Cheers, Bill



*) Normal column function use this syntax: MAX( ALL/ /DISTINCT expression).
Scalar function MAX has this syntax: MAX(expression, expression...).
Can you use the CASE statement?

ie. SELECT CASE WHEN A > B THEN A ELSE B END
FROM MYTABLE;

Kind of ugly.
__________________
--
Jonathan Petruk
DB2 Database Consultant
Reply With Quote
  #3 (permalink)  
Old 11-23-04, 08:56
hurmavi hurmavi is offline
Registered User
 
Join Date: Jan 2004
Location: Europe, Finland, Helsinki
Posts: 60
OK!

I think, CASE would work, allright.

However, since we have more than two columns to choose, result can really be pretty much ugly, indeed. Thank you, anyway - we'll use it, if no better solution will be found.

Cheers, Bill
Reply With Quote
  #4 (permalink)  
Old 11-23-04, 09:13
J Petruk J Petruk is offline
Registered User
 
Join Date: Mar 2004
Location: Toronto, ON, Canada
Posts: 513
You could always bury it into a function:

CREATE FUNCTION SCHEMA1.NEWMAX
(VAL1 INTEGER, VAL2 INTEGER)
RETURNS INTEGER
LANGUAGE SQL
BEGIN ATOMIC
RETURN CASE WHEN VAL1 > VAL2 THEN VAL1
ELSE VAL2
END;
END @
__________________
--
Jonathan Petruk
DB2 Database Consultant
Reply With Quote
  #5 (permalink)  
Old 11-23-04, 10:00
Damian Ibbotson Damian Ibbotson is offline
Padawan
 
Join Date: Jun 2002
Location: UK
Posts: 525
... or hold your values in a CTE and use the column function.

Code:
with temptable (col1) as
(
 values  (1),(2),(0),(9)
)
select min(col1) from temptable
Reply With Quote
  #6 (permalink)  
Old 11-23-04, 11:41
Brett Kaiser Brett Kaiser is offline
Window Washer
 
Join Date: Nov 2002
Location: Jersey
Posts: 10,303
Hold the phone...are you saying V8 doesn't have MIN and MAX?
__________________
Brett
8-)

It's a Great Day for America everybody!

dbforums Yak CorralRadio 'Rita
dbForums Member List
I'm Good Once as I ever was

The physical order of data in a database has no meaning.
Reply With Quote
  #7 (permalink)  
Old 11-23-04, 11:47
J Petruk J Petruk is offline
Registered User
 
Join Date: Mar 2004
Location: Toronto, ON, Canada
Posts: 513
Quote:
Originally Posted by Brett Kaiser
Hold the phone...are you saying V8 doesn't have MIN and MAX?
It has MIN and MAX the column functions:
SELECT MAX(A) FROM MYTABLE;
(returns the biggest A)

But not the scalar function:
SELECT MAX(A,B) FROM MYTABLE;
(returns the larger of A or B for *each* columns)
__________________
--
Jonathan Petruk
DB2 Database Consultant
Reply With Quote
  #8 (permalink)  
Old 11-23-04, 11:55
Brett Kaiser Brett Kaiser is offline
Window Washer
 
Join Date: Nov 2002
Location: Jersey
Posts: 10,303
Well it's glad that I didn't know that, now that it's going away...

Because it not ANSI?

Code:
SELECT MAX(NAME,TSNAME) FROM SYSIBM.SYSTABLES;
Amazing...
__________________
Brett
8-)

It's a Great Day for America everybody!

dbforums Yak CorralRadio 'Rita
dbForums Member List
I'm Good Once as I ever was

The physical order of data in a database has no meaning.
Reply With Quote
  #9 (permalink)  
Old 11-23-04, 11:59
J Petruk J Petruk is offline
Registered User
 
Join Date: Mar 2004
Location: Toronto, ON, Canada
Posts: 513
Quote:
Originally Posted by Brett Kaiser
Well it's glad that I didn't know that, now that it's going away...

Because it not ANSI?

Code:
SELECT MAX(NAME,TSNAME) FROM SYSIBM.SYSTABLES;
Amazing...
I think it's sticking around in z/OS, but it's never existed that I can recall on Linux/Unix/Windows.
__________________
--
Jonathan Petruk
DB2 Database Consultant
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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 Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On