如何在 SQL 服务器中获取关于日期的唯一记录

How to get Unique record with repsect to Date in SQL server

这是我的桌子:

server  database  %full     Alert date
Server1 model     80.89 12/22/14 8:08 AM
Server1 tempdb    90.65 12/23/14 12:07 PM
Server1 Rep       80.33 12/4/14 8:06 AM
Server2 DB1       99.98 12/15/14 8:07 PM
Server2 DB1       99.99 12/15/14 8:07 PM
Server2 DB1       99.99 12/15/14 8:07 PM
Server2 DB2       95.2  12/29/14 6:06 AM
Server2 DB2       91.6  12/29/14 6:06 AM
Server2 DB1       98.57 12/29/14 6:06 AM
Server2 DB1       98.49 12/29/14 6:06 AM
Server2 DB1       98.17 12/29/14 6:06 AM
Server2 DB1       97.72 12/29/14 6:06 AM
Server3 DB1       97.86 12/29/14 6:06 AM
Server3 DB1       88.56 12/29/14 6:06 AM
Server3 DB1       88.48 12/29/14 6:06 AM
Server3 DB2       89.25 12/29/14 6:06 AM
Server3 DB2       89.1  12/29/14 6:06 AM
Server3 tempdb    90.76 12/29/14 8:07 PM
Server3 tempdb    90.77 12/29/14 8:07 PM
Server3 tempdb    90.77 12/29/14 8:07 PM
Server3 tempdb    90.75 12/29/14 8:07 PM

我想计算一段时间内每台服务器上的数据库完全警报数。我期望这样的结果:

server  database    %full   Alert date
Server1 model     80.89   12/22/14 8:08 AM
Server1 tempdb    90.65   12/23/14 12:07 PM
Server1 Rep       80.33   12/4/14 8:06 AM
Server2 DB1       99.99   12/15/14 8:07 PM
Server2 DB1       95.2    12/29/14 6:06 AM
Server2 DB2       91.6    12/29/14 6:06 AM
Server3 DB1       88.48   12/29/14 6:06 AM
Server3 DB2       89.25   12/29/14 6:06 AM
Server3 tempdb    90.75   12/29/14 8:07 PM

我正在使用以下查询:

SELECT  x.RowNo,server_Name, Database_Name,Percent_full,notification_count,Alert_date
FROM 
(
    SELECT F.server_Name, Database_Name,Percent_full,notification_count,F.Alert_date, 
        ROW_NUMBER() OVER (PARTITION BY F.Alert_date ORDER BY F.server_Name DESC) AS RowNo
       --ROW_NUMBER() OVER (PARTITION BY F.server_Name ORDER BY F.Alert_date DESC) AS RowNo1
    FROM 
    tbl1 D 
    INNER JOIN tb12 F ON D.Logicalservername = F.Server_Name
    WHERE DBPlatformCode = 'SQL' and environmentcode ='PROD'
    and F.Alert_Date between '12-01-2014' and '12-31-2014'
    --and F.server_Name='LOUSASWPS02'       
) x
WHERE x.RowNo = 1 
order by server_name

但是,跨服务器通用的数据库名称很少,因此无法获得准确的通知计数。

注:这些重复值分别对应数据库中的每个数据文件

将您的 window function 更改为

Row_number()OVER (PARTITION BY server_Name, Database_Name, F.Alert_date
            ORDER BY F.Percent_full )

partition by 中您需要添加 server_Name, Database_Name, F.Alert_date,在 Order by 中使用 Percent_full

像这样更改您的查询..

SELECT x.RowNo,
       server_Name,
       Database_Name,
       Percent_full,
       notification_count,
       Alert_date
FROM   (SELECT F.server_Name,
               Database_Name,
               Percent_full,
               notification_count,
               F.Alert_date,
               Row_number()OVER (PARTITION BY server_Name, Database_Name, F.Alert_date ORDER BY F.Percent_full ) AS RowNo
        FROM   tbl1 D
               INNER JOIN tb12 F
                       ON D.Logicalservername = F.Server_Name
        WHERE  DBPlatformCode = 'SQL'
               AND environmentcode = 'PROD'
               AND F.Alert_Date BETWEEN '12-01-2014' AND '12-31-2014'
       --and F.server_Name='LOUSASWPS02'       
       ) x
WHERE  x.RowNo = 1
ORDER  BY server_name