Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 Transact-SQL (2000)
 Min/Max/Avg for each column

Author  Topic 

alexc
Starting Member

10 Posts

Posted - 2002-09-30 : 18:25:49
Here's the problem:

I have a table that includes the following columns:

Row_ID Col1 Col2 Col3
-------- -------- -------- --------
1 5 7 2
2 10 14 4
3 15 21 6

I would like to return the Min/Max/Avg for each column like this:

Column Min Max Avg
-------- ------- ------- --------

Col1 5 15 10
Col2 7 21 14
Col3 2 6 4


Is there any way to do this? I would prefer not to have to specify the column names and I don't care if all columns are returned.

Thanks for your help

robvolk
Most Valuable Yak

15732 Posts

Posted - 2002-09-30 : 19:41:13
You could possibly do this using the INFORMATION_SCHEMA.COLUMNS view to get the column names, and then build a SQL command string with those names, and then EXECUTE it, but the problem is that you'll have to tell the procedure which columns NOT to include (char/varchar, dates, bits, binary, etc.)

If I may ask, why are you storing the data in columns like that? Are they representing the same kind of thing (Quantity1, Quantity2, Quantity3 etc.) or are they completely different attributes? The kind of thing you're requesting doesn't make a lot of sense without more background into the structure of the table being queried.

Go to Top of Page

MichaelP
Jedi Yak

2489 Posts

Posted - 2002-09-30 : 19:49:49
I worked at this for a bit, and I think you are going to have to come up with a dynamic SQL solution if you want to do ti withotu specifing the column names.

Column 1, 2, and 3 are values abotu the same entity right?
I think if your data was stored a bit differently, this could be done.

If Col1, 2, and 3 were "child records" of parent "Row_ID" this could be done. It's possible that you can move your data into that format in a temp table, and then run your MIN MAX AVG on those.

Michael

EDIT: SNIPED!

<Yoda>Use the Search page you must. Find the answer you will.</Yoda>

Edited by - michaelp on 09/30/2002 19:50:25
Go to Top of Page
   

- Advertisement -