alex
alex

Reputation: 7905

Aggregate function along other records

Let's say I am going to fetch all records of table T1. Also, I need to have access to min and max date value of records.

I may do that using two queries:

select * from T1 ;
select min(created_at) as min_date,max(created_at) as max_date from T1;

This is two separate queries , but is it possible to have them in one query?
I mean all records plus min and max value of a specific column.

Upvotes: 2

Views: 49

Answers (2)

Tamil
Tamil

Reputation: 1203

SELECT * FROM `T1` JOIN (SELECT MIN(`created_at`) AS min_date,
MAX(`created_at`) AS max_date FROM `T1` ) AS temp

Upvotes: 0

juergen d
juergen d

Reputation: 204854

select *,
       (select min(created_at) from T1) as min_date,
       (select max(created_at) from T1) as max_date
from T1;

Upvotes: 3

Related Questions