SQLite如何使用生成的列在同一查询中执行其他计算

时间:2019-03-08 17:01:37

标签: sql sqlite

我习惯于SAS中的sql语法,该语法允许使用其他基于聚集函数生成的列来创建其他计算。例如:

select date(created_at) as date,
       count(distinct case when platform = 'Android' then user_id end) as 
       Android,
       count(distinct case when platform = 'iOS' then user_id end) as iOS,
       calculated Android + calculated iOS as Total
from dataset
group by 1;

我的问题在上面创建的最后一列“总计”中。 SAS具有称为“计算”的内置功能,该功能可以识别已经生成的列,并允许使用该列进行其他计算。

SQLite中是否存在类似的东西,还是子查询是唯一的其他选择?

1 个答案:

答案 0 :(得分:0)

您需要在标准SQL中使用子查询或CTE:

select d.*, (Android + iOS) as Total
from (select date(created_at) as date,
             count(distinct case when platform = 'Android' then user_id end) as Android,
             count(distinct case when platform = 'iOS' then user_id end) as iOS,
      from dataset
      group by date(created_at)
     ) d;

calculated关键字是proc sql中的一个方便扩展。但是,它在其他数据库中不可用。

相关问题