SELECT Distinct基于少数列

时间:2014-10-29 18:33:14

标签: sql-server distinct

我有一个MS SQL查询

SELECT DISTINCT Date_of_Record, Computer_Name, IP_Address, Agent_Version, Mac_Address
FROM [SEP_Versions].dbo.Computers
WHERE Computer_Name in (SELECT Computer_Name
                        FROM [SEP_Versions].dbo.Computers
                        GROUP BY Computer_Name
                        HAVING COUNT(DISTINCT Agent_Version) > 1)
AND [SEP_Versions].dbo.Computers.COMPUTER_NAME LIKE '%s001'
AND [SEP_Versions].dbo.Computers.IP_Address LIKE '%.100'
ORDER BY Computer_Name, Date_of_Record

输出

Date        Computer   IP               Version         Mac
2014-10-24  COMP001    10.10.10.10      12.1.3001.165   NULL
2014-10-25  COMP001    10.10.10.10      12.1.3001.165   NULL
2014-10-26  COMP001    10.10.10.10      12.1.3001.165   NULL
2014-10-27  COMP001    10.10.10.10      12.1.4013.4013  NULL
2014-10-28  COMP001    10.10.10.10      12.1.4013.4013  NULL
2014-10-29  COMP001    10.10.10.10      12.1.5773.2276  NULL

如何更改SELECT DISTINCT语句,以便在最早的日期输出具有不同版本的记录,即

Date        Computer   IP               Version         Mac
2014-10-24  COMP001    10.10.10.10      12.1.3001.165   NULL
2014-10-27  COMP001    10.10.10.10      12.1.4013.4013  NULL
2014-10-29  COMP001    10.10.10.10      12.1.5773.2276  NULL

@Pradeep

您的解决方案输出

Date        Computer   IP               Version         Mac
2014-10-17  COMP001    10.10.10.10      12.1.3001.165   NULL
2014-10-17  COMP002    10.10.10.11      12.1.4013.4013  NULL
2014-10-17  COMP003    10.10.10.12      12.1.5773.2276  NULL
2014-10-17  COMP004    10.10.10.14      11.0.5003.2276  NULL

@ 1010和@AWinkle

如果我跑

SELECT DISTINCT Computer_Name, IP_Address, Agent_Version
FROM dbo.Computers
WHERE Computer_Name in (SELECT Computer_Name
                        FROM dbo.Computers
                        GROUP BY Computer_Name
                        HAVING COUNT(DISTINCT Agent_Version) > 1)
AND dbo.Computers.COMPUTER_NAME LIKE '%s001'
AND dbo.Computers.IP_Address LIKE '%.100'
ORDER BY Computer_Name

然后我得到

Computer   IP               Version         Mac
COMP001    10.10.10.10      12.1.3001.165   NULL
COMP001    10.10.10.10      12.1.4013.4013  NULL
COMP001    10.10.10.10      12.1.5773.2276  NULL

如何修改SQL语句,以便添加相应的Date_of_Record

3 个答案:

答案 0 :(得分:3)

在SQL Server中,您不能仅按选定数量的列选择不同,您需要对它们进行分组。使用MIN函数将在最早的日期提供不同的版本(通过computer / ip / mac)。

SELECT MIN(Date_of_Record) Earliest_Date_of_Record, Computer_Name, IP_Address, Agent_Version, Mac_Address
FROM [SEP_Versions].dbo.Computers
WHERE Computer_Name in (SELECT Computer_Name
                    FROM [SEP_Versions].dbo.Computers
                    GROUP BY Computer_Name
                    HAVING COUNT(DISTINCT Agent_Version) > 1)
AND [SEP_Versions].dbo.Computers.COMPUTER_NAME LIKE '%s001'
AND [SEP_Versions].dbo.Computers.IP_Address LIKE '%.100'
GROUP BY Computer_Name, IP_Address, Agent_Version, Mac_Address
ORDER BY Computer_Name, Date_of_Record

答案 1 :(得分:1)

我认为这就是你想要的

SELECT MIN(Date_of_Record), Computer_Name, IP_Address, Agent_Version, Mac_Address
FROM ...
WHERE ...
GROUP BY Computer_Name, IP_Address, Agent_Version, Mac_Address
ORDER BY ...

group by子句将聚合Computer_Name,IP_Address,Agent_Version,Mac_Address的每个组合的行。 MIN(Date_of_Record)将为每个组计算最早的日期。

答案 2 :(得分:0)

简单回答是使用窗口功能。

;WITH cte
     AS (SELECT DISTINCT Row_number()
                           OVER (
                             partition BY Computer_Name,Agent_Version
                             ORDER BY Date_of_Record) RN,
                         Date_of_Record,
                         Computer_Name,
                         IP_Address,
                         Agent_Version,
                         Mac_Address
         FROM   [SEP_Versions].dbo.Computers
         WHERE  Computer_Name IN (SELECT Computer_Name
                                  FROM   [SEP_Versions].dbo.Computers
                                  GROUP  BY Computer_Name
                                  HAVING Count(DISTINCT Agent_Version) > 1)
                AND [SEP_Versions].dbo.Computers.COMPUTER_NAME LIKE '%s001'
                AND [SEP_Versions].dbo.Computers.IP_Address LIKE '%.100')
SELECT Date_of_Record,
                     Computer_Name,
                     IP_Address,
                     Agent_Version,
                     Mac_Address
FROM   cte
WHERE  RN = 1
ORDER  BY Computer_Name,
          Date_of_Record