Skip to content

How to select the best item in each group?

I have table reports:

In human language: there are reports for each month. Each report could be in XML or CSV format. There could be 1-2 reports for each month in unique format.

I want to select the reports for all months, picking only 1 file for each month. The XML format is more preferable.

So, expected output is:

Explanation: the file jan.csv was excluded since there is more preferable report for that month: jan.xml.

Advertisement

Answer

As mentioned in the comments your data structure has a number of challenges. It really needs a column for ReportDate or something along those lines that is a date/datetime so you know which month the report belongs to. That would also give you something to sort by when you get your data back. Aside from those much needed improvements you can get the desired results from your sample data with something like this.

User contributions licensed under: CC BY-SA
4 People found this is helpful