sql - How to create a query to list all combinations possibilities - Stack Overflow

PHOTO EMBED

Mon Apr 08 2024 10:26:05 GMT+0000 (Coordinated Universal Time)

Saved by @flaviobrito #sql

SELECT
   CCY1, CCY2, CER.Exchange_Rate
FROM
    ( 
    SELECT
        c1.rec_id AS rec_id1, c1.currency AS CCY1,
        c2.rec_id AS rec_id2, c2.currency AS CCY2
    FROM
        currency c1
        CROSS JOIN --all combinations...
        currency c2
    WHERE
        c1.rec_id <> c2rec_id -- ...removes same pairs
    ) foo
    LEFT JOIN -- ...get matching FX pairs
    CurrencyExchangeRate CER ON foo.rec_id1 = cer.[from] AND foo.rec_id2 = cer.[to]
content_copyCOPY

https://stackoverflow.com/questions/2258735/how-to-create-a-query-to-list-all-combinations-possibilities