如何获取Max列的对应值

时间:2015-12-10 02:48:30

标签: sql-server

如果我有这些数据怎么办?我需要得到最新时间和最近一次往返的时间???

int

我需要获取不同站点中所有服务器的最新时间及其相应的往返。因为在我的代码中我也得到了往返的最大值。

     Server| Site | RoundTrip |   Time   |
       A      AA      200       09:02:17
       A      AA      100       09:02:16
       A      AA      150       09:02:14
       B      AA      250       09:02:15
       B      AA      200       09:02:18
       B      AA      270       09:02:16
       C      AA      100       09:02:19
       C      AA      150       09:02:18
       C      AA      200       09:02:15
       D      AA      300       09:02:13
       D      AA      200       09:02:15
       D      AA      250       09:02:14
       A      BB      200       09:02:58
       A      BB      100       09:02:49
       A      BB      150       09:02:53
       B      BB      150       09:02:50
       B      BB      350       09:02:54
       B      BB      250       09:02:53
       C      BB      200       09:02:56
       C      BB      175       09:02:55
       C      BB      250       09:02:57
       D      BB      260       09:02:50
       D      BB      200       09:02:52
       D      BB      140       09:02:51

任何人都可以帮助我吗? 请..

4 个答案:

答案 0 :(得分:1)

我认为你SELECT MAX(RoundTrip) GROUP BY (Server, Site)。您可以使用ROW_NUMBER选择最大值,如下所示:

;WITH CTE AS (
   SELECT * FROM (
      SELECT
         *,
         ROW_NUMBER() OVER(PARTITION BY Server, Site ORDER BY RoundTrip DESC) AS RN
      FROM Your_Table
   ) AS A
   WHERE RN = 1 -- choose max value RoundTrip)

-- using CROSS APPLY
SELECT A.Site, A.[AA-RoundTrip], A.[AA-Time], B.[BB-RoundTrip], B.[BB-Time]
   FROM (
      SELECT Site, RoundTrip AS [AA-RoundTrip], Time AS [AA-Time]
      FROM CTE WHERE Server = AA) AS A
   CROSS APPLY (
      SELECT Site, RoundTrip AS [BB-RoundTrip], Time AS [BB-Time]
      FROM CTE WHERE Server = BB AND Site = A.Site) AS B

-- using INNER JOIN
   SELECT A.Site, A.[AA-RoundTrip], A.[AA-Time], B.[BB-RoundTrip], B.[BB-Time]
   FROM (
      SELECT Site, RoundTrip AS [AA-RoundTrip], Time AS [AA-Time]
      FROM CTE WHERE Server = AA) AS A
   INNER JOIN (
      SELECT Site, RoundTrip AS [BB-RoundTrip], Time AS [BB-Time]
      FROM CTE WHERE Server = BB) AS B
     ON A.Site = B.Site

答案 1 :(得分:1)

我认为该行中的某些内容可行:

SELECT  AA.SERVER, 
        AA.[AA-ROUNDTRIP],
        AA.[TIME-AA],
        BB.[BB-ROUNDTRIP],
        BB.[TIME-BB]        
FROM
(SELECT SERVER, 
        MAX(ROUNDTRIP)[AA-ROUNDTRIP],
        TIME[TIME-AA]
  FROM ANYTABLE
  WHERE SITE= 'AA'
  GROUP BY SERVER,TIME) AA

INNER JOIN 
    (SELECT SERVER , 
            MAX(ROUNDTRIP)[BB-ROUNDTRIP],
            TIME[TIME-BB]
    FROM ANYTABLE
    WHERE SITE= 'BB'
    GROUP BY SERVER,TIME) BB

ON AA.SERVER=BB.SERVER

答案 2 :(得分:0)

以下查询使用files.tWrite(Levels.Warn, "Fix the problem"); 计算每个网站和服务器的最大往返次数,然后使用PARTITION计算出正确的值。

CASE

使用SQL小提琴: http://sqlfiddle.com/#!3/007a0/5

答案 3 :(得分:0)

这样的东西?

WITH LEFT_PART AS (
SELECT 
        SERVER,
        AA_Roundtrip = MIN(Roundtrip)
    FROM 
        DATA
    WHERE
        Site = 'AA'
    GROUP BY Server
),RIGHT_PART AS (
SELECT 
        SERVER,
        BB_Roundtrip = MIN(Roundtrip)
    FROM 
        DATA
    WHERE
        Site = 'BB'
    GROUP BY Server
)
SELECT 
        LP.Server
        ,AA_Roundtrip
        ,Time_AA = (
            SELECT TOP(1) time FROM DATA D
                WHERE
                    D.Server = LP.Server
                    AND D.Roundtrip = LP.AA_Roundtrip
                ORDER BY
                    D.time DESC
        ) 
        ,BB_Roundtrip
        ,Time_BB = (
            SELECT TOP(1) time FROM DATA D
                WHERE
                    D.Server = LP.Server
                    AND D.Roundtrip = RP.BB_Roundtrip
                ORDER BY
                    D.time  DESC    
        )
    FROM 
        LEFT_PART LP 
        JOIN RIGHT_PART RP ON LP.Server = RP.Server