【问题标题】:number duplicate entries in a table表中重复条目的编号
【发布时间】:2022-08-03 21:14:56
【问题描述】:

我有一个下表。

cid oid
1 12
1 12
1 23
1 34
1 55
1 55
1 55

我尝试了以下查询。

select
   cid,
   oid,
   dense_rank() over (partition by oid order by order_date) as oid_history   
from 
    master.t1
where 
    cid = 1
order by 
    order_date asc;

得到以下输出。

cid oid oid_history
1 12 1
1 12 2
1 23 1
1 34 1
1 55 1
1 55 2
1 55 3

预期输出。

cid oid oid_history
1 12 1
1 12 1
1 23 2
1 34 3
1 55 4
1 55 4
1 55 4

谢谢:)

  • 在您的示例表中,列 order_date 不存在

标签: sql snowflake-cloud-data-platform


【解决方案1】:

忽略在您的示例表中没有列 order_date 的事实......您可以试试这个吗?

select
   cid,
   oid,
   dense_rank() over (partition by cid order by oid) as oid_history   
from 
    master.t1
where 
    cid = 1
order by 
    oid_history asc;

【讨论】:

  • partition by cidwhere cid = 1 是多余的
【解决方案2】:

像这样的东西应该工作:

选择旧的,计数(*) 来自表名 按旧分组 按旧订购

【讨论】:

    猜你喜欢
    • 2011-09-05
    • 2016-08-11
    • 2012-01-18
    • 2013-03-09
    • 2013-03-16
    • 2017-08-10
    • 2012-04-10
    • 2012-12-06
    • 1970-01-01
    相关资源
    最近更新 更多