[英]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.