I am writing software where I have a box size (width=10, height=20, length=10) and a Postgres database with shelves (also defined by width, height, length). Now I want to find all shelves where I can put a specific box (the shelf dimensions need to be greater or equal to the box). I was originally thinking that I could do this:
SELECT shelves.* WHERE shelves.width = 10 AND shelves.height = 20 AND shelves.length = 10
However, my box can fit in any of these shelves, because it can just be rotated to fit:
|------|---------|----------|----------|
| id | width | height | length |
|------|---------|----------|----------|
| 1 | 20 | 10 | 10 |
|------|---------|----------|----------|
| 2 | 10 | 20 | 10 |
|------|---------|----------|----------|
| 2 | 10 | 10 | 20 |
|------|---------|----------|----------|
So, in JavaScript, this code would look like this:
const boxdim = [box.width, box.height, box.length].sort();
const shelfdim = [shelf.width, shelf.height, shelf.length].sort();
const canFit = boxdim[0] <= shelfdim[0] && boxdim[1] <= shelfdim[1] && boxdim[2] <= shelfdim[2];
Is is possible to do this in Postgres? I would need something that looks like this (which is obviously pseudo code and doesn't work:
SELECT shelves.* WHERE SORT(shelves.width, shelves.height, shelves.length)[0] = 10 AND SORT(shelves.width, shelves.height, shelves.length)[1] = 10 AND SORT(shelves.width, shelves.height, shelves.length)[2] = 20
Any tips appreciated.