1

我想在表中插入每组第一条记录的副本(稍作修改)。我可以使用“with”语句和分区上的行号来获取每个组的完整第一条记录。

     with rankedByTime_PerInteraction as(
SELECT
    ROW_NUMBER() over(PARTITION By displayId, ObjectId, InteractionId order by AssetLog.Recorded Desc) as timeRank,         
    CasLog.*


    FROM         AssetLog INNER JOIN CasLog ON AssetLog.Id = CasLog.ID 
        where   FixType > 0
    )   
select newId()
           ,EVENTCOUNT
           ,PROTOCOLVERSION
           ,EVENTLOOKUPSET
           ,2000
           ,DISPLAYSERIAL
           ,DISPLAYID
           ,MACHINETYPE
           ,GEAR
           ,RF_NODE
           ,OBJECTSERIAL
           ,OBJECTRSSILEVEL
           ,OBJECTSTATUS
           ,OBJECTID
           ,OBJECTINFO
           ,CASTIMESTAMP
           ,SPARE1
           ,SPARE2
           ,[CHECKSUM]
           ,ALARMTIME
           ,MINRANGE
           ,EventId
           ,InteractionId
from rankedByTime_PerInteraction    
where timeRank = 1 

效果很好。但是我想在插入语句中使用它。

    INSERT INTO [CasLog]
           ([ID]
           ,[EVENTCOUNT]
           ,[PROTOCOLVERSION]
           ,[EVENTLOOKUPSET]
           ,[EVENTNUMBER]
           ,[DISPLAYSERIAL]
           ,[DISPLAYID]
           ,[MACHINETYPE]
           ,[GEAR]
           ,[RF_NODE]
           ,[OBJECTSERIAL]
           ,[OBJECTRSSILEVEL]
           ,[OBJECTSTATUS]
           ,[OBJECTID]
           ,[OBJECTINFO]
           ,[CASTIMESTAMP]
           ,[SPARE1]
           ,[SPARE2]
           ,[CHECKSUM]
           ,[ALARMTIME]
           ,[MINRANGE]
           ,[EventId]
           ,[InteractionId])

with rankedByTime_PerInteraction as(
    SELECT
        ROW_NUMBER() over(PARTITION By displayId, ObjectId, InteractionId order by AssetLog.Recorded Desc) as timeRank,         
        CasLog.*
        FROM         AssetLog INNER JOIN CasLog ON AssetLog.Id = CasLog.ID 
        where   FixType > 0
    )   
select newId()
           ,EVENTCOUNT
           ,PROTOCOLVERSION
           ,EVENTLOOKUPSET
           ,2000
           ,DISPLAYSERIAL
           ,DISPLAYID
           ,MACHINETYPE
           ,GEAR
           ,RF_NODE
           ,OBJECTSERIAL
           ,OBJECTRSSILEVEL
           ,OBJECTSTATUS
           ,OBJECTID
           ,OBJECTINFO
           ,CASTIMESTAMP
           ,SPARE1
           ,SPARE2
           ,[CHECKSUM]
           ,ALARMTIME
           ,MINRANGE
           ,EventId
           ,InteractionId
from rankedByTime_PerInteraction    
where timeRank = 1 

但是,我在关键字“with”附近得到了不正确的语法。

是否可以在插入语句中使用 with 语句?

如果不是,我怎样才能最好地在表中插入每组第一条记录的副本?

谢谢

4

1 回答 1

1

尝试这个:

INSERT INTO [CasLog]
           ([ID]
           ,[EVENTCOUNT]
           ,[PROTOCOLVERSION]
           ,[EVENTLOOKUPSET]
           ,[EVENTNUMBER]
           ,[DISPLAYSERIAL]
           ,[DISPLAYID]
           ,[MACHINETYPE]
           ,[GEAR]
           ,[RF_NODE]
           ,[OBJECTSERIAL]
           ,[OBJECTRSSILEVEL]
           ,[OBJECTSTATUS]
           ,[OBJECTID]
           ,[OBJECTINFO]
           ,[CASTIMESTAMP]
           ,[SPARE1]
           ,[SPARE2]
           ,[CHECKSUM]
           ,[ALARMTIME]
           ,[MINRANGE]
           ,[EventId]
           ,[InteractionId])

select newId()
           ,EVENTCOUNT
           ,PROTOCOLVERSION
           ,EVENTLOOKUPSET
           ,2000
           ,DISPLAYSERIAL
           ,DISPLAYID
           ,MACHINETYPE
           ,GEAR
           ,RF_NODE
           ,OBJECTSERIAL
           ,OBJECTRSSILEVEL
           ,OBJECTSTATUS
           ,OBJECTID
           ,OBJECTINFO
           ,CASTIMESTAMP
           ,SPARE1
           ,SPARE2
           ,[CHECKSUM]
           ,ALARMTIME
           ,MINRANGE
           ,EventId
           ,InteractionId
from (SELECT
        ROW_NUMBER() over(PARTITION By displayId, ObjectId, InteractionId order by AssetLog.Recorded Desc) as timeRank,         
        CasLog.*
        FROM         AssetLog INNER JOIN CasLog ON AssetLog.Id = CasLog.ID 
        where   FixType > 0
    ) AS rankedByTime_PerInteraction
where timeRank = 1

如果您只想获得一行,则可以使用 SELECT TOP 1 newid()...

于 2013-06-05T23:50:59.890 回答