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_NUMBERこれは私のクエリですが、(4,7) にある行のみを列挙しs.ExitStateTypeId、他の行は -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 に答える