如何使用SQL中的計算字段排序?如何使用SQL中計算字段的順序?
select a.Customer
,a.PlanTo
,a.Dollar01
,a.Dollar02
,a.Dollar03
,a.Dollar04
,a.Dollar05
,a.Dollar06
,a.Dollar07
,a.Dollar08
,a.Dollar09
,a.Dollar10
,a.Dollar11
,a.Dollar12
,(CAST(a.Dollar01 as decimal) + CAST(a.Dollar02 as decimal)
+ CAST(a.Dollar03 as decimal) + CAST(a.Dollar04 as decimal)
+ CAST(a.Dollar05 as decimal) + CAST(a.Dollar06 as decimal)
+ CAST(a.Dollar07 as decimal) + CAST(a.Dollar08 as decimal)
+ CAST(a.Dollar09 as decimal) + CAST(a.Dollar10 as decimal)
+ CAST(a.Dollar11 as decimal) + CAST(a.Dollar12 as decimal)) as TOTAL1
from MDM_STAT.sds.SMarginText a
where a.salesyear = '2016'
order by a.total1
這是給我的「共1」列不存在,但你看,我已經創造了它,如果我不使用order by
子句工作。
刪除別名'a.' - 嘗試'按total1'排序... – sgeddes
'a.total1'不存在,因爲SQL Server將在'SMarginText'中查找該列,但'ORDER BY TOTAL1 ;'會工作得很好。 –
(1)格式化你的代碼和(2)澄清(標記)你實際使用的DBMS是PostgreSQL還是SQL Server? – Nicarus