我有一个来自mysql的sql查询,它将以十进制保存的值转换为点(。)。如何将其转换为支持SQL Server?它是SUM(IF - Query。
SELECT routines.date, routines.time,
SUM( IF( measurements.title = 'T_Temperatur', CAST( REPLACE( routines.value, ',', '.' ) AS DECIMAL( 18, 2 ) ), null ) ) AS Temperatur
FROM routines
INNER JOIN measure_routine ON routines.id = measure_routine.routine_id
INNER JOIN measurements ON measure_routine.measure_id = measurements.id
INNER JOIN pools ON measure_routine.pool_id = pools.id
GROUP BY routines.date, routines.time
ORDER BY routines.date, routines.time;
Mysql结果:
这是SQL服务器错误:
Msg 156, Level 15, State 1, Line 2
Incorrect syntax near the keyword 'IF'.
Msg 102, Level 15, State 1, Line 2
Incorrect syntax near ','.
答案 0 :(得分:4)
使用SQL标准CASE表达式:
SELECT routines.date, routines.time,
SUM( CASE WHEN measurements.title = 'T_Temperatur' THEN CAST( REPLACE( routines.value, ',', '.' ) AS DECIMAL( 18, 2 ) ) ELSE NULL END) AS Temperatur
FROM routines
INNER JOIN measure_routine ON routines.id = measure_routine.routine_id
INNER JOIN measurements ON measure_routine.measure_id = measurements.id
INNER JOIN pools ON measure_routine.pool_id = pools.id
GROUP BY routines.date, routines.time
ORDER BY routines.date, routines.time;