替代Sybase中的窗口函数

时间:2016-02-28 11:23:11

标签: sql sybase-ase rank window-functions

我有以下查询

  SELECT M_PILLAR,
       M_MXCOUNTRY,
       M_LIMIT_NETEXPOSURE,
       M_LIMIT_CURRENCY_NETEXPOSURE,
       M_MAXUTILIZATION_NETEXPOSURE,
       M_AVAILABLE_NETEXPOSURE,
       M_GROUP,
       M_ENTITIES,
       M_COUNTERPARTIES,
       rank() OVER (partition BY M_COUNTERPARTIES,M_PILLAR,M_MAXEXPOSURE_NETEXPOSURE) AS test
FROM X_LP_PSR_REP

但看起来我的Sybase DB不支持窗口功能。

我尝试了以下内容:

SELECT M_PILLAR,
   M_MXCOUNTRY,
   M_LIMIT_NETEXPOSURE,
   M_LIMIT_CURRENCY_NETEXPOSURE,
   M_MAXUTILIZATION_NETEXPOSURE,
   M_AVAILABLE_NETEXPOSURE,
   M_GROUP,
   M_ENTITIES,
   M_COUNTERPARTIES,
   rank() OVER (ORDER BY M_COUNTERPARTIES,M_PILLAR,M_MAXEXPOSURE_NETEXPOSURE) AS test FROM X_LP_PSR_REP

用“order by”替换“partition by”也没有用。

请告知如何继续。

我正在使用Sybase ASE

确切的版本是:

Adaptive Server Enterprise / 15.7.0 / EBF 21207 SMP SP50 / P / Solaris AMD64 / OS 5.10 / ase157sp5x / 3284/64位/ FBO / Thu Jul 11​​ 07:19:59 2013

1 个答案:

答案 0 :(得分:2)

您可以使用相关子查询(尽管效率低得多)来执行此操作。对于rank()(使用第二个使用ORDER BY且语法正确的示例),如下所示:

SELECT r.*,
       (SELECT 1 + COUNT(*)
        FROM X_LP_PSR_REP r2
        WHERE (r2.M_COUNTERPARTIES < r.M_COUNTERPARTIES) OR
              (r2.M_COUNTERPARTIES = r.M_COUNTERPARTIES AND r2.M_PILLAR < r.M_PILLAR) OR
              (r2.M_COUNTERPARTIES = r.M_COUNTERPARTIES AND r2.M_PILLAR = r.M_PILLAR AND r2.M_MAXEXPOSURE_NETEXPOSURE < r.M_MAXEXPOSURE_NETEXPOSURE)
       ) as test
FROM X_LP_PSR_REP r;