2016-02-25 70 views
0

我有一個NAMES表,其中列idname獲取與子查詢給出的最小值匹配的行

查詢是根據單詞結尾數最少的字母獲取結果。例如:亨利,哈里,約翰,弗雷德裏克是我在桌上的名字。 n和k是隻有1個字作爲結束字母的字母。

這是我迄今所做..

select substr(name, -1, 1) as character, 
     count(*) as wordscount 
    from names    
group by substr(name, -1, 1) 
order by count(*); 

此獲取我的結果如下

character wordscount 
----------- ------------ 
n   1 
k   1 
y   2 

我的查詢應該取像

character wordscount 
----------- ------------ 
n   1 
k   1 

我不結果由於可能有多個字母的詞數相同,所以不想使用rownum = 1

select * 
    from (select substr(name, -1, 1) as character, 
       count(*) as wordsCount 
      from names    
     group by substr(name, -1, 1) 
     order by count(*)) 
where rownum = 1; 

我不想使用上面的查詢,因爲這會從我的結果中省略k 1

回答

0

你想找?

select substr(name, -1, 1) as character, count(*) as wordscount from names    
group by substr(name, -1, 1) having count(*)= ( 
    select min(count(*)) from names    
    group by substr(name, -1, 1) 
) 

或不用額外的包裝物具有:

select * from (
select substr(name, -1, 1) as character, count(*) as wordscount from names    
group by substr(name, -1, 1)) A where wordscount = ( 
    select min(count(*)) from names    
    group by substr(name, -1, 1) 
) 
+0

這不正是我需要的。但是有沒有別的辦法可以「有」 –

+0

@codenoob當然。我添加了它。 – dcieslak

+0

我無法讚揚我的低聲譽。非常感謝你的幫助 –