Row_Number()超过某些值行

时间:2013-10-11 08:55:03

标签: tsql select sql-server-2008-r2 row-number

SELECT
        ROW_NUMBER() OVER (PARTITION BY s.ExitStateTypeId ORDER BY s.InsertDate) AS Number
        ,x.Id
        ,p.FirstName
        ,p.LastName
        ,p.PN
        ,t.Name
        ,s.ExitStateTypeId as [Status]
        ,g.Name
        ,x.OrganisationId
        ,d.Name AS Direction
        ,d.Id AS DirectionId
        ,h.Name AS Referal
        ,h.Id AS HealthOrgTypeId
        ,s.IssueDate
        ,s.InsertDate
  FROM [DB1].[dbo].[Exits] x
  INNER JOIN [DB1].[dbo].[ExitStates] s on x.Id = s.ExitId
  INNER JOIN [DB1].[dbo].[HealthOrgTypes] h on x.HealthOrgTypeId = h.Id
  INNER JOIN [DB1].[dbo].[Directions] d on x.DirectionId = d.Id
  INNER JOIN [DB1].[dbo].[HealthCards] c on x.HealthCardId = c.Id
  INNER JOIN [DB2].[pr].[TABLE] p on p.Id = c.TABLEId
  INNER JOIN [DB3].[orgs].[Organizations] g on g.Id = x.OrganisationId
  INNER JOIN [DB4].[dbo].ExitStateTypes t on t.Id = s.ExitStateTypeId
  WHERE s.Id = (SELECT MAX(es.Id) from ExitStates es WHERE es.ExitId=x.Id)
  ORDER BY s.InsertDate 

这是我的查询,但我需要的是我只希望ROW_NUMBER枚举s.ExitStateTypeId(4,7)中的那些行,对于其他行应该是 - 1。

这是不正确的查询,只是为了得到这个想法。

SELECT 
    ROW_NUMBER() OVER (PARTITION BY s.ExitStateTypeId Where s.ExitStateTypeId IN (4,7) 
                       ORDER BY s.InsertDate) AS Number
        ,x.Id
        ,p.FirstName
        ,p.LastName
        ,p.PN
        ,t.Name
        ,s.ExitStateTypeId as [Status]
        ,g.Name
        ,x.OrganisationId
        ,d.Name AS Direction
        ,d.Id AS DirectionId
        ,h.Name AS Referal
        ,h.Id AS HealthOrgTypeId
        ,s.IssueDate
        ,s.InsertDate
     FROM [DB1].[dbo].[Exits] x
     INNER JOIN [DB1].[dbo].[ExitStates] s on x.Id = s.ExitId
     INNER JOIN [DB1].[dbo].[HealthOrgTypes] h on x.HealthOrgTypeId = h.Id
     INNER JOIN [DB1].[dbo].[Directions] d on x.DirectionId = d.Id
     INNER JOIN [DB1].[dbo].[HealthCards] c on x.HealthCardId = c.Id
     INNER JOIN [DB2].[pr].[TABLE] p on p.Id = c.TABLEId
     INNER JOIN [DB3].[orgs].[Organizations] g on g.Id = x.OrganisationId
     INNER JOIN [DB4].[dbo].ExitStateTypes t on t.Id = s.ExitStateTypeId
WHERE s.Id = (SELECT MAX(es.Id) from ExitStates es WHERE es.ExitId=x.Id)
ORDER BY s.InsertDate 

所以,我想要s.ExitStateTypeId分区,只要它是4或7,其他分区为

这是我期待的数据

enter image description here

2 个答案:

答案 0 :(得分:1)

也许结合使用CASEROW_NUMBER

SELECT Number = CASE WHEN s.ExitStateTypeId NOT IN (4,7) THEN -1
        ELSE Row_number() OVER ( 
           partition BY s.exitstatetypeid 
           ORDER BY s.insertdate) END, 
       x.id, 
       p.firstname, 
       p.lastname, 
       p.pn, 
       t.name, 
       s.exitstatetypeid          AS [Status], 
       g.name, 
       x.organisationid, 
       d.name                     AS Direction, 
       d.id                       AS DirectionId, 
       h.name                     AS Referal, 
       h.id                       AS HealthOrgTypeId, 
       s.issuedate, 
       s.insertdate 
FROM   [DB1].[dbo].[exits] x 
       INNER JOIN [DB1].[dbo].[exitstates] s 
               ON x.id = s.exitid 
       INNER JOIN [DB1].[dbo].[healthorgtypes] h 
               ON x.healthorgtypeid = h.id 
       INNER JOIN [DB1].[dbo].[directions] d 
               ON x.directionid = d.id 
       INNER JOIN [DB1].[dbo].[healthcards] c 
               ON x.healthcardid = c.id 
       INNER JOIN [DB2].[pr].[table] p 
               ON p.id = c.tableid 
       INNER JOIN [DB3].[orgs].[organizations] g 
               ON g.id = x.organisationid 
       INNER JOIN [DB4].[dbo].exitstatetypes t 
               ON t.id = s.exitstatetypeid 
WHERE  s.id = (SELECT Max(es.id) 
               FROM   exitstates es 
               WHERE  es.exitid = x.id) 
ORDER  BY s.insertdate 

答案 1 :(得分:0)

获取行号,左外连接并将空行号设置为-1(如下所示,可能需要稍微修复一下,因为我没有要使用的模式):

;with RowNumber( ExitId, RowNumber )
as
(
    SELECT
        x.Id
        , ROW_NUMBER() OVER (PARTITION BY s.ExitStateTypeId ORDER BY s.InsertDate)          
      FROM 
        [DB1].[dbo].[Exits] x
        INNER JOIN [DB1].[dbo].[ExitStates] s 
         on x.Id = s.ExitId
      WHERE 
        s.Id = (SELECT MAX(es.Id) from ExitStates es WHERE es.ExitId=x.Id)
        and s.ExitStateTypeId in ( 4, 7 )
)

SELECT
        ISNULL( rn.RowNumber, -1 ) AS Number
        ,x.Id
        ,p.FirstName
        ,p.LastName
        ,p.PN
        ,t.Name
        ,s.ExitStateTypeId as [Status]
        ,g.Name
        ,x.OrganisationId
        ,d.Name AS Direction
        ,d.Id AS DirectionId
        ,h.Name AS Referal
        ,h.Id AS HealthOrgTypeId
        ,s.IssueDate
        ,s.InsertDate
  FROM [DB1].[dbo].[Exits] x
  INNER JOIN [DB1].[dbo].[ExitStates] s on x.Id = s.ExitId
  INNER JOIN [DB1].[dbo].[HealthOrgTypes] h on x.HealthOrgTypeId = h.Id
  INNER JOIN [DB1].[dbo].[Directions] d on x.DirectionId = d.Id
  INNER JOIN [DB1].[dbo].[HealthCards] c on x.HealthCardId = c.Id
  INNER JOIN [DB2].[pr].[TABLE] p on p.Id = c.TABLEId
  INNER JOIN [DB3].[orgs].[Organizations] g on g.Id = x.OrganisationId
  INNER JOIN [DB4].[dbo].ExitStateTypes t on t.Id = s.ExitStateTypeId
  left outer join RowNumber rn
    on x.Id = rn.ExitId
  WHERE s.Id = (SELECT MAX(es.Id) from ExitStates es WHERE es.ExitId=x.Id)
  ORDER BY s.InsertDate