我是MYSQL的新手,我正在尝试将其转换为MSSQL。
我不理解突出显示的部分......
Q1。日期加入如何运作......? ... join(选择@last_time:= current_timestamp,@ last_object:= null)?
Q2。如何翻译" @last_time:= state_time,@ last_object:= object_id"在MSSQL中?
感谢您的帮助!
SELECT c.*,
coalesce(b.type, 'history') AS type,
coalesce(b.STATE, current_state) AS STATE,
coalesce(b.state_type, 1) AS state_type,
coalesce(CASE
WHEN b.start_time < range_start
THEN range_start
ELSE b.start_time
END, range_start) AS start_time,
coalesce(CASE
WHEN b.end_time > range_end
THEN range_end
ELSE b.end_time
END, range_end) AS end_time,
coalesce(b.time_span, timestampdiff(SECOND, range_start, range_end)) AS time_span,
range_start,
range_end,
timestampdiff(SECOND, range_start, range_end) AS total_span,
comment_data
FROM (
SELECT d.object_id,
d.name1,
d.name2,
d.name1 AS display_name,
'top' AS LEVEL,
CASE
WHEN d.name1 = 'Server Hosting'
THEN 'medium'
ELSE 'high'
END AS `tab` ,
e.current_state,
d.name1 AS setorder
FROM N_objects a
JOIN N_servicegroups b ON a.object_id = b.servicegroup_object_id
JOIN N_servicegroup_members c ON b.servicegroup_id = c.servicegroup_id
JOIN N_objects d ON c.service_object_id = d.object_id left
JOIN N_servicestatus e ON e.service_object_id = c.service_object_id
WHERE a.name1 = 'Dashboard'
AND d.is_active = 1
AND d.object_id IN (895)
) c * *
JOIN (
SELECT last_day(current_timestamp) + interval 1 day - interval 1 month AS range_start,
last_day(current_timestamp) + interval 1 day AS range_end
) d * *
LEFT JOIN (
SELECT b.*
FROM (
SELECT type,
object_id,
STATE,
state_type,
start_time,
end_time,
timestampdiff(SECOND, start_time, end_time) AS time_span,
'' AS comment_data
FROM (
* * SELECT 'history' AS type,
object_id,
STATE,
state_type,
CASE
WHEN @last_object != object_id
THEN current_timestamp
ELSE @last_time
END AS end_time,
state_time AS start_time,
@last_time: = state_time,
@last_object: = object_id
FROM N_statehistory a
JOIN (
SELECT @last_time: = current_timestamp,
@last_object: = NULL
) b * *
ORDER BY object_id,
start_time DESC
) a
UNION ALL
SELECT 'downtime' AS type,
object_id,
0,
1,
scheduled_start_time,
scheduled_end_time,
timestampdiff(SECOND, scheduled_start_time, scheduled_end_time),
comment_data
FROM N_downtimehistory
) b
JOIN (
SELECT last_day(current_timestamp) + interval 1 day - interval 1 month AS range_start,
last_day(current_timestamp) + interval 1 day AS range_end
) d ON b.start_time BETWEEN range_start
AND range_end
OR b.end_time BETWEEN range_start
AND range_end
) b ON b.object_id = c.object_id
ORDER BY setorder,
start_time
答案 0 :(得分:1)
我会通过将查询复制到SQL Server Management Studio,运行它(当然会抛出错误)并开始逐个处理错误来解决这个问题。如果可能的话,我可能会试着把整个小块的小块拿出来让它们单独工作。
我的:=
运算符(@last_time:=state_time,@last_object:=object_id
)我认为是一个赋值运算符;它将@
变量分配给那些相应的字段值。在MSSQL中,您可以执行SET @last_time = state_time
或SELECT @last_time=state_time
我认为来自您的示例和一些文档(http://dev.mysql.com/doc/refman/5.7/en/user-variables.html)它看起来可能正在尝试进行某种迭代维护,就像返回的每一行一样,它正在更新{下一轮的{1}}和@last_time
变量。自从我使用MySQL以来已经有一段时间了,所以我不确定这是否是预期的行为。
如果您解决查询试图解决的问题并从那里构建查询而不是尝试直接将一个语法移植到另一个上,那么移植可能会更容易。