2016-01-20 95 views
-1
SELECT RIGHT(timestamp,LEN(timestamp) -12) as DailyTime, left(roundtrip, LEN(roundtrip) -2) as HalfHourDuration, site_code 
FROM tblServer_Status 
WHERE timestamp >= dateadd(day, datediff(day,'19000101',CURRENT_TIMESTAMP),'19000101') AND timestamp < dateadd(day, datediff(day,'19000101',CURRENT_TIMESTAMP)+1,'19000101') AND server = 'ServerName' AND site_code = 'A' 
GROUP BY timestamp, roundtrip, site_code HAVING(((COUNT(site_code))>0)) 
ORDER BY timestamp 

每个时间间隔最新值我有这样的代码,让我这种输出如何获得在SQL Server

| DailyTime | HalfHourDuration | Site_Code| 
    12:00AM   122    A 
    12:00AM   143    A 
    12:00AM   242    A 
    12:30AM   112    A 
    12:30AM   222    A 
    12:30AM   462    A 
    01:00AM   322    A 
    01:00AM   642    A 
    01:00AM   322    A 
    01:30AM   146    A 
    01:30AM   167    A 
    01:30AM   116    A 
    02:00AM   163    A 
    02:00AM   145    A 
    02:00AM   121    A 
    02:30AM   149    A 
    02:30AM   135    A 
    02:30AM   111    A 
    ................................... 

,但我需要得到每一次最新的时间。 喜欢这个

| DailyTime | HalfHourDuration | Site_Code| 
    12:00AM   242    A 
    12:30AM   462    A 
    01:00AM   322    A 
    01:30AM   116    A 
    02:00AM   121    A 
    02:30AM   111    A 

就是这样的。 任何人都可以帮助我配置我的代码。

谢谢。

回答

0

可以使用row_number()做到这一点:

with t as (
     <your query here without order by> 
    ) 
select t.* 
from (select t.*, 
      row_number() over (partition by DailyTime 
           order by HalfHourDuration desc 
           ) as seqnum 
     from t 
    ) t 
where seqnum = 1; 
+0

伙计。谢谢。但时间的顺序是按数字。你能帮我把它按AM - PM排序。 – Elphrian

+0

我需要从12:00 AM到11:30 PM的时间排序。感谢您的答复。 – Elphrian

0

添加最大功能将您的列HalfHourDuration。 这将列出通过时间戳单独分组的最大值

+0

我已经试过了,但输出相同 – Elphrian