I’m trying to select rows, where neither A nor B have an entry with more €:
This is an example of my data
Column A | Column B | € |
---|---|---|
A | B | 100 |
B | A | 100 |
C | B | 80 |
B | C | 80 |
D | E | 75 |
E | D | 75 |
And this is the desire output. Because B have a higher value in column € pair up C with B is not possible.
Column A | Column B | € |
---|---|---|
A | B | 100 |
B | A | 100 |
D | E | 75 |
E | D | 75 |
I tried rank() over (partition by...order by ...)
but did not get any results.
The real data ("ok" marks qualifying rows):
Origen --- Destino --- Dinero
2 -------- 13 ------- 822205 € ok
13 -------- 2 ------- 822205 € ok
32 -------- 13 ------- 794909 €
13 -------- 32 ------- 794909 €
27 -------- 32 ------- 774035 € ok
32 -------- 27 ------- 774035 € ok
2 -------- 32 ------- 740558 €
32 -------- 2 ------- 740558 €
30 -------- 13 ------- 717471 €
13 -------- 30 ------- 717471 €
27 -------- 13 ------- 703739 €
13 -------- 27 ------- 703739 €
17 -------- 13 ------- 695366 €
13 -------- 17 ------- 695366 €
30 -------- 27 ------- 694234 €
27 -------- 30 ------- 694234 €
2 -------- 27 ------- 686425 €
27 -------- 2 ------- 686425 €
17 -------- 30 ------- 663328 € ok
30 -------- 17 ------- 663328 € ok
2
Answers
This only returns rows where no other entry exists that has the same
a
orb
and a highereur
:But for what you seem to need, traverse the table top down (sorted by
dinero
). Every pair of rows after the first pair potentially depends on previous picks. Not easily implemented with pure SQL (set-based logic). You need a procedural solution.The following SQL produces the desired output:
The query determines the greater of the maximum amount for each member of a pair and then filters the rows by selecting only those where
amount
equals the maximum associated with both members.