I am working with two tables, the first, purchases
, below (note, this is a clipping of the purchases
table:
| ID | Date | Value | Type | Satisfied By |
|:-------:|:---------:|:-----:|:----:|:------------:|
| SALE100 | 1/1/2019 | -5 | OUT | |
| SALE201 | 1/9/2019 | -10 | OUT | |
| SALE203 | 2/22/2019 | -1 | OUT | |
| SALE205 | 3/14/2019 | -1 | OUT | |
I am trying to determine which MAKE
items from another table, makes
, satisfies these sales.
| ID | Date | Value | Needed For |
|:-------:|:----------:|:-----:|:----------:|
| MAKE300 | 12/24/2018 | 5 | SALE100 |
| MAKE301 | 1/3/2019 | 3 | SALE201 |
| MAKE399 | 1/5/2019 | 5 | SALE201 |
| MAKE401 | 1/7/2019 | 3 | SALE201 |
| MAKE401 | 1/7/2019 | 3 | SALE203 |
| MAKE912 | 2/1/2019 | 1 | SALE205 |
I am trying to write a query that will enable me to determine which ID
or IDs
from the makes
table satisfies my sales.
My end results would look either like, in the case that they are LISTAGG
:
| ID | Date | Value | Type | Satisfied By |
|:-------:|:---------:|:-----:|:----:|:-------------------------:|
| SALE100 | 1/1/2019 | -5 | OUT | MAKE300 |
| SALE201 | 1/9/2019 | -10 | OUT | MAKE301, MAKE399, MAKE401 |
| SALE203 | 2/22/2019 | -1 | OUT | MAKE401 |
| SALE205 | 3/14/2019 | -1 | OUT | MAKE912 |
However, when writing the following line of code:
(SELECT LISTAGG(makes.id, ', ') WITHIN GROUP (ORDER BY NULL) FROM makes WHERE purchased.id = needed_for.id) ELSE NULL END AS Satisfied_By
results in an error stating:
ORA-01489: result of string concatenation is too long 01489. 00000 - "result of string concatenation is too long"
I have also tried the following query to obtain results like this (which is ideal):
| ID | Date | Value | Type | Satisfied By |
|:-------:|:---------:|:-----:|:----:|:------------:|
| SALE100 | 1/1/2019 | -5 | OUT | MAKE300 |
| SALE201 | 1/9/2019 | -10 | OUT | MAKE301 |
| SALE201 | 1/9/2019 | -10 | OUT | MAKE399 |
| SALE201 | 1/9/2019 | -10 | OUT | MAKE401 |
| SALE203 | 2/22/2019 | -1 | OUT | MAKE401 |
| SALE205 | 3/14/2019 | -1 | OUT | MAKE912 |
CASE WHEN Type = 'OUT' THEN
(SELECT
makes.id
FROM
makes
WHERE
makes.id IN (
SELECT
makes.id
FROM
makes
WHERE
sales.id = purchases.id
)) ELSE NULL END AS Satisfied_By
Which yields
ORA-01427: single-row subquery returns more than one row 01427. 00000 - "single-row subquery returns more than one row"
I have found many examples of this error on Stack Overflow, which is where I adopted the IN
method from, and from this source, but I am still getting the error. Any help is appreciated.