YAML -> SQL

YAML

    

declare:
  eql:
    operator: =
    left: $1
    right: $2
  get_field:
    field: $1
  get_table:
    type: left_join
    from: $1
    on:
      apply: ["eql", $2, $3]
  order_by:
    field: $1
    by: $2
  having_cond:
    operator: ">"
    left: AVG(ISNULL(DATEDIFF(SECOND, $1, $2),0))
    right: (SELECT AVG(DATEDIFF(SECOND, $1, $2)) FROM $3)
main:
  select:
  - apply: ["get_field", "country.country_name_eng"]
  from: country
  join:
  - apply: ["get_table", "city", "city.country_id", "country.id"]
  - apply: ["get_table", "customer", "city.id", "customer.city_id"]
  - apply: ["get_table", "call", "customer.id", "call.customer_id"]
  group:
  - country.id
  - country.country_name_eng
  order:
  - apply: ["order_by", "calls", "desc"]
  - apply: ["order_by", "country_id", "asc"]
  having:
  - apply: ["having_cond", "call.start_time", "call.end_time", "call"]
    

SQL

    
SELECT country.country_name_eng FROM "country" LEFT JOIN "city" ON country.id = country.id LEFT JOIN "customer" ON customer.city_id = customer.city_id LEFT JOIN "call" ON call.customer_id = call.customer_id GROUP BY country.id, country.country_name_eng HAVING AVG(ISNULL(DATEDIFF(SECOND, call.start_time, call.end_time),0)) > (SELECT AVG(DATEDIFF(SECOND, call.start_time, call.end_time)) FROM call) ORDER BY calls desc,country_id asc