繁体   English   中英

包含AS的SQL SELECT语句

[英]SQL SELECT statement containing AS

我有SQL查询:

select title, scale / next_scale, c
from ( select title, scale, scale*D0 AS c, 
              lead(scale) over(partition by title order by scale asc) as next_scale,
              row_number() over(partition by title order by scale asc) as agg_row
       from signatures
     ) agg
where agg_row = 1;

并且它按预期工作。 但是,我真正希望排序的“标度”值是几列之间的算术运算,因此我尝试使用AS子句(如上所示)并将查询修改为:

select title, scale / next_scale, c
from ( select title, scale, scale*D0 AS c, 
              lead(scale) over(partition by title order by c asc) as next_scale,
              row_number() over(partition by title order by c asc) as agg_row
       from signatures
     ) agg
where agg_row = 1;

但是,它在ORDER BY c处失败。 为什么是这样? 我可以用ORDER BY scale*D0代替它,效果很好。 但是,我最终将要使用这样的术语: scale*D0*D1*D2*...*D100 ; 而且我不需要计算3次不同的时间-更不用说查询的物理长度了。 我希望具有scale*D0*D1*D2*...*D100 AS c ,然后具有ORDER BY c

这可能吗?

我正在使用PostgreSQL。

非常感谢,布雷特

在子查询中计算c

select title, scale / next_scale, c
from ( select title, scale, c, 
              lead(scale) over(partition by title order by c asc) as next_scale,
              row_number() over(partition by title order by c asc) as agg_row
       from (select title, scale, scale * D0 AS c from signatures) signatures_calc
     ) agg
where agg_row = 1;

您可以按列的序号ORDER BY:

select title, scale / next_scale, c
from ( select title, scale, scale*D0 AS c, 
          lead(scale) over(partition by title order by scale asc) as next_scale,
          row_number() over(partition by title order by scale asc) as agg_row
   from signatures
 ) agg
where agg_row = 1
order by 3;

下订单时是子查询,应填写帐单

select title, scale / next_scale, c
from ( select title, scale, scale*D0 AS c, 
          lead(scale) over(partition by title order by scale asc) as next_scale,
          row_number() over(partition by title order by scale asc) as agg_row
       from signatures
       order by 3
 ) agg
where agg_row = 1;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM