簡體   English   中英

SQL區別但查詢區別不大

[英]SQL Distinct but not so distinct query

也許我把這個復雜化了...

我只需要一個“簡單”查詢來選擇一列,但只返回不同的行。
這是關鍵,如果它們相似而不是完全匹配,則需要排除它們。

例如,這是我當前的查詢

select distinct channel from audio_srcs order by channel

訣竅是,這將返回如下內容

Channel
--------
1001
1003
1003 <Description@unitid>
1004
1004 <Description@unitid>

我想要顯示1003行中的唯一一行和1004行之一(例如)。 我不在乎它返回哪一個。

我想要的最終結果將如下所示:

Channel
---------
1001
1003
1004
etc.
SELECT distinct left(Channel, 4) as Channel  --  or maybe first 5 characters
 from audio_srcs
 order by channel 

我將分隔符作為區別。 您只需要列中的第一個標記:

SELECT distinct left(Channel,
                     (case when charindex(' ', channel) <= 0 then len(channel)
                           else charindex(' ', channel)
                      end
                     )
                    ) as Channel  --  or maybe first 5 characters
from audio_srcs
order by channel 

我已經使用了SQL Server版本的字符串函數。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM