處理領帶我有這樣的數據集:邏輯用於與骨料
| ID | TYPE | PERCENT |
------|------|---------|
| 123 | A | 0.5 |
| 123 | B | 0.5 |
| 456 | A | 0.7 |
| 456 | B | 0.3 |
| 789 | A | 1 |
我想以下結果:
| ID | TYPE | PERCENT |
------|------|---------|
| 123 | A | 0.5 |
| 456 | A | 0.7 |
| 789 | A | 1 |
即,獲得MAX(percent)
每個id
和相應type
。
我目前使用
SELECT ...
FROM
(SELECT [id], MAX([percent]) AS [p]
FROM [highest]
GROUP BY [id]) a
LEFT JOIN [highest] b
ON b.[id] = a.[id]
AND b.[percent] = a.[p]
並獲得
| ID | P | TYPE | PERCENT |
--- --|-----|------|---------|
| 123 | 0.5 | A | 0.5 |
| 123 | 0.5 | B | 0.5 |
| 456 | 0.7 | A | 0.7 |
| 789 | 1 | A | 1 |
那麼你的問題是什麼?每個ID只需要一個記錄? –
@db_brad正確。 – Kermit