我从未使用过qualify row_number()/ rank(),我有一些问题。
我在teradata中尝试此查询:
select sit_site_id
, count(distinct shp_shipment_id)
from WHOWNER.BT_SHP_SHIPMENTS
group by sit_site_id
QUALIFY RANK() OVER (PARTITION BY sit_site_id
ORDER BY count(distinct shp_shipment_id) ) = 3
但结果是:'表中没有数据'。
我希望获得前3个sit_site_id值以及更多shp_shipment_id。
我的错误在哪里?
谢谢!
答案 0 :(得分:1)
我希望获得前3个sit_site_id值以及更多shp_shipment_id
如果是这样,那么您不需要使用RANK()/ ROW_NUMBER()。相反,你可以使用TOP。您的查询应如下所示:
select TOP 3 sit_site_id
, count(distinct shp_shipment_id) shp_shipment_id_cnt
from WHOWNER.BT_SHP_SHIPMENTS
group by sit_site_id
order by shp_shipment_id_cnt DESC;
答案 1 :(得分:0)
如果你想使用qualify
,那么你可以这样做:
select sit_site_id, count(distinct shp_shipment_id)
from WHOWNER.BT_SHP_SHIPMENTS
group by sit_site_id
qualify row_number() over (order by count(distinct shp_shipment_id) desc) <= 3;
但是,我会在@ Clark的回答中推荐top
。