Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

select aggregate function and all other columns

How do I select all columns in a table and an aggregate function in a convenient way?

I.e. say that I have a table with 100 columns, and I want to send the following

SELECT Max(Columns 44), ALL OTHER COLUMNS
FROM zz
Group by ALL OTHER COLUMNS 

Thanks!

like image 688
D.Loo Avatar asked Sep 21 '16 09:09

D.Loo


1 Answers

To select all columns from the table is:

select * from zz;

To select a maximum from the table is

select max(column44) from zz;

The two combined:

select zz.*, (select max(column44) from zz) as maxcol44
from zz;

If you want to omit column44 in your result rows and only have maxcol44, then you must list the columns:

select 
  column1, 
  column2, 
  ...
  column43, 
  (select max(column44) from zz) as maxcol44,
  column45, 
  ...
from zz;
like image 127
Thorsten Kettner Avatar answered Sep 21 '22 20:09

Thorsten Kettner