select
*
from (
select *,
row_number() over(partition by appid order by cnt desc) as rn
from (
-- unpivot the data using dynamic SQL
select appid, "1980" as tag, [1980] as cnt from t union all
select appid, "1990", [1990] from t union all
select appid, "2_5d", [2_5d] from t union all
select appid, "2d", [2d] from t union all
select appid, "2d_fighter", [2d_fighter] from t union all
...
select appid, "last_tag", [last_tag] from t
-- finish unpivoting
) x
) y
where rn <= 3
order by appid, rn
2条答案
按热度按时间1l5u6lss1#
如果取消分割数据,查询将变得微不足道。要取消分割数据,您需要检索表元数据,并根据下面所示的逻辑组装一个大型sql。请参见下面查询中的标记。
解决方案如下所示:
8mmmxcuj2#
可以使用information\u schema.columns视图获取该表的所有列名。有了它,您可以很容易地生成一些sql语句来获取数据。
另一种选择是使用ssis或PowerBI来透视数据。