Within a table containing the data_inicio
, data_fim
, hora_inicio
, hora_fim
columns, you need to identify the following occurrences within a recordset: the lower start date, the higher end date, the start time lower and the end time greater.
The image below lists a set of records in which I want to check the smaller start_date, larger_del_fim, shorter start_time, longer_file:
Basedontherecordsshownintheimageabove,theshorteststartdateissimpletoachieve,simplybyusingtheMINoperator.Thesamegoesforgettingthehighestenddate.IjustneedtousetheMAXoperatortogetit.TocalculatethehighestinitialtimeandthelowestendtimeIcannotusethesameapproachascanbeseeninthesqlresultbelow.
SQL:
selectmin(data_inicio),max(data_fim),min(hora_inicio),max(hora_fim)fromminha_tabelawherecodigo=2
Result:
To calculate, both the smallest start time and the highest end time need to take more than just the column itself into account. Simply resorting to the MIN and MAX operator will not resolve it. I need to take into account also the date columns. Looking at the first image we can realize that the smallest start time is' 1000 'because the smallest date is '04 / 05'. The highest end time is' 1900 ', because the highest end date is '20 / 05'.
Is there a way to implement this type of query simply through SQL? If so, how?
This example is in the SQL Fiddle SQL Fiddle .