0

Hello when I run the below query:

SELECT fundid 
FROM funds
WHERE reportingfrequency = 'Daily'
AND primarynavcostlocation = 'Wroclaw'
AND primaryaccountmgr1 = 'wx

I get:

"fundid"
11111
22222

So now what I'm trying to do is insert both those fundid rows along with an array so that the data will look something like below:

"fundid" | "listdate"
11111    | {2021-01-02, 2021-01-03}
22222    | {2021-01-02, 2021-01-03}

I've tried to finagle the query's around to get an insert working such as -

INSERT into nav_due_dates (fund_id, listdate)
VALUES(
        (
            SELECT fundid 
            FROM funds
            WHERE reportingfrequency = 'Daily'
            AND primarynavcostlocation = 'Wroclaw'
            AND primaryaccountmgr1 = 'wxy'
        ),
        (
            SELECT array_agg(weekdays::date)
            FROM generate_series(date'2021-01-01', date'2021-12-31', interval '1' day) as t(weekdays)
            LEFT JOIN holidays.poland(2021, 2021) f ON (weekdays = f.datestamp)
            WHERE f.datestamp IS NULL 
            AND extract(dow from weekdays) BETWEEN 1 AND 5
        )
);

It returns:

ERROR: more than one row returned by a subquery used as an expression

and I've also tried the following after doing some digging -

INSERT INTO nav_due_dates (fund_id, listdate)
        SELECT fundid 
        WHERE reportingfrequency = 'Daily'
        AND primarynavcostlocation = 'Wroclaw'
        AND primaryaccountmgr1 = 'wxy', 
             (
                SELECT array_agg(weekdays::date)
                FROM generate_series(date'2021-01-01', date'2021-12-31', interval '1' day) as t(weekdays)
                LEFT JOIN holidays.poland(2021, 2021) f ON (weekdays = f.datestamp)
                WHERE f.datestamp IS NULL 
                AND extract(dow from weekdays) BETWEEN 1 AND 5
             )
       FROM funds;

But the syntax errors I can't seem to resolve such as removing the , or the parenthesis around the array select statement that populates the listdate column.

0

2 Answers 2

1

I think you want to do this :

insert into nav_due_dates (fund_id, listdate)
select fundid, dd 
from 
    (
        SELECT fundid 
        FROM funds
        WHERE reportingfrequency = 'Daily'
        AND primarynavcostlocation = 'Wroclaw'
        AND primaryaccountmgr1 = 'wxy'
    ) t1
cross join 
    (
        SELECT array_agg(weekdays::date) dd
        FROM generate_series(date'2021-01-01', date'2021-12-31', interval '1' day) as t(weekdays)
        LEFT JOIN holidays.poland(2021, 2021) f ON (weekdays = f.datestamp)
        WHERE f.datestamp IS NULL 
        AND extract(dow from weekdays) BETWEEN 1 AND 5
    ) t2
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the answer, but I managed to solve it on my own and it was just an ordering on the syntax issue XD
0

So I managed to get it working and it was entirely because of the ordering of the FROM section.

INSERT INTO nav_due_dates (fund_id, listdate)
SELECT fundid,
     (
        SELECT array_agg(weekdays::date)
        FROM generate_series(date'2021-01-01', date'2021-12-31', interval '1' day) as t(weekdays)
        LEFT JOIN holidays.poland(2021, 2021) f ON (weekdays = f.datestamp)
        WHERE f.datestamp IS NULL 
        AND extract(dow from weekdays) BETWEEN 1 AND 5
     )
FROM funds
WHERE reportingfrequency = 'Daily'
AND primarynavcostlocation = 'Wroclaw'
AND primaryaccountmgr1 = 'wxy';

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.