您可以通过
SELECT [RegisterId]
,[DeviceSerial]
, Max ([RegisterName]),0 AS Contribution
FROM [RegisterCountLogs]
WHERE DeviceSerial = 2160563829
GROUP BY [RegisterId]
,[DeviceSerial]
做1组现在,如果你只是想有RegisterId和DeviceSerial的一个组合行,那么你就必须添加一个HAVING
SELECT [RegisterId]
,[DeviceSerial]
, Max ([RegisterName]),0 AS Contribution
FROM [RegisterCountLogs]
WHERE DeviceSerial = 2160563829
GROUP BY [RegisterId]
,[DeviceSerial]
HAVING Count (*) = 1
。
编辑
在进一步思考,你可能希望在最新/最新注册的名称。
SELECT [RegisterId]
, [DeviceSerial]
, [RegisterName]
, 0 AS Contribution
FROM [RegisterCountLogs]
Where RegisterId =
(
SELECT Max ([RegisterId])
FROM [RegisterCountLogs]
WHERE DeviceSerial = 2160563829
GROUP BY [DeviceSerial]
)
能,如果你只是把'选择disinct' 2列部分的为子查询并选择在外部查询工作的呢? (即RegisterId主键,或者是(RegisterId,DeviceSerial)组合中唯一的表) – DrCopyPaste 2014-09-23 14:51:12
我的SQL技能严重生锈,您能发布查询吗? – Chris 2014-09-23 14:51:50
如果该组合具有多个“RegisterName”值,您想要哪个值? – 2014-09-23 14:51:58