联接表时无法获得独特的结果

时间:2018-11-28 19:25:41

标签: sql sql-server-2008

我遇到的问题与SELECT DISTINCT still showing duplicates

我现在了解到DISTINCT应用于整行,因此结果正在做应做的事情。我正在尝试返回以下行,但我不希望任何重复的序列号。如果有重复项,我只想取最后一个条目。

这是我的查询:

USE [db1]
SELECT DISTINCT Serial.[Serial Number], Plugin.[Plugin Name] AS [Model], Revision.Revision AS [Firmware Revision], ConnectionType.[Connection Type] As [Connection]
FROM Station
JOIN Plugin ON Station.[Plugin ID] = Plugin.[Plugin ID]
JOIN Serial ON Station.[Serial ID] = Serial.[Serial ID]
JOIN Revision ON Station.[Revision ID] = Revision.[Revision ID]
JOIN Connection ON Station.[Connection ID] = Connection.[Connection ID]
JOIN ConnectionType On Connection.[Connection Type ID] = ConnectionType.[Connection Type ID]
WHERE Station.[MAC Address ID] = (select MAC.[MAC Address ID] from MAC where mac.[MAC Address] = '123456')
ORDER BY Serial.[Serial Number] DESC

这是结果:

enter image description here

因此您可以看到X12132的两个条目,因为该设备上的固件已在某个时刻进行了更新,而DISTINCT已应用于整个生产线,但不幸的是,导致序列号重复。我该怎么做才能解决此问题?

我尝试做一个GROUP BY Serial.[Serial Number],但这给了我以下错误:

  

信息8120,第16级,状态1,第2行   选择列表中的“ Plugin.Plugin名称”列无效,因为它既不包含在聚合函数中,也不包含在GROUP BY子句中。   消息145,第15级,州1,第2行   如果指定了SELECT DISTINCT,则ORDER BY项目必须出现在选择列表中。

我感觉也许与我的联接有关,但是我一直无法弄清楚。如果您需要有关其他表格的更多信息,请告诉我。任何帮助表示赞赏。

3 个答案:

答案 0 :(得分:0)

通过此查询获取所需内容的最简单方法是在row_number()中使用order by

SELECT TOP (1) WITH TIES Serial.[Serial Number], Plugin.[Plugin Name] AS [Model], Revision.Revision AS [Firmware Revision], ConnectionType.[Connection Type] As [Connection]
FROM Station
JOIN Plugin ON Station.[Plugin ID] = Plugin.[Plugin ID]
JOIN Serial ON Station.[Serial ID] = Serial.[Serial ID]
JOIN Revision ON Station.[Revision ID] = Revision.[Revision ID]
JOIN Connection ON Station.[Connection ID] = Connection.[Connection ID]
JOIN ConnectionType On Connection.[Connection Type ID] = ConnectionType.[Connection Type ID]
WHERE Station.[MAC Address ID] = (select MAC.[MAC Address ID] from MAC where mac.[MAC Address] = '123456')
ORDER BY ROW_NUMBER() OVER (PARTITION BY Serial.[Serial Number] ORDER BY ? DESC);

我不知道您如何定义“最后输入”,因此ORDER BY ?用于定义顺序的列。

答案 1 :(得分:0)

您似乎对X12132有两个固件版本。您选择最大固件版本并按其他列分组:

SELECT
    Serial.[Serial Number],
    Plugin.[Plugin Name] AS [Model], 
    MAX(Revision.Revision) AS [Firmware Revision], 
    ConnectionType.[Connection Type] As [Connection]
FROM ...
GROUP BY Serial.[Serial Number], Plugin.[Plugin Name] AS [Model], ConnectionType.[Connection Type]

答案 2 :(得分:0)

USE [db1]
SELECT DISTINCT Serial.[Serial Number], Plugin.[Plugin Name] AS [Model], Revision.Revision AS [Firmware Revision], ConnectionType.[Connection Type] As [Connection]
FROM Station
JOIN Plugin ON Station.[Plugin ID] = Plugin.[Plugin ID]
JOIN Serial ON Station.[Serial ID] = Serial.[Serial ID]
JOIN Revision ON Station.[Revision ID] = Revision.[Revision ID]
JOIN Connection ON Station.[Connection ID] = Connection.[Connection ID]
JOIN ConnectionType On Connection.[Connection Type ID] = ConnectionType.[Connection Type ID]
WHERE Station.[MAC Address ID] = (select MAC.[MAC Address ID] from MAC where mac.[MAC Address] = '123456')
gorup by Serial.[Serial Number],Plugin.[Plugin Name],Revision.Revision, ConnectionType.[Connection Type]
ORDER BY Serial.[Serial Number] DESC