## Round 1 Playoff Schedule

N teams (N between 1 and 32) just finished the season and are all qualified for the playoff. If the number of teams were 2, 4, 8, 16, or 32 (powers of 2), the playoff schedule would be trivial: 1st team plays vs last team, 2nd – vs 2nd from the last, etc. However, there is no guarantee that the number of teams would be a power of 2. The challenge is to write a single SELECT statement that accepts the number of teams as a parameter and generates the round 1 pairings.

There should be 1, 2, 4, 8, or 16 teams (power of 2) in the 2nd round.

Expected Results:

For 6 teams:

```Game # Playoff Round 1 Pairings
------ ---------------------------
1 Team-3 vs Team-6
2 Team-4 vs Team-5
Team-1 advances to Round 2
Team-2 advances to Round 2
```

For 7 teams:

```Game # Playoff Round 1 Pairings
------ --------------------------
1 Team-2 vs Team-7
2 Team-3 vs Team-6
3 Team-4 vs Team-5
Team-1 advances to Round 2
```

For 8 teams:

```Game # Playoff Round 1 Pairings
------ ------------------------
1 Team-1 vs Team-8
2 Team-2 vs Team-7
3 Team-3 vs Team-6
4 Team-4 vs Team-5
```

For 1 team (no playoffs needed):

```Game # Playoff Round 1 Pairings
------ ---------------------------
Team-1 is a Champion!
```

For 11 teams:

```Game # Playoff Round 1 Pairings
------ ---------------------------
1 Team-6 vs Team-11
2 Team-7 vs Team-10
3 Team-8 vs Team-9
Team-1 advances to Round 2
Team-2 advances to Round 2
Team-3 advances to Round 2
Team-4 advances to Round 2
Team-5 advances to Round 2
```

You can use a substitution variable and run the query in SQL*Plus to test the results.

To submit your answer (one or more!) please start following this blog and add a comment to this post.

A correct answer (and workarounds!) will be published here in a week.

If you like this post, you may want to join my new Oracle group on Facebook: https://www.facebook.com/groups/sqlpatterns/

For more tricks and cool techniques check my book “Oracle SQL Tricks and Workarounds” for instructions.

Advertisements