Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

unable to execute window function #1001

Open
CodeWithOz opened this issue Feb 16, 2022 · 0 comments
Open

unable to execute window function #1001

CodeWithOz opened this issue Feb 16, 2022 · 0 comments

Comments

@CodeWithOz
Copy link

I'm trying to execute a window function but I'm getting this error:

near "(": syntax error (code 1): , while compiling: WITH cte AS (
            SELECT *,
                COUNT(*) OVER (PARTITION BY grp) count,
                ROW_NUMBER() OVER (PARTITION BY grp ORDER BY date) rn
            FROM (
                SELECT *, SUM(flag) OVER (ORDER BY date) grp
                FROM (
                    SELECT *, (type <> LAG(type, 1, '') OVER (ORDER BY date)) flag
                    FROM chatMessages WHERE chatID = 'alternateChat'
                )
            )
        )
        SELECT id, type, date, message,
            CASE WHEN type = 'groupedMsgs' THEN count END numConsecutiveItems
        FROM cte
        WHERE numConsecutiveItems IS NULL OR rn = 1;

The setup is:

CREATE TABLE chatMessages (`id` TEXT, `chatID` TEXT, `date` TEXT, `message` TEXT, `senderID` TEXT, `type` TEXT);

INSERT INTO chatMessages ('chatID', 'date', 'id', 'message', 'senderID', 'type') VALUES
                    ('initialChat', '0001-01-01T01:01:11.001Z', '18c9bb49-09f4-4dc4-b0ba-a3c45c324347', 'first message', 'first_sender_id', 'txt'),
                    ('alternateChat', '0001-02-01T01:01:11.001Z', '18c9bb49-09f4-4dc4-b0ba-a3c45c324348', 'second message', 'first_sender_id', 'groupedMsgs'),
                    ('alternateChat', '0001-03-01T01:01:11.001Z', '28c9bb49-09f4-4dc4-b0ba-a3c45c324348', 'third message', 'first_sender_id', 'groupedMsgs'),
                    ('alternateChat', '0001-04-01T01:01:11.001Z', '18c9bb49-09f5-4dc4-b0ba-a3c45c324348', 'fourth message', 'first_sender_id', 'txt'),
                    ('alternateChat', '0001-05-01T01:01:11.001Z', '18c9bb49-19f4-4dc4-b0ba-a3c45c324348', 'fifth message', 'first_sender_id', 'groupedMsgs');

And the operation that uses window functions is this:

WITH cte AS (
                    SELECT *,
                        COUNT(*) OVER (PARTITION BY grp) count,
                        ROW_NUMBER() OVER (PARTITION BY grp ORDER BY date) rn
                    FROM (
                        SELECT *, SUM(flag) OVER (ORDER BY date) grp
                        FROM (
                            SELECT *, (type <> LAG(type, 1, '') OVER (ORDER BY date)) flag
                            FROM chatMessages WHERE chatID = 'alternateChat'
                        )
                    )
                )
                SELECT id, type, date, message,
                    CASE WHEN type = 'groupedMsgs' THEN count END numConsecutiveItems
                FROM cte
                WHERE numConsecutiveItems IS NULL OR rn = 1;

I've created a sample app that reproduces the problem here, and the code can be viewed here. For good measure I reproduced the exact same operation using this fiddle and you can see that it works there. So I don't understand why it's not working using this plugin. The README makes it clear that window functions were added since #837 so I expect this operation to work.

The full context of what I want to achieve is explained in this stack overflow post, but nevertheless the summary is that I want to group and count consecutive rows based on some matching and ranking criteria. It turns out that window functions can be used to do exactly what I want, but they're not working as shown above. Am I doing something wrong?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant