本文介紹了在查詢中獲取唯一結果的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!
問題描述
我相信這對于 t-sql 大師來說會非常簡單.
Im sure this will be pretty simple for a t-sql guru.
我從表格中得到以下結果
I have the following result from a table
idMain IdSecondary TextValue
1 1 text1
1 2 text2
2 5 text3
2 6 text5
我只想獲得 idMain 的第一次出現.
And I want to obtain the first occurence of the idMain only.
結果應該是這樣的.
idMain idSecondary TextValue
1 1 text1
2 5 text3
我怎樣才能做到這一點?
How can I achieve this?
推薦答案
您應該能夠加入子查詢,如下例所示.這假設第一"是指最低的 idSecondary
:
You should be able to join with a sub query, as in the following example. This assumes that by "first" you mean the lowest idSecondary
:
SELECT t.idMain, sub_t.minSecondary, t.textValue
FROM your_table t
JOIN (SELECT idMain, MIN(idSecondary) minSecondary
FROM your_table
GROUP BY idMain) sub_t ON (sub_t.idMain = t.idMain);
這篇關于在查詢中獲取唯一結果的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!
【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!