MaxMin Function within Select Statement SQL 2012 -
i having few issues making max function work within select statement see example data below:
table 1 table 2 visit_id car_id move_id visit_id movestartdate moveenddate 1 1 25/07/2016 27/07/2016 b 2 2 28/07/2016 28/07/2016 c 1 3 b 19/07/2016 22/07/2016 d 3 4 d 28/06/2016 30/06/2016
i select statement pick min start time , max start time based on visit_id expecting:
result visit_id car_id startdate enddate 1 25/07/2016 28/07/2016 b 2 19/07/2016 22/07/2016
so far have tried have inner joins in select statement:
,(max (enddate) visit.visit_id = move.visit_id) end date
i have looked @ other queries second select statement within select end like:
select visit_id, car_id ,(select max(enddate) full outer join table 2 on table 1.visit_id = table 2.visit_id group table 1.visit_id) end date
hope have provided enough info stumped.
if want car_id = 3 in result:
select t1.visit_id, t1.car_id, min(movestartdate), max(moveenddate) table1 t1 join table2 t2 on t1.visit_id = t2.visit_id group t1.visit_id, t1.car_id
returns:
sql>select t1.visit_id, t1.car_id, min(movestartdate), max(moveenddate) sql&from table1 t1 sql& join table2 t2 on t1.visit_id = t2.visit_id sql&group t1.visit_id, t1.car_id; visit_id car_id ======== =========== ==================== ==================== 1 25/07/2016 28/07/2016 b 2 19/07/2016 22/07/2016 d 3 28/06/2016 30/06/2016 3 rows found
Comments
Post a Comment