Question: I have a table with a list of folders and I want to return only the top level folders.

For example, if the original table (tblFolders) has a single column (Fldrs) as below:

**Fldrs**

```
C:\Folder1
C:\Folder1\Subfolder1
C:\Folder1\Subfolder2
C:\Folder2\Subfolder1

```

I'd like to return:

```
C:\Folder1
C:\Folder2\Subfolder1

```

Thanks in advance

Answer: 
A standard SQL solution is to UNION all your dates

```
SELECT MyID, MAX(thedate ) AS maxdate, MIN(thedate ) AS mindate
FROM
(
SELECT MyID, Date1 AS thedate FROM table
UNION ALL
SELECT MyID, Date2 AS thedate FROM table
UNION ALL 
SELECT MyID, Date3 AS thedate FROM table
UNION ALL
SELECT MyID, Date4 AS thedate FROM table
) T
GROUP BY MyID

```

There might be better other solutions with window functions, depending on your RDBMS, which you haven't specified. But this one should work with any RDBMS.
Most, but not all, databases actually support `least()` and `greatest()`:

```
select t.*,
       least(date1, date2, date3, date4) as min_date,
       greatest(date1, date2, date3, date4) as max_date
from t;

```

The only caveat is that the return value is `NULL` if *any* of the values are `NULL`. That doesn't seem to be an issue based on the example data in your question.

EDIT:

On SQL Server, you would unpivot and use `apply`:

```
select t.*, v.*
from t cross apply
     (select max(dte) as max_date, min(dte) as min_date
      from (values (t.date1), (t.date2), (t.date3), (t.date4)) v(dte)
     ) v

```