Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

sql server join same table

This is my Rows in a table :

Id    |       FromDate            |       ToDate             | prod_Id
------|---------------------------|--------------------------|-------
1     |   2012-08-13 07:00:00.000 |  2012-08-14 18:59:00.000 |    10
1     |   2012-08-13 07:00:00.000 |  2012-08-15 18:59:00.000 |    10
1     |   2012-08-13 07:00:00.000 |  2012-08-16 18:59:00.000 |    10 

I would like a result like below:

Id  |   FromDate                  |        ToDate             | prod_Id
----|-----------------------------|---------------------------|---------
1   |    2012-08-13 07:00:00.000  |   2012-08-16 18:59:00.000 |   10 

How to do it?

like image 261
Uday Avatar asked Feb 19 '23 09:02

Uday


1 Answers

 SELECT Id, MIN(FromDate), MAX(ToDate), prod_Id
 FROM TbName
 GROUP BY Id, prod_Id
like image 69
Patrick Guimalan Avatar answered Feb 25 '23 11:02

Patrick Guimalan