0

shortDesc我怎样才能逐字段选择一行?我不在乎哪条线,但我只需要一条线。

我的表格摘录将是

|longDescr|shortDesc|
|---------|---------|
| First 1 | first   |
| First 2 | first   |
| First 3 | first   |
| First 4 | first   |
| Second 1| second  |
| Second 2| second  |
| Second 3| second  |
| Second 4| second  |
| Third 1 | third   |
| Third 2 | third   |
| Third 3 | third   |

我期望的是光标结果:

|longDescr|shortDesc|
|---------|---------|
| First 1 | first   |
| Second 1| second  |
| Third 1 | third   |

我真的不知道如何编写我的 SQL 查询。有任何想法吗?

4

2 回答 2

2
Select longDescr,shortDesc from
(Select Row_Number() Over(Partition By shortDesc order by shortDesc) as Rows ,*
 from TableName ) t 
where Rows=1;
于 2013-06-03T07:17:34.307 回答
1

试试这个 usingGROUP BY子句:

SELECT MIN(longDescr) AS longDescr, shortDesc
FROM MyTable
GROUP BY shortDesc

输出:

╔═══════════╦═══════════╗
║ LONGDESCR ║ SHORTDESC ║
╠═══════════╬═══════════╣
║ First 1   ║ first     ║
║ Second 1  ║ second    ║
║ Third 1   ║ third     ║
╚═══════════╩═══════════╝

看到这个 SQLFiddle

您可以根据需要使用MIN()MAX()运行。

于 2013-06-03T07:16:36.567 回答