鉴于我有这个数据集。
Player Team Date_Played
John Smith New York 2/25/2014
Joe Smith New York 2/25/2014
Steve Johnson New York 2/25/2014
Steph Curry Orlando 2/25/2014
Frank Anthony Orlando 2/26/2014
Brian Smith Bulls 2/26/2014
Steve Johnson Bulls 2/27/2014
Steph Curry Bulls 2/28/2014
Ben Smith Bulls 3/28/2014
我想知道如何编写一个返回每个团队玩家数量一半的查询。我希望它看起来像这样:
Player Team Date_Played
John Smith New York 2/25/2014
Joe Smith New York 2/25/2014
Steph Curry Orlando 2/25/2014
Brian Smith Bulls 2/26/2014
Steve Johnson Bulls 2/27/2014
我考虑过尝试使用LIMIT或TOP命令,但我不知道如何根据特定列中的不同值编写查询来限制结果。 有任何想法吗?这可能吗?
答案 0 :(得分:1)
您可以使用窗口功能。我会使用row_number()
和count()
:
select t.*
from (select t.*, count(*) over (partition by team) as cnt,
row_number() over (partition by team order by team) as seqnum
from t
) t
where seqnum <= 0.5 * cnt;
还有其他使用单一功能的方法,例如ntile()
:
select t.*
from (select t.*,
ntile(2) over (partition by team order by team) as tile
from t
) t
where tile = 1;
或percentile()
或其他人。