【发布时间】:2020-06-01 11:43:22
【问题描述】:
我正在尝试加入基于分组列“年份”的 sqlite 表。我从一个返回数据库中所有年份的 select 子句开始,现在我想对同一个表进行子查询以获取各种统计信息并根据年份加入它。以下是我所拥有的。
select strftime('%Y', `date`, 'unixepoch') as `year` from transactions as t
left join (
select sum(amount) as `expenses`,
strftime('%Y', `date`, 'unixepoch') as `year` from transactions
where type = -1 and user_id = 1
group by strftime('%Y', `date`, 'unixepoch')
) as e on e.year = t.year
left join (
select sum(amount) as `income`,
strftime('%Y', `date`, 'unixepoch') as `year` from transactions
where type = 1 and user_id = 1
group by strftime('%Y', `date`, 'unixepoch')
) as i on i.year = t.year
group by strftime('%Y', `date`, 'unixepoch');
当我尝试运行该 sql 语句时,我得到“没有这样的列:t.year”。为什么我不能根据别名列加入?
【问题讨论】:
-
请提供minimal reproducible example,
create table ...和insert ...几行的形式;也是与来自inserts 的样本数据匹配的所需输出。