简体   繁体   English

Sybase SQL:分区上的行号

[英]Sybase SQL: row number over a partition

please could you help me?你能帮帮我吗? I am trying to understand how Sybase ase SQL works.我试图了解 Sybase ase SQL 的工作原理。

Basically I have a query like this:基本上我有这样的查询:

select ClientId,StartDate,EndDate from TableName

And I need to create a temporary table where I will have我需要创建一个临时表

ClientID = A, StartDate 20180101, 1
ClientID = A, StartDate 20190101, 2
ClientID = A, StartDate 20200101, 3
ClientID = B, StartDate 20180101, 1
ClientID = B, StartDate 20190101, 2
ClientID = C, StartDate 20190101, 1
ClientID = C, StartDate 20200101, 2

Do you know how to create it?你知道如何创建它吗? I looked around but I could not find any suitable solution (rownum does not work and creating我环顾四周,但找不到任何合适的解决方案(rownum 不起作用并创建

SELECT row_number = identity(32),t.*
INTO #TempTable

does not work (in this case I get [1,2,3,4,5,6,7] instead of [1,2,3,1,2,1,2]不起作用(在这种情况下,我得到 [1,2,3,4,5,6,7] 而不是 [1,2,3,1,2,1,2]

Thank you for your help!谢谢您的帮助!

OP hasn't provided the table DDL, nor a sample set of insert statements, so a few assumptions: OP 没有提供表格 DDL,也没有提供一组insert语句的示例,所以有一些假设:

  • StartDate is a varchar() (for the sake of this answer; should be able to switch to date , datetime or datetime without any issues) StartDate是一个 varchar() (为了这个答案;应该能够切换到datedatetimedatetime没有任何问题)
  • EndDate is not required in the desired output (per OP's example output) EndDate在所需的 output 中不需要(根据 OP 的示例输出)
  • the combination (ClientId, StartDate) is unique (otherwise the proposed answer - below - will not generate the desired results)组合(ClientId, StartDate)是唯一的(否则建议的答案 - 下面 - 不会产生所需的结果)

Sample data:样本数据:

create table clients
(ClientId       varchar(10)     null
,StartDate      varchar(10)     null
,EndDate        varchar(20)     null
)
go

insert into clients values ('A', '20180101', null)
insert into clients values ('A', '20190101', null)
insert into clients values ('A', '20200101', null)

insert into clients values ('B', '20180101', null)
insert into clients values ('B', '20190101', null)

insert into clients values ('C', '20190101', null)
insert into clients values ('C', '20200101', null)
go

As @Impaler has mentioned, Sybase ASE does not support 'window functions' so we need to get a bit creative.正如@Impaler 所提到的,Sybase ASE 不支持“窗口函数”,因此我们需要有点创意。

One idea using a self join:使用自联接的一个想法:

select  c1.ClientId,
        c1.StartDate,
        count(*) as num

from    clients c1
join    clients c2

on      c1.ClientId   = c2.ClientId
and     c1.StartDate >= c2.StartDate

group by c1.ClientId, c1.StartDate
order by 1,2
go

 ClientId   StartDate  num
 ---------- ---------- -----------
 A          20180101             1
 A          20190101             2
 A          20200101             3
 B          20180101             1
 B          20190101             2
 C          20190101             1
 C          20200101             2

NOTE: This query may perform poorly for larger data sets and/or where there are no useful indexes, ymmv...注意:对于较大的数据集和/或没有有用的索引的情况下,此查询可能表现不佳,ymmv...



Demonstrating what happens if the (ClientId, StartDate) pair is not unique...演示如果(ClientId, StartDate)对不是唯一的会发生什么...

Assume our data set looks like:假设我们的数据集如下所示:

insert into clients values ('A', '20180101', null)
insert into clients values ('A', '20190101', null)
insert into clients values ('A', '20200101', null)

insert into clients values ('B', '20180101', null)
insert into clients values ('B', '20190101', null)   -- duplicate (ClientId, StartDate)
insert into clients values ('B', '20190101', null)   -- duplicate (ClientId, StartDate)

insert into clients values ('C', '20190101', null)
insert into clients values ('C', '20200101', null)
go

The proposed query generates:建议的查询生成:

 ClientId   StartDate  num
 ---------- ---------- -----------
 A          20180101             1
 A          20190101             2
 A          20200101             3

 B          20180101             1
 B          20190101             6     -- oops, only 2 rows for 'B' and the wrong 'num' value for this row

 C          20190101             1
 C          20200101             2

If the proposed query does not work in OP's environment it may be necessary for OP to provide a minimal, reproducible example ;如果建议的查询在 OP 的环境中不起作用,则 OP 可能需要提供一个最小的、可重现的示例 in particular, provide a sample create table and insert into statements to adequately demonstrate the real data set.特别是,提供一个示例create tableinsert into语句,以充分展示真实的数据集。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM