You're absolutely right to think about filtering the recursive CTE output to get closer to your desired result, but there are a few nuances to consider. Let's analyze this approach and see how we can adapt the recursive CTE to produce the desired output:
All combinations of size 2 or more from the transaction 231574 (which contains products 111, 222, 444).
The combinations are formatted as comma-separated strings with quotes around each product ID (e.g., "111","222").
Current Recursive CTE Behavior
The current recursive CTE builds the full "basket" of products for each transaction, step by step, producing:
code
combination
111
111, 222
111, 222, 444
This output includes:
Single products (111), which we don't want (since we need combinations of at least 2 products).
Intermediate combinations (111, 222), which are useful but not in the desired format.
The full combination (111, 222, 444), which is useful but not in the desired format.
Can We Filter the Recursive CTE Output?
Filtering the recursive CTE output directly won't give us all the desired combinations because the recursive CTE is designed to build the full basket incrementally, not to generate all possible sub-combinations (e.g., "111","444" is not naturally produced by the recursion). However, we can modify the query to:
Use the recursive CTE to identify transactions with at least 2 products and build the full basket.
Then, in a separate step, generate all possible sub-combinations of size 2 or more from the full basket.
Unfortunately, generating all sub-combinations (e.g., pairs, triplets, etc.) from the full basket without array functions is not straightforward in SQL. The recursive CTE approach is great for building the full basket but not for generating all subsets.
Alternative Approach: Use Self-Joins to Generate Combinations
To achieve your exact desired output without array functions, we need to explicitly generate all combinations of size 2 or more using self-joins. Here's how we can do it:
sql
WITH transaction_products AS (
-- Filter to transactions with at least 2 products
SELECT transaction_id, product_id
FROM transactions
WHERE transaction_id IN (
SELECT transaction_id
FROM transactions
GROUP BY transaction_id
HAVING COUNT(DISTINCT product_id) >= 2
)
),
pairs AS (
-- Generate all pairs (combinations of size 2)
SELECT DISTINCT
t1.transaction_id,
-- Ensure consistent ordering (e.g., "111, 444" not "444, 111")
CASE
WHEN t1.product_id < t2.product_id
THEN CONCAT('"', t1.product_id, '","', t2.product_id, '"')
ELSE CONCAT('"', t2.product_id, '","', t1.product_id, '"')
END AS combination
FROM transaction_products t1
JOIN transaction_products t2
ON t1.transaction_id = t2.transaction_id
AND t1.product_id < t2.product_id -- Avoid duplicates (e.g., "111, 222" and "222, 111")
),
triplets AS (
-- Generate all triplets (combinations of size 3)
SELECT DISTINCT
t1.transaction_id,
-- Ensure consistent ordering
CONCAT('"', t1.product_id, '","', t2.product_id, '","', t3.product_id, '"') AS combination
FROM transaction_products t1
JOIN transaction_products t2
ON t1.transaction_id = t2.transaction_id
AND t1.product_id < t2.product_id
JOIN transaction_products t3
ON t1.transaction_id = t3.transaction_id
AND t2.product_id < t3.product_id
)
-- Combine all combinations (pairs, triplets, etc.)
SELECT combination
FROM pairs
UNION
SELECT combination
FROM triplets
ORDER BY combination;
Explanation of the Above Query
transaction_products CTE:
Filters to only include transactions with at least 2 distinct products.
pairs CTE:
Generates all pairs of products within each transaction.
Uses t1.product_id < t2.product_id to avoid duplicate pairs (e.g., "111, 222" and "222, 111").
Formats the output with quotes and commas (e.g., "111","222").
triplets CTE:
Generates all triplets of products within each transaction.
Uses t1.product_id < t2.product_id < t3.product_id to ensure consistent ordering and avoid duplicates.
Formats the output with quotes and commas (e.g., "111","222","444").
Final SELECT:
Combines the pairs and triplets using UNION to remove duplicates.
This matches your desired output, except it also includes "111","222", which is logically correct (since it's a valid pair from transaction 231574). If you don't want "111","222", you would need to explain why it's excluded in your desired output.
Why Filtering the Recursive CTE Doesn't Work
If you try to filter the recursive CTE output to get only combinations of size 2 or more, you'll still miss some combinations. For example, the recursive CTE produces "111, 222, 444", but it doesn't naturally produce "111, 444" or "222, 444" because those are not part of the incremental build process. To get those, you would need a completely different recursive logic that generates all possible subsets, which is much more complex and not practical without array functions.
Final Note
If your database supports array functions (e.g., PostgreSQL's ARRAY_AGG), this problem becomes much simpler, as you can aggregate products into an array and then generate all possible combinations from that array. However, since you requested a solution without array functions, the self-join approach above is the most practical way to achieve your desired output.