Sybase替代DENSE_RANK()OVER(PARTITION BY

时间:2017-07-03 11:53:35

标签: sql sybase-ase window-functions

我有一个下面的SQL查询,需要将其转换为Sybase。

SELECT prd_name, DENSE_RANK() OVER (PARTITION BY prd_cat ORDER BY createddt) FROM product

表脚本:

CREATE table product(prd_name varchar(10),
                     prd_cat varchar(10), 
                     createddt datetime)
INSERT INTO product values('Product 1', 'Toy', CONVERT(DATE,'2017-05-30'))
INSERT INTO product values('Product 2', 'Toy', CONVERT(DATE,'2017-05-31'))
INSERT INTO product values('Product 3', 'Toy', CONVERT(DATE,'2017-05-31'))
INSERT INTO product values('Product 4', 'Toy1', CONVERT(DATE,'2017-05-29'))

版本:Adaptive Server Enterprise / 15.7

1 个答案:

答案 0 :(得分:0)

某些版本的Sybase支持窗口功能。我假设你没有这样的版本。

您可以将其替换为相关子查询:

SELECT p.prd_name,
       (SELECT COUNT(DISTINCT createddt)
        FROM product p2
        WHERE p2.prd_cat = p.prd_cat AND p2.createddt < p.createddt
       )
FROM product p;