Let's say I have a table B
:
client_id | n_periods | qtty |
---|---|---|
1 | 9 | 200 |
2 | 3 | 100 |
3 | 8 | 150 |
And then I have a (large) table foo
:
client_id | date | period | amount |
---|---|---|---|
1 | 2022-01-01 | 1 | 3000 |
1 | 2022-01-01 | 2 | 2800 |
1 | 2022-01-01 | 24 | 2800 |
2 | 2022-01-05 | 1 | 3500 |
2 | 2022-01-05 | 2 | 3500 |
2 | 2022-01-05 | 24 | 3500 |
I want to randomly SELECT
the top B.n_periods FROM FOO WHERE B.client_id = foo.client_id
. I other words, I want to randomly select n-rows for each client_id in foo
, where the n-rows is given in table B
also for each client_id. I also want to perform a simple substraction foo.amount minus B.qtty.
I have tried:
SELECT
TOP B.n_periods
foo.client_id,
foo.date,
foo.period,
foo.amount - B.qtty
FROM B, foo
But I get the error:
[42000][923] ORA-00923: FROM keyword not found where expected
I am a bit lost, I have tried using CONNECT BY
but maybe I don't understand well how to use it.