您可以通過
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