在SQL 2000中模拟ROW_NUMBER并结合左外连接

时间:2015-08-10 08:03:01

标签: tsql sql-server-2000

我阅读了一些文章并阅读了过去的帖子,但似乎无法让我的查询正常工作。

基本上我试图在带有左外连接的选择查询上添加RowNumber列。

奇怪的是,如果我用一个字段返回查询,一切正常。

原始TSQL:

SELECT TOP 10 a.ACCOUNTID, 
              ACCOUNT, 
              a.LASTHISTORYBY, 
              a.LASTHISTORYDATE, 
              a.CreateDate, 
              a.[Type], 
              [Status], 
              Email, 
              Fax, 
              MainPhone, 
              WebAddress, 
              adr.ADDRESSID, 
              EntityID, 
              ADDRESS1, 
              ADDRESS2, 
              ADDRESS3, 
              ADDRESS4, 
              City, 
              adr.[State], 
              Postalcode, 
              County, 
              Country, 
              IsPrimary, 
              adr.CreateDate, 
              adr.CreateUser, 
              adr.ModifyDate, 
              adr.ModifyUser, 
              htaccount.SCHOOLCLOSED
FROM sysdba.ACCOUNT a 
LEFT OUTER JOIN sysdba.[ADDRESS] adr ON a.ADDRESSID = adr.ADDRESSID 
LEFT OUTER JOIN sysdba.[HT_ACCOUNT] htaccount ON a.ACCOUNTID = htaccount.ACCOUNTID

Obviusoly工作得很好但是尝试添加类似的RowNumber功能是一种绝对的痛苦。当我运行新脚本时,RowNumber似乎返回到9k值。我也看到了重复的值。

带有RowNumber的TSQL:

SELECT TOP 300 RowNumber, 
               a.ACCOUNTID, 
               a.LASTHISTORYBY, 
               a.LASTHISTORYDATE
  FROM (SELECT ACCOUNTID, 
               LASTHISTORYBY, 
               LASTHISTORYDATE, 
               (SELECT count(*) FROM sysdba.ACCOUNT AS t1 WHERE t1.ACCOUNTID <= t2.ACCOUNTID) AS RowNumber FROM sysdba.ACCOUNT AS t2) as a
  --LEFT OUTER JOIN sysdba.[ADDRESS] adr ON ADDRESSID = adr.ADDRESSID 
  --LEFT OUTER JOIN sysdba.[HT_ACCOUNT] htaccount ON a.ACCOUNTID = htaccount.ACCOUNTID
  WHERE RowNumber > 50 AND  RowNumber < 100

我评论了左外连接,因为我正在测试,但理想情况下需要连接。

非常感谢任何帮助!

此致

1 个答案:

答案 0 :(得分:0)

您的“行数”应包含所有相关条件作为主要查询(意味着应选择左连接后)。
试试这个:

CREATE VIEW vw_Base 
AS

SELECT        a.ACCOUNTID, 
              ACCOUNT, 
              a.LASTHISTORYBY, 
              a.LASTHISTORYDATE, 
              a.CreateDate, 
              a.[Type], 
              [Status], 
              Email, 
              Fax, 
              MainPhone, 
              WebAddress, 
              adr.ADDRESSID, 
              EntityID, 
              ADDRESS1, 
              ADDRESS2, 
              ADDRESS3, 
              ADDRESS4, 
              City, 
              adr.[State], 
              Postalcode, 
              County, 
              Country, 
              IsPrimary, 
              adr.CreateDate, 
              adr.CreateUser, 
              adr.ModifyDate, 
              adr.ModifyUser, 
              htaccount.SCHOOLCLOSED
FROM sysdba.ACCOUNT a 
LEFT OUTER JOIN sysdba.[ADDRESS] adr ON a.ADDRESSID = adr.ADDRESSID 
LEFT OUTER JOIN sysdba.[HT_ACCOUNT] htaccount ON a.ACCOUNTID = htaccount.ACCOUNTID
GO

SELECT *
FROM 
(
    SELECT *, (
               SELECT COUNT(*) 
               FROM vwBase t2
               WHERE t2.ACCOUNTID <= t1.ACCOUNTID) As RowNumber
    FROM vwBase t1
) iq
WHERE RowNumber > 50 AND RowNumber < 100

此外,您在两个select语句中都缺少order by子句。无需按顺序选择前x行。