Let's suggest I have the following table of rates:
ID | baseAsset | quoteAsset
-------+--------------+-----------------
1 | BTC | USDT
2 | USDT | BTC
3 | LUNA | ETH
4 | ETH | LUNA
5 | USD | BTC
The problem is that I have no idea how to get rates and exclude those rates that have reverse pair. E.g. in my case I want to get something like this:
ID | baseAsset | quoteAsset
-------+--------------+-----------------
1 | BTC | USDT
3 | LUNA | ETH
5 | USD | BTC
Now there is only BTC/USDT
(without USDT/BTC
). So, the query should take only first pair (no matter BTC/USDT
, or LUNA/ETH
, or another one) and find reverse pair to exclude. If there should be additional column (like createdAt
or updatedAt
) — correct me and let me know if my question still is not clear, please.
In SQL you'd use
LEAST
andGREATEST
for this, provided your DBMS supports these functions:In standard SQL you can replace above
LEAST
byand above
GREATEST
byAnother simple approach is to use
NOT EXISTS
: