Frage

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 

This is my query, but what I need is that I want only those rows to be enumerated by ROW_NUMBER which s.ExitStateTypeId is in (4,7), for the other rows it should be -1.

Here's the incorrect query just for getting the idea.

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 

So, I want partition by s.ExitStateTypeId just when it is 4 or 7 and -1 for the others

This is data I expect

enter image description here

War es hilfreich?

Lösung

Perhaps with a combination of CASE and ROW_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 

Andere Tipps

Get row numbers, left outer join and set null row number value to -1 (like below, might have to fix it up a bit as I don't have a schema to work with):

;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 
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top