SQL服务器显示错误的行号

时间:2014-02-14 15:02:30

标签: sql sql-server

这是我的sql

SELECT * FROM  
     (
  SELECT Row_number() OVER (ORDER BY [bbajobs].JID) AS RowNumber
       ,[BBAJobs].[JID]
       ,[AccountReference] as [Acc Ref]
       ,[BBAJobs].[OEReference] as [OERef]
       ,[JobType],[JobState]
       ,[JobShippedDate]
       ,[UPSShippingNumber]
       ,[CustomerName] [Customer Name]
       ,[ContactName] [Contact Name]
       ,[Telephone]
       ,[JobDescription]
       ,[CallRem].[rem]
       ,[CallRem].[callStatus]
       ,[CallRem].[ShopRemarks]
       ,CustomerNotes
       ,ShopNotes
       ,RecievedDate
       ,UserName 
   FROM dbo.BBAJobs LEFT OUTER JOIN dbo.CallRem 
ON dbo.BBAJobs.JID = dbo.CallRem.jid) x 
WHERE 1<2  
AND x.jid IN (SELECT CONVERT(VARCHAR, data) AS [JID] 
              FROM dbo.Split1('33180,33265,33047', ','))                  
AND RowNumber BETWEEN 1 AND 20

当我执行上面的sql时,rwo数字显示不像1,2,3

当我排除这一行

AND x.jid IN (SELECT CONVERT(VARCHAR, data) AS [JID] 
              FROM dbo.Split1('33180,33265,33047', ','))

然后显示右行号。不排除该行&amp;位置不能行号可以显示吗?请帮忙。感谢

4 个答案:

答案 0 :(得分:1)

这样写的方式,子查询'x'返回一个有序的数据集。返回该数据集后,以下行将其过滤到与其匹配的行:

AND x.jid IN (SELECT CONVERT(VARCHAR, data) AS [JID] FROM dbo.Split1('33180,33265,33047', ','))

在'x'子查询中移动此条件应产生所需的结果。

SELECT * FROM  
(SELECT Row_number() OVER (ORDER BY [bbajobs].JID) AS RowNumber,                    
[BBAJobs].[JID],[AccountReference] as [Acc Ref],[BBAJobs].[OEReference] as [OERef],[JobType],[JobState],
[JobShippedDate],[UPSShippingNumber],[CustomerName] [Customer Name],[ContactName] [Contact Name],[Telephone],
[JobDescription],[CallRem].[rem],[CallRem].[callStatus],[CallRem].[ShopRemarks],
CustomerNotes,ShopNotes,RecievedDate,UserName FROM dbo.BBAJobs LEFT OUTER JOIN dbo.CallRem 
ON dbo.BBAJobs.JID = dbo.CallRem.jid
AND [bbajobs].jid IN (SELECT CONVERT(VARCHAR, data) AS [JID] FROM dbo.Split1('33180,33265,33047', ','))) x 
WHERE 1<2   
AND RowNumber BETWEEN 1 AND 20

答案 1 :(得分:1)

如果要从子查询中选择20行,则需要确保在对行进行编号之前/同时应用过滤行的所有条件。在子查询中移动条件:

SELECT * 
FROM   (SELECT Row_number() 
                 OVER ( ORDER BY [bbajobs].jid ) AS RowNumber, 
               [bbajobs].[jid], 
               [accountreference]          AS [Acc Ref], 
               [bbajobs].[oereference]     AS [OERef], 
               [jobtype], 
               [jobstate], 
               [jobshippeddate], 
               [upsshippingnumber], 
               [customername]              [Customer Name], 
               [contactname]               [Contact Name], 
               [telephone], 
               [jobdescription], 
               [callrem].[rem], 
               [callrem].[callstatus], 
               [callrem].[shopremarks], 
               customernotes, 
               shopnotes, 
               recieveddate, 
               username 
        FROM   dbo.bbajobs 
               LEFT OUTER JOIN dbo.callrem 
                            ON dbo.bbajobs.jid = dbo.callrem.jid
        WHERE dbo.bbajobs.jid IN (SELECT CONVERT(VARCHAR, data) AS [JID] 
                     FROM   dbo.Split1('33180,33265,33047', ',')) ) x 
WHERE  1 < 2 
       AND rownumber BETWEEN 1 AND 20 

答案 2 :(得分:0)

在应用ID的where子句之前计算行号。

在括号内的语句中移动ID的where子句,如下所示 -

SELECT * FROM  
(
SELECT Row_number() OVER (ORDER BY [bbajobs].JID) AS RowNumber,                    
[BBAJobs].[JID],[AccountReference] as [Acc Ref],[BBAJobs].[OEReference] as [OERef],[JobType],[JobState],
[JobShippedDate],[UPSShippingNumber],[CustomerName] [Customer Name],[ContactName] [Contact Name],[Telephone],
[JobDescription],[CallRem].[rem],[CallRem].[callStatus],[CallRem].[ShopRemarks],
CustomerNotes,ShopNotes,RecievedDate,UserName 
FROM dbo.BBAJobs LEFT OUTER JOIN dbo.CallRem 
ON dbo.BBAJobs.JID = dbo.CallRem.jid
WHERE [bbajobs].jid IN (SELECT CONVERT(VARCHAR, data) AS [JID] FROM dbo.Split1('33180,33265,33047', ','))     
) 
x 
WHERE 1<2            
AND RowNumber BETWEEN 1 AND 20

未经测试,但应该可以使用。

答案 3 :(得分:0)

试试这个...过滤掉你内部查询中的行

SELECT * FROM  
     (
  SELECT Row_number() OVER (ORDER BY [bbajobs].JID) AS RowNumber
       ,[BBAJobs].[JID]
       ,[AccountReference] as [Acc Ref]
       ,[BBAJobs].[OEReference] as [OERef]
       ,[JobType],[JobState]
       ,[JobShippedDate]
       ,[UPSShippingNumber]
       ,[CustomerName] [Customer Name]
       ,[ContactName] [Contact Name]
       ,[Telephone]
       ,[JobDescription]
       ,[CallRem].[rem]
       ,[CallRem].[callStatus]
       ,[CallRem].[ShopRemarks]
       ,CustomerNotes
       ,ShopNotes
       ,RecievedDate
       ,UserName 
   FROM dbo.BBAJobs LEFT OUTER JOIN dbo.CallRem 
   ON dbo.BBAJobs.JID = dbo.CallRem.jid
   WHERE [BBAJobs].[JID] IN 
                          (SELECT CONVERT(VARCHAR, data) AS [JID] 
                           FROM dbo.Split1('33180,33265,33047', ',')) 
   ) x 
WHERE 1<2                   
AND RowNumber BETWEEN 1 AND 20