1

/* THIS WORKS - 显示昨天所有 prod 服务器的每小时会话 */

SELECT
SUM(active_sessions_peak + inactive_sessions_peak) 
AS TotalSessions, machine_name, time_stamp
FROM  ctrx_archive_system_perf
WHERE machine_name LIKE 'vmw-mc-%p%'
GROUP BY machine_name, time_stamp
ORDER BY machine_name

/* 这也有效 - 每个服务器只返回午夜的值 */

SELECT
SUM(active_sessions_peak + inactive_sessions_peak) 
AS TotalSessions, machine_name, time_stamp
FROM   vw_ctrx_archive_system_perf
WHERE machine_name LIKE 'vmw-mc-%p%'
AND 
time_stamp = CAST(DATEADD(d,-1,GETDATE()) AS DATE) 
GROUP BY machine_name, time_stamp
ORDER BY machine_name

最后一个给了我每个服务器的 TotalSessions,但只有午夜一个。在一天中,这些会话数当然会发生变化。我正在寻找的是每个 machine_name 当天报告的最大数字。我研究了从 SUM 中获取 MAX 并尝试了一个临时表和一个子查询,但都无法使用任何一种方法。

4

2 回答 2

0

你可能想要这样的东西:

SELECT MAX(TotalSessions) as MaxSessions, machine_name
FROM (
  SELECT
  SUM(active_sessions_peak + inactive_sessions_peak) 
  AS TotalSessions, machine_name, time_stamp
  FROM  ctrx_archive_system_perf
  WHERE machine_name LIKE 'vmw-mc-%p%'
  GROUP BY machine_name, time_stamp
) AS machine_sums
GROUP BY machine_name
ORDER BY machine_name

这应该适用于任何理解 SQL 的数据库。如果您还需要获取相关的时间戳,事情就会变得更加困难。

于 2013-08-09T19:51:10.153 回答
0
;with cte1 as
    select
        sum(active_sessions_peak + inactive_sessions_peak) as TotalSessions,
        machine_name,
        time_stamp
    from ctrx_archive_system_perf
    where machine_name LIKE 'vmw-mc-%p%'
    group by machine_name, time_stamp
), cte2 as (
    select
        row_number() over (partition by machine_name order by TotalSessions desc) as row_num,
        *
    from cte1 
)
select
    TotalSessions, machine_name, time_stamp
from cte2
where row_num = 1
于 2013-08-10T07:08:06.763 回答