SQL Recipes (Beta II)
A FREE cookbook for SQL queries and examples
Register FAQ Search Today's Posts Mark Forums Read

SQL - Questions and Answers Have a SQL question? Post it here. First do a search to see if someone hasn't already answered it.

Go Back   SQL Recipes a FREE cookbook of SQL queries and examples > SQL queries and examples > SQL - Questions and Answers

Reply
 
LinkBack Thread Tools Search this Thread Rating: Thread Rating: 1 votes, 5.00 average.
  #1 (permalink)  
Old 04-20-2006, 12:08 AM
ben ben is offline
Administrator
 
Join Date: Mar 2007
Posts: 77
ben has disabled reputation
Default ANY dialect question:

Find the minimum or maximum value across several columns?


Find the minimum or maximum value across several columns

Assume you have the following columns:

col1 col2
2 4
1 2
3 5

We want the answer to be 1 for minimum and 5 for maximum.

This question was asked on the postgres.sql mailing list http://article.gmane.org/gmane.comp.db.postgresql.sql/17243
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

ANSWER(S):

  #2 (permalink)  
Old 04-20-2006, 12:15 AM
ben ben is offline
Administrator
 
Join Date: Mar 2007
Posts: 77
ben has disabled reputation
Default Postgresql answer. Re: Find the minimum or maximum value across several columns?

Postgresql has two commands that come in handy, one is greatest() the other least()

SELECT max(greatest(col1, col2) FROM TABLE
SELECT min(least(col1, col2) FROM TABLE
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 05-07-2006, 11:44 PM
Rob137
 
Posts: n/a
Default T-SQL answer. Re: Find the minimum or maximum value across several columns?

OK, you asked for ANY answer, so this should work with most dialects:

IF EXISTS (SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'MyMinMaxTable')
DROP TABLE MyMinMaxTable
exec sp_dboption 'TelairDB', 'select into/bulkcopy', 'true'

CREATE TABLE [dbo].[MyMinMaxTable] (
[my_min] [decimal](16, 4) NULL ,
[my_max] [decimal](16, 4) NULL
) ON [PRIMARY]
GO

INSERT INTO MyMinMaxTable
SELECT min(ord_qty), max(ord_qty)
FROM AA_TestTable

INSERT INTO MyMinMaxTable
SELECT min(sitm_qty), max(sitm_qty)
FROM AA_TestTable

INSERT INTO MyMinMaxTable
SELECT min(this_qty), max(this_qty)
FROM AA_TestTable

INSERT INTO MyMinMaxTable
SELECT min(that_qty), max(that_qty)
FROM AA_TestTable

INSERT INTO MyMinMaxTable
SELECT min(other_qty), max(other_qty)
FROM AA_TestTable

SELECT min(my_min) AS abs_min, max(my_max) AS abs_max
FROM MyMinMaxTable
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Edit/Delete Message Reply With Quote
  #4 (permalink)  
Old 05-11-2006, 07:30 AM
Dimitar
 
Posts: n/a
Default ANY answer. Re: Find the minimum or maximum value across several columns?

How about this?
SELECT max(c) FROM
  (SELECT col1 AS c FROM t1
   UNION
   SELECT col2 AS c FROM t1) AS t2
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Edit/Delete Message Reply With Quote
  #5 (permalink)  
Old 03-29-2007, 04:42 PM
Vj
 
Posts: n/a
Default MS SQL answer. Re: Find the minimum or maximum value across several columns?

SELECT min(s.t) AS  minimum,max(s.t)  AS maximum FROM (SELECT col1 AS t FROM t1 union SELECT col2 AS t FROM t1) s
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Edit/Delete Message Reply With Quote
  #6 (permalink)  
Old 12-04-2007, 07:01 PM
Unregistered
 
Posts: n/a
Default ANY answer. Re: Find the minimum or maximum value across several columns?

Do yourself a favor - just use an insert trigger and do the evaluation in the trigger..

too expensive on select
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Edit/Delete Message Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

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

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
Find empty columns? Bufnita Ciumec SQL - Questions and Answers 0 03-06-2007 01:44 AM
Find the maximum value of a column and return the corresponding id? ben SQL - Questions and Answers 3 10-21-2006 08:30 PM


All times are GMT. The time now is 07:43 AM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0
Copyright (c) 2006-2007 SQL Recipes

1 2 3 4 5 6 7