问题描述
考虑像这样的桌子
我需要生成一个结果集,其中包含按视图计数排序的家庭 ID(例如 1、2、3 和 4)子集的前两 (2) 行.我想做类似的事情
I need to generate a result set with the top two(2) rows for a subset of family ids (say, 1,2,3 and 4) ordered by view count. I'd like to do something like
但是,当然,order by 以这种方式在 union all 上下文中无效.有什么建议?我知道我可以运行一组 4 个查询,将结果存储到临时表中并选择该临时表的内容作为最终结果,但我宁愿尽可能避免使用临时表.
but, of course, order by isn't valid in a union all context in this manner. Any suggestions? I know I could run a set of 4 queries, store the results into a temp table and select the contents of that temp as the final result, but I'd rather avoid using a temp table if possible.
注意:在实际应用中,每个family id的记录数是不确定的,view_counts也不是固定的,如上例所示.
Note: in the real app, the number of records per family id is indeterminate, and the view_counts are also not fixed as they appear in the above example.
推荐答案
你可以试试这样的
这篇关于联合和订购的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!