我有下表具有重複值的表。我只想從中獲取最大長度的文本。如何在Sql Server中使用GROUP BY作爲多列時選擇最長的字符串
+----+---------------------------------+-------------------+
| ID | Text | no |
+----+---------------------------------+-------------------+
| 1 | lorem ipsum ipsum | 8955\5445 |
+----+---------------------------------+-------------------+
| 1 | ipsum | 1879668\4554\4554 |
+----+---------------------------------+-------------------+
| 1 | lorem ipsum ipsum | 5464 |
+----+---------------------------------+-------------------+
| 2 | lorem ipsum ipsum derome | 13465465\54555\45 |
+----+---------------------------------+-------------------+
| 2 | lorem ipsum derome ipsum derome | 555454 |
+----+---------------------------------+-------------------+
我的查詢:
select id,MAX(text),MAX(no) from table1 group by id
結果:
+----+--------------------------+-----------+
| id | Text | no |
+----+--------------------------+-----------+
| 1 | lorem ipsum ipsum | 8955\5445 |
+----+--------------------------+-----------+
| 2 | lorem ipsum ipsum derome | 555454 |
+----+--------------------------+-----------+
預期輸出:
+----+--------------------------+-------------------+
| id | Text | no |
+----+--------------------------+-------------------+
| 1 | lorem ipsum ipsum | 1879668\4554\4554 |
+----+--------------------------+-------------------+
| 2 | lorem ipsum ipsum derome | 13465465\54555\45 |
+----+--------------------------+-------------------+
編寫查詢 –
對於要長的文字和最大的無每個ID來實現,無關? – jarlh