Skip to content

Unnable to run correlated subquery #43747

Closed

Description

@miroha

Greetings!
Some preview:
I've some table. Each date contains data snapshots for 15 days ago and 50 days ahead. If I'll go to my table and run SELECT WHERE date = '2022-01-27' I will get rows with block_date from 2022-01-13 to 2022-03-18. I need to build reports per month, so at date = '2022-01-27' I need to see rows with block_date from 2022-01-01 to 2022-01-31. I can find this missing information if I go back a day. At date = '2022-01-26' I will find information for block_date = 2022-01-12 and so on.

For example I've these rows:

|date        | block_date   | total_plan_volume_grp20  |
| -----------| ------------ | -------------------------|
| 2022-01-27 | 2022-01-14   | 10                       |
| 2022-01-27 | 2022-01-13   | 20                       |
| 2022-01-26 | 2022-01-14   | 10                       | 
| 2022-01-26 | 2022-01-13   | 20                       | 
| 2022-01-26 | 2022-01-12   | 30                       | 
| 2022-01-25 | 2022-01-14   | 10                       | 
| 2022-01-25 | 2022-01-13   | 20                       |  
| 2022-01-25 | 2022-01-12   | 30                       | 
| 2022-01-25 | 2022-01-11   | 40                       |

I expect to get this:

|date        |block_date    |total_plan_volume_grp20    |
| --------   | ------------ | ------------------------- |
| 2022-01-27 | 2022-01-14   | 10                        |
| 2022-01-27 | 2022-01-13   | 20                        |
| 2022-01-27 | 2022-01-12   | 30                        |
| 2022-01-27 | 2022-01-11   | 40                        |
| 2022-01-26 | 2022-01-14   | 10                        |
| 2022-01-26 | 2022-01-13   | 20                        | 
| 2022-01-26 | 2022-01-12   | 30                        | 
| 2022-01-26 | 2022-01-11   | 40                        | 
| 2022-01-25 | 2022-01-14   | 10                        |
| 2022-01-25 | 2022-01-13   | 20                        | 
| 2022-01-25 | 2022-01-12   | 30                        | 
| 2022-01-25 | 2022-01-11   | 40                        | 

Early we used PostgreSQL and query below works fine with Postge. I tried to modify query a bit and run it in ClickHouse.
Query:

    SELECT
      `date`,
      block_date,
      total_plan_volume_grp20
    FROM schema.table 
    UNION ALL
    SELECT 
      addDays(`date`, 1),
      block_date,
      total_plan_volume_grp20
    FROM schema.table t1
    where exists (
       select true 
       from schema.table t2 
       where t2.`date` = addDays(t1.`date`, 1))
    UNION ALL
    SELECT 
      addDays(`date`, 2),
      block_date,
      total_plan_volume_grp20
    FROM schema.table t1
    where exists (
       select true 
       from schema.table t2 
       where t2.`date` = addDays(t1.`date`, 2))

It throws an error:
while processing query: 'SELECT true FROM schema.table AS t2 WHERE date = addDays(t1.date, 1)', required columns: 'date' 't1.date', maybe you meant: ['date']: While processing 1 IN (SELECT 1 FROM (SELECT true FROM schema.table AS t2 WHERE t2.date = addDays(t1.date, 1)) LIMIT 1). (UNKNOWN_IDENTIFIER) (version 22.10.2.11 (official build))

How to handle this behaviour? Or any tips how can I change query?
Sorry, If I chose category wrong.

Thanks!

Using:
ClickHouse 22.10.2.11
DBeaver 22.2.5

Activity

  1. added
    unexpected behaviour
    on Nov 28, 2022
  2. tavplubix commented on Nov 28, 2022

    @tavplubix
    Member

    Correlated subqueries are not supported

  3. changed the title [-]Unnable to run query with EXISTS[/-] [+]Unnable to run correlated subquery[/+] on Nov 28, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    unexpected behaviour

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions