我现在的sql是这样的 如何让结果中每一条的顺序都按照ranking的大小排序呢 SELECT srcroleid, COLLECTLIST(dstopenid) AS…
- User
我现在的sql是这样的 如何让结果中每一条的顺序都按照ranking的大小排序呢 SELECT src_roleid, COLLECT_LIST(dst_openid) AS dst_openids, COLLECT_LIST(dst_roleid) AS dst_roleids, COLLECT_LIST(dst_areaid) AS dst_areaids, COLLECT_LIST(dst_platid) AS dst_platids, COLLECT_LIST(friendtype) AS dst_friendtypes, COLLECT_LIST(dst_plat_nickname) AS dst_plat_nicknames, COLLECT_LIST(ranking) AS rankings FROM ${t1} GROUP BY src_roleid LIMIT 500
- Assistant
要使
COLLECT_LIST聚合函数收集的列表按照ranking排序,您可以在COLLECT_LIST函数中添加ORDER BY ranking子句。请注意,这取决于您所使用的数据库是否支持在聚合函数中使用ORDER BY子句。以下是修改后的 SQL 查询:
sqlSELECT src_roleid, COLLECT_LIST(dst_openid ORDER BY ranking) AS dst_openids, COLLECT_LIST(dst_roleid ORDER BY ranking) AS dst_roleids, COLLECT_LIST(dst_areaid ORDER BY ranking) AS dst_areaids, COLLECT_LIST(dst_platid ORDER BY ranking) AS dst_platids, COLLECT_LIST(friendtype ORDER BY ranking) AS dst_friendtypes, COLLECT_LIST(dst_plat_nickname ORDER BY ranking) AS dst_plat_nicknames, COLLECT_LIST(ranking ORDER BY ranking) AS rankings FROM ${t1} GROUP BY src_roleid LIMIT 500这样,每个
COLLECT_LIST收集的列表都会按照ranking的顺序排序。如果您需要按照降序排序,可以将ORDER BY ranking DESC。注意:确保您的数据库支持在聚合函数中使用
ORDER BY子句。例如,某些版本的 Hive、Spark SQL 等支持此功能。如果您的数据库不支持,您可能需要采用其他方法,例如使用子查询或窗口函数来实现排序。