1
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_NUMBERs.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 和 -1 的时候

这是我期望的数据

在此处输入图像描述

4

2 回答 2

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 
于 2013-10-11T09:01:42.877 回答
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 
于 2013-10-11T09:36:42.910 回答