Рекурсивный запрос в RedShift - PullRequest
0 голосов
/ 09 июня 2018

Я очень плохо знаком с SQL и RedShift.У меня есть две таблицы.

account_usage:

account_id | usage_month |  usage_cost | usage_plan      | usage_type
   1      | 06-01-2018  |   100$      | 2018 - Custom   | dining
   1      | 06-01-2018  |   40$       | 2018 - Standard | office_supply
   2      | 06-01-2018  |   20$       | 2018 - Standard | dining
   2      | 06-01-2018  |   30$       | 2018 - Custom   | office_supply
   3      | 06-01-2018  |   25$       | 2018 - Custom   | dining
   3      | 06-01-2018  |   22$       | 2018 - Standard | office_supply

account_structure:
account_id | account_parent_id | account_name
  1          |  3                | account_1
  2          |  3                | account_2
  3          |  0                | account_3

из этих двух таблиц, я хочу построить таблицу агрегации.В этой таблице для каждого идентификатора общее использование будет суммой использования одного и того же аккаунта + использования всех его дочерних учетных записей.total_usage_by_type будет строкой json, которая будет накапливать использование use_type в строке json.

account_usage_aggregations:
account_id | usage_month |  usage_plan  | total_usage | total_usage_by_type
1          | 06-01-2018  |  2018-Custom   | 100      |{"dining":100}
1          | 06-01-2018  |  2018-Standard | 40       |{"office_supply":40}
2          | 06-01-2018  |  2018-Custom   | 30       |{"office_supply":30}
2          | 06-01-2018  |  2018-Standard | 20       |{"dining":20}
3          | 06-01-2018  |  2018-Standard | 82       |{"office_supply":62 , "dining": 20}
3          | 06-01-2018  |  2018-Custom   | 155      |{"office_supply":100, "dining": 55}

Я хотел решить эту проблему в рекурсивном запросе и начал с этого

  with C as (
     select account_id,
        usage_type,
        usage_cost,
        account_id as RootID
    from account_usage
    union all
    select account_id,
        usage_type,
        usage_cost,
        C.RootID
    from account_usage
    join C 
    on account_structure.account_parent_id = C.account_id
 ) select * from C;

Но я столкнулся со следующей ошибкой.

 [Amazon](500310) Invalid operation: relation "c" does not exist;

1 оператор не выполнен.

Есть ли способ выполнить рекурсивные запросы в redShift?

...