MS-Access选择第一行非唯一ID

时间:2017-06-02 13:47:53

标签: sql ms-access unique

如何在MS-Access中选择具有相同非唯一ID的两个多行的第一个数据集?

我没有使用非唯一ID设计此混乱,而ID之外的其他字段是随机字符串值,因此可能是唯一的或重复的。

说我有输入表

Tab_INPUT
--------------------------
| ID | VAL1 | VAL2 | ... | 
- - - - - - - - - - - - - 
| 1  | "A"  | "1a" | ... |
| 2  | "B"  | "2b" | ... |
| 2  | "C"  | "2a" | ... |
| 3  | "D"  | "3c" | ... |
| 3  | "E"  | "3b" | ... |
| 3  | "F"  | "3a" | ... |
| 4  | "G"  | "4a" | ... | 
--------------------------

我现在想为每个不同的ID选择第一行,即即

Tab_OUTPUT (DESIRED)
--------------------------
| ID | VAL1 | VAL2 | ... |
- - - - - - - - - - - - - 
| 1  | "A"  | "1a" | ... |
| 2  | "B"  | "2b" | ... |
| 3  | "D"  | "3c" | ... |
| 4  | "G"  | "4a" | ... |
--------------------------

我不能使用像

这样的东西
SELECT ID, Min(VAL1), Min(Val2) FROM Tab_INPUT GROUP BY ID

因为这会弄乱自己的行

Tab_OUTPUT (WRONG)
--------------------------
| ID | VAL1 | VAL2 | ... |
- - - - - - - - - - - - - 
| 1  | "A"  | "1a" | ... |
| 2  | "B"  | "2a" | ... |
| 3  | "D"  | "3a" | ... |
| 4  | "G"  | "4a" | ... |
--------------------------

我需要行保持原样,只需选择具有唯一ID的第一行

1 个答案:

答案 0 :(得分:2)

尝试使用First代替Min

SELECT ID, First(VAL1) AS Val1, First(Val2) AS Val2
FROM Tab_INPUT 
GROUP BY ID