English 中文(简体)
选择不同的亚物价值
原标题:Selecting distinct substring values

我有一个类似于MAC地址的领域,因为第一部分是一个集团,第二部分是序列号。 我的田间是字母数字,5位数长,头3位是团体识别。

我需要一个问询,给我所有独特的团体身份证和第一个序列数字。 此处为抽样数据:

ID
-----
X4MCC
X4MEE
X4MFF
V21DD
8Z6BB
8Z6FF

希望产出:

ID
-----
X4MCC
V21DD
8Z6BB

我知道我可以这样做。 SlectT DISTINCT SUBSTRING(ID, 1, 3),但我不知道如何从地理学上取得第一种。

最佳回答
SELECT
   ID
FROM
   (
   SELECT
      ID,
      ROW_NUMBER() OVER (PARTITION BY SUBSTRING(ID, 1, 3) ORDER BY ID) AS rn
   FROM MyTable
   ) oops
WHERE
   rn = 1
问题回答

另一种方式似乎与gbn查询的费用相同:

SELECT MIN(id)
FROM your_table
GROUP BY SUBSTRING(id, 1, 3);




相关问题
Find the nth occurrence of substring in a string

This seems like it should be pretty trivial, but I am new at Python and want to do it the most Pythonic way. I want to find the index corresponding to the n th occurrence of a substring within a ...

Get Substring - everything before certain char

I m trying to figure out the best way to get everything before the - character in a string. Some example strings are below. The length of the string before - varies and can be any length 223232-1....

Finding a substring of multiple occurences in a string [C++]

is there any STL algorithm or a standard way of finding how many occurences of particular substring are there in a string? For example in string: How do you do at ou the string "ou" appears twice. ...

substring on html.encode

how can I limit the # of characters to display for html.encode? <%= Html.Encode(item.LastName.Substring(1,30))%> error: Index and length must refer to a location within the string.

Drawing in NStextField

If you wanted to implement a highlighting for specific substrings in a NSTextField like on the screenshot (Tweetie.app) how would you do it? :) Tweetie Link Highlighting http://qkpic.com/88c61 Thanks ...

Substring to truncate text?

I m trying to figure out how to truncate the first paragraph, and I ve tried: $div.children( ( p:eq(0) ).substring(0,100)); $div.children( ( p:eq(0) .substring(0,100))); But neither have worked... ...

热门标签