People say CTE is a view and you can\'t set a variable in CTE. But I successfully set a variable @ttl in CTE running the query below:
with CTE as (select @ttl