我正在编写一个内部有LEFT OUTER JOIN
的存储过程。现在我需要将所有具有不同外键总数的行返回到nth(假设为10)!
例如,假设我的表格如下:
+----+---------------+----------------+--------------+---------------+
| FK | Name | UserName | Asignee | Comment |
+----+---------------+----------------+--------------+---------------+
| 31 | Test Agent... | Manny Virk | Sumit Sharda | Test Agent... |
| 31 | Test Agent... | Manny Virk | Manny Virk | Test Agent... |
| 32 | Test Agent... | Manny Virk | Gary Pannu | test |
| 32 | Test Agent... | Manny Virk | Sunny Tiwana | test |
| 32 | Test Agent... | Manny Virk | Manny Virk | test |
| 33 | Test Agent... | Rachel Stalony | Gary Pannu | test |
| 33 | Test Agent... | Rachel Stalony | Sunny Tiwana | test |
| 33 | Test Agent... | Rachel Stalony | Manny Virk | test |
| 34 | Test Agent... | Manny Virk | Gary Pannu | test |
| 34 | Test Agent... | Manny Virk | Sunny Tiwana | test |
| 34 | Test Agent... | Manny Virk | Manny Virk | test |
| 35 | Test TeamW... | Manny Virk | Sunny Tiwana | adsf asdf ... |
| 35 | Test TeamW... | Manny Virk | Manny Virk | adsf asdf ... |
| 36 | New Agent ... | Manny Virk | Sumit Sharda | Test comme... |
| 36 | New Agent ... | Manny Virk | Sunny Tiwana | Test comme... |
| 36 | New Agent ... | Manny Virk | Manny Virk | Test comme... |
| 37 | Test TeamW... | Manny Virk | Sunny Tiwana | Some comme... |
| 37 | Test TeamW... | Manny Virk | Manny Virk | Some comme... |
| 38 | qwer qwer.... | Manny Virk | Sunny Tiwana | some new c... |
| 38 | qwer qwer.... | Manny Virk | Manny Virk | some new c... |
| 42 | asdfsafdsa... | Manny Virk | Gary Pannu | NULL |
| 42 | asdfsafdsa... | Manny Virk | Manny Virk | NULL |
| 43 | 20161214 J... | Manny Virk | Gary Pannu | My Test co... |
| 43 | 20161214 J... | Manny Virk | Sunny Tiwana | My Test co... |
| 41 | Happy Wedd... | Manny Virk | Gary Pannu | Svi togeth... |
| 41 | Happy Wedd... | Manny Virk | Manny Virk | Svi togeth... |
| 44 | Call all l... | Manny Virk | Sumit Sharda | Call all l... |
| 44 | Call all l... | Manny Virk | Manny Virk | Call all l... |
| 45 | Reperition... | Manny Virk | Gary Pannu | NULL |
| 45 | Reperition... | Manny Virk | Manny Virk | NULL |
| 46 | Some Task | Manny Virk | Sumit Sharda | Some Task ... |
| 46 | Some Task | Manny Virk | Manny Virk | Some Task ... |
| 47 | asdf | Manny Virk | Sumit Sharda | 6twdfwu |
| 47 | asdf | Manny Virk | Manny Virk | 6twdfwu |
| 48 | Test Daily... | Manny Virk | Sumit Sharda | Test Daily... |
| 48 | Test Daily... | Manny Virk | Manny Virk | Test Daily... |
| 49 | Test Weekl... | Manny Virk | Sumit Sharda | Test Weekl....|
+----+---------------+----------------+--------------+---------------+
我希望程序能够将所有行返回到FK = 43,包括所有FK = 43,因为这是最后的第10个不同的FK!
有人可以帮我写这个SQL吗?
答案 0 :(得分:1)
在标准SQL中执行此操作的一种方法是:
select t.*
from t
where t.fk in (select t2.fk
from t t2
group by t2.fk
order by t2.fk
fetch first 10 rows only
);
确切的语法可能因数据库而异,尤其是fetch first
子句。
答案 1 :(得分:0)
select
FK,Name,UserName,Asignee,Comment
from
(
select
FK,Name,UserName,Asignee,Comment
,dense_rank() over (order by FK) dr
from table
) a
where dr<=10