SQL Recipes
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
  #1 (permalink)  
Old 04-20-2006, 01:08 AM
ben ben is offline
Administrator
 
Join Date: Mar 2007
Posts: 93
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:
Code:
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 Gmane -- Mail To News And Back Again
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, 01:15 AM
ben ben is offline
Administrator
 
Join Date: Mar 2007
Posts: 93
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-08-2006, 12:44 AM
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!
Reply With Quote
  #4 (permalink)  
Old 05-11-2006, 08: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!
Reply With Quote
  #5 (permalink)  
Old 03-29-2007, 05: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!
Reply With Quote
  #6 (permalink)  
Old 12-04-2007, 08: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!
Reply With Quote
  #7 (permalink)  
Old 06-18-2008, 09:31 AM
Robert from Czech
 
Posts: n/a
Default ANY answer. Re: Find the minimum or maximum value across several columns?

One math solution:

For Min try:
SELECT
Min((Col1 + Col2 - Col1 * SIGN( Col1 - Col2 ) - Col2 * SIGN( Col2 - Col1 ) ) DIV 2) AS MinVal
FROM table_name;
For Max try:
SELECT
Max((Col1 + Col2 - Col1 * SIGN( Col2 - Col1 ) - Col2 * SIGN( Col1 - Col2 ) ) DIV 2) AS MaxVal
FROM table_name;
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #8 (permalink)  
Old 08-13-2008, 08:12 PM
Unregistered
 
Posts: n/a
Lightbulb ANY answer. Re: Find the minimum or maximum value across several columns?

case
when Col1 < Col2 then Col1
else Col2
end as NewCol
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not 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 02:44 AM
Find the maximum value of a column and return the corresponding id? ben SQL - Questions and Answers 2 04-28-2006 05:27 AM


All times are GMT. The time now is 05:23 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-2008 SQL Recipes

1 2 3 4 5 6 7 8