I am trying to use the QUALIFY Clause in Snowflake since it does not support the use of DISTINCT ON as Postgres does. However, when I use the QUALIFY Clause in my query it returns different results from that in Postgres, and each time I run this snowflake for a result set of thousands of records it returns a different answer while if I run it for a few hundreds of records it provides the correct result each time. While in Postgres it returns the correct result each time regardless of the size of the result set. According to these pages:
Using `DISTINCT ON` in Snowflake
DISTINCT ON and QUALIFY should work in the same way, so I'm not exactly sure why I am having different results?
Snowflake:
SELECT wallet_transaction.walletId,
wallet_transaction.balance,
wallet_transaction.transactionDate,
wallet_transaction.parkerId,
wallet_transaction.operatorName,
wallet_transaction.operatorId,
wallet_transaction.offerName,
wallet_transaction.offerId
FROM wallet_transaction
WHERE {% condition snapshot_date %} wallet_transaction.transactionDate {% endcondition %}
QUALIFY ROW_NUMBER() OVER (PARTITION BY wallet_transaction.walletId
ORDER BY wallet_transaction.walletId, wallet_transaction.transactionDate DESC) = 1;
Postgres:
SELECT DISTINCT ON(wallet_transaction.walletId)
wallet_transaction.walletId,
wallet_transaction.balance,
wallet_transaction.transactionDate,
wallet_transaction.parkerId,
wallet_transaction.operatorName,
wallet_transaction.operatorId,
wallet_transaction.offerName,
wallet_transaction.offerId
FROM wallet_transaction
WHERE {% condition snapshot_date %} wallet_transaction.transactionDate {% endcondition %}
ORDER BY wallet_transaction.walletId,wallet_transaction.transactionDate DESC;