SQL连接与同一组中的上一行

时间:2015-03-30 08:21:04

标签: sql tsql

我的SQL查询有问题。我正在尝试创建一个将数据放到表中的脚本。我有按日期排序的新值临时表,我将搜索之前必须具有相同GroupId,TransactionId和FieldTypeId但ClaimModificationId必须更小的值。

下面我写一个脚本,如果没有错误抛出就行是好的:

where m2.ClaimModificationId < m1.ClaimModificationId

sql不允许对m1表进行引用。有没有一种方法可以区别地写出这种情况?

create table #modifications (
  [ClaimModificationId] INT IDENTITY(1,1),
  [GroupId] INT,
  [FieldTypeId] INT,
  [FieldName] NVARCHAR(255),
  [TransactionId] INT,
  [NewValue] NVARCHAR(255),
  [UserEmail] NVARCHAR(255),
  [ModificationDate] DATETIME,
  [Action] NVARCHAR(50))

select top 10 
     m1.[GroupId], 
     m1.[FieldTypeId], 
     m1.[FieldName], 
     m1.[TransactionId], 
     cm4.[NewValue] as OldValue, 
     m1.[NewValue], 
     m1.[UserEmail], 
     m1.[ModificationDate], 
     m1.[Action]
from #modifications m1 
left join (
     select max(m2.ClaimModificationId) as ClaimModificationId, m2.[GroupId], m2.[FieldTypeId], m2.TransactionId 
     from #modifications m2
     where m2.ClaimModificationId < m1.ClaimModificationId
     group by m2.GroupId, m2.FieldTypeId, m2.TransactionId) m3 
on m3.groupId = m1.GroupId and m3.FieldTypeId = m1.FieldTypeId and m3.TransactionId = m1.TransactionId
LEFT JOIN #modifications cm4 ON m3.ClaimModificationId = cm4.ClaimModificationId 

2 个答案:

答案 0 :(得分:1)

尝试OUTER APPLY

SELECT TOP 10
        m1.[GroupId] ,
        m1.[FieldTypeId] ,
        m1.[FieldName] ,
        m1.[TransactionId] ,
        cm4.[NewValue] AS OldValue ,
        m1.[NewValue] ,
        m1.[UserEmail] ,
        m1.[ModificationDate] ,
        m1.[Action]
FROM    #modifications m1
        OUTER APPLY ( SELECT    MAX(m2.ClaimModificationId) AS ClaimModificationId ,
                                m2.[GroupId] ,
                                m2.[FieldTypeId] ,
                                m2.TransactionId
                      FROM      #modifications m2
                      WHERE     m2.ClaimModificationId < m1.ClaimModificationId
                                AND m2.groupId = m1.GroupId
                                AND m2.FieldTypeId = m1.FieldTypeId
                                AND m2.TransactionId = m1.TransactionId
                      GROUP BY  m2.GroupId ,
                                m2.FieldTypeId ,
                                m2.TransactionId
                    ) m3
        LEFT JOIN #modifications cm4 ON m3.ClaimModificationId = cm4.ClaimModificationId 

答案 1 :(得分:0)

你可以尝试这样的事情

create table #modifications
(
  [ClaimModificationId] INT IDENTITY(1,1),
  [GroupId] INT,
  [FieldTypeId] INT,
  [FieldName] NVARCHAR(255),
  [TransactionId] INT,
  [NewValue] NVARCHAR(255),
  [UserEmail] NVARCHAR(255),
  [ModificationDate] DATETIME,
  [Action] NVARCHAR(50)
)

INSERT INTO #modifications values(1,1,'field',2,'new val1','email@email.com',GETDATE(),'inserted')
INSERT INTO #modifications values(1,2,'field',2,'val1','email@email.com',GETDATE(),'inserted')
INSERT INTO #modifications values(2,1,'field',3,'val2','email@email.com',GETDATE(),'inserted')
INSERT INTO #modifications values(1,1,'field',2,'val3','email@email.com',GETDATE(),'inserted')
INSERT INTO #modifications values(1,1,'field',2,'val4','email@email.com',GETDATE(),'inserted')
INSERT INTO #modifications values(1,1,'field',2,'val5','email@email.com',GETDATE(),'inserted')
INSERT INTO #modifications values(2,1,'field',3,'val5','email@email.com',GETDATE(),'inserted')

SELECT TOP 10 
     m1.[GroupId], 
     m1.[FieldTypeId], 
     m1.[FieldName], 
     m1.[TransactionId], 
     LAG([NewValue]) OVER(PARTITION by GroupId, FieldTypeId, TransactionId ORDER BY ClaimModificationId ASC) as OldValue, 
     m1.[NewValue], 
     m1.[UserEmail], 
     m1.[ModificationDate], 
     m1.[Action]
from #modifications m1