SQL。语法选择执行算术运算

时间:2016-07-28 09:32:32

标签: sql-server

我希望在选择的syntac sql中执行算术运算,例如10减5 = 5,但我的选择不起作用。

select DATEDIFF(MINUTE,on_duty, clock_in ) as late, DATEDIFF(MINUTE ,off_duty, clock_out ) as Early, late - Early as jajalan from kkpsurabaya

look image pls

1 个答案:

答案 0 :(得分:1)

由于解析查询的方式,您不能在SELECT和ORDER BY之外的同一个select中使用该函数的结果,因为you cannot refer to an alias。你可以多次调用同一个函数:

select DATEDIFF(MINUTE,on_duty, clock_in ) as late, 
       DATEDIFF(MINUTE ,off_duty, clock_out ) as Early, 
       DATEDIFF(MINUTE,on_duty, clock_in )- DATEDIFF(MINUTE ,off_duty, clock_out ) as jajalan 
from kkpsurabaya

或共同表格:

WITH CTE AS
(
    SELECT DATEDIFF(MINUTE,on_duty, clock_in ) as Late,
    DATEDIFF(MINUTE ,off_duty, clock_out ) as Early,
    t.*
    FROM kkpsurabaya t  
)
SELECT Late, Early, Late - Early as jajalan
FROM CTE