2

我正在使用支持 SQL92 的 OLEDB 接口查询数据系统。我的查询问题等同于这里解决的问题:SQL Query to find early date取决于 column value changes,但是那里提供并复制下面的解决方案对于 SQL92 来说太高级了:

SELECT JobCodeId, MIN(LastEffectiveDate) AS mindate 
FROM    ( 
        SELECT  *, 
                prn - rn AS diff 
        FROM    ( 
                SELECT  *, 
                        ROW_NUMBER() OVER (PARTITION BY JobCodeID  
                                    ORDER BY LastEffectiveDate) AS prn, 
                        ROW_NUMBER() OVER (ORDER BY LastEffectiveDate) AS rn 
                FROM    @tmp 
                ) q 
        ) q2 
GROUP BY 
        JobCodeId, diff 
ORDER BY 
        mindate 

该解决方案的 SQL92 兼容版本会是什么样子?

4

1 回答 1

1

利用:

SELECT JobCodeId, 
       MIN(LastEffectiveDate) AS mindate 
  FROM (SELECT  *, 
                prn - rn AS diff 
           FROM (SELECT *,  
                        (SELECT CASE WHEN COUNT(*) = 0 THEN 1 ELSE COUNT(*) END
                           FROM @tmp t
                          WHERE t.JobCodeID = r.JobCodeID
                            AND t.LastEffectiveDate <= x.LastEffectiveDate) AS prn,
                        (SELECT COUNT(*) + 1
                           FROM @tmp t
                          WHERE t.LastEffectiveDate <= x.LastEffectiveDate) AS rn
                   FROM @tmp x) q 
       ) q2 
GROUP BY JobCodeId, diff 
ORDER BY mindate
于 2010-03-01T20:38:29.460 回答