需要根据最新记录从表中的先前记录中获取数据

时间:2015-12-17 07:13:30

标签: sql sql-server stored-procedures

我写了一个查询来获取用户的最新数据输入,但现在我需要从同一个表中获取另一条记录。我需要以前的记录意味着最新记录和之前的记录,需要一些来自前一记录行和一些来自最新记录的数据。

以下是我的代码,它根据创建日期或所需日期提供最新记录数据

ALTER PROCEDURE [dbo].[RFI_GetGIGCommunicationLog]
    @IDUPW as int ,
    @IDStatus as int--,
    --@TotalGIGCount as INT OUTPUT
AS
BEGIN

    SET NOCOUNT ON;

    select upw.IDUPW,gig.IDGig,gig.Number,gig.DateCreated,gig.DateRequired, Originator.Originator,
    lc.IDLifeCycle,lc.LoggedById,lc.OwnedByID, lc.AssignedToID, lc.LoggedOn,lc.Detail,
    f.IDFunction,f.Name,f.PastTenseFunction,AssignedTo.AssignedBy,LoggedBy.LoggedBy, OwnedBy.OwnedBy from [UserXProjectXWorkFlowUserGroup] upw
    inner join gig on gig.IDUPW= upw.IDUPW
    cross apply (select top 1(lic.idLifecycle),lic.AssignedToID,lic.LoggedByID,lic.OwnedByID,lic.IDLevelXFunction, lic.LoggedOn,lic.Detail from lifecycle lic where gig.IdGIG= lic.IDGIG order by loggedon desc  ) lc

    cross apply (SELECT concat(u.FirstName, ' ', u.LastName) as'AssignedBy' FROM [User] u WHERE u.IDUser = lc.AssignedToID) AssignedTo
    cross apply (SELECT concat(u.FirstName, ' ', u.LastName) as'LoggedBy' FROM [User] u WHERE u.IDUser = lc.LoggedByID) LoggedBy
    cross apply (SELECT  concat(u.FirstName, ' ', u.LastName) as'OwnedBy' FROM [User] u WHERE u.IDUser = lc.OwnedByID) OwnedBy
    cross apply (SELECT  concat(u.FirstName, ' ', u.LastName) as'Originator' FROM [User] u WHERE u.IDUser = upw.IDUser) Originator
    inner join levelXfunction lf on lf.IDLevelXFunction= lc.IDLevelXFunction
    inner join [Function] f on f.IDFunction = lf.IdFunction
    where gig.IDUPW= @IDUPW  and upw.active=1 
     AND 1=(
       CASE
        WHEN @IDStatus!=0 AND f.IDFunctionXTab= @IDStatus THEN 1
        WHEN @IDStatus=0 THEN 1 ELSE 0
       END
      )
END

1 个答案:

答案 0 :(得分:1)

尝试这样的事情......

;WITH CTE_Result AS
(
SELECT 
         upw.IDUPW
        ,gig.IDGig
        ,gig.Number
        ,gig.DateCreated
        ,gig.DateRequired
        ,Originator.Originator
        ,lc.IDLifeCycle
        ,lc.LoggedById
        ,lc.OwnedByID
        ,lc.AssignedToID
        ,lc.LoggedOn
        ,lc.Detail
        ,f.IDFunction
        ,f.Name
        ,f.PastTenseFunction
        ,AssignedTo.AssignedBy
        ,LoggedBy.LoggedBy
        ,OwnedBy.OwnedBy 
        ,ROW_NUMBER() OVER (PARTITION BY lic.IDGIG ORDER BY loggedon DESC ) AS Row_lifecycle
    FROM [UserXProjectXWorkFlowUserGroup] upw
    INNER JOIN gig on gig.IDUPW= upw.IDUPW
    INNER JOIN lifecycle lc ON gig.IdGIG= lc.IDGIG     
    CROSS APPLY (SELECT concat(u.FirstName, ' ', u.LastName) as'AssignedBy' FROM [User] u WHERE u.IDUser = lc.AssignedToID) AssignedTo
    CROSS APPLY (SELECT concat(u.FirstName, ' ', u.LastName) as'LoggedBy' FROM [User] u WHERE u.IDUser = lc.LoggedByID) LoggedBy
    CROSS APPLY (SELECT  concat(u.FirstName, ' ', u.LastName) as'OwnedBy' FROM [User] u WHERE u.IDUser = lc.OwnedByID) OwnedBy
    CROSS APPLY (SELECT  concat(u.FirstName, ' ', u.LastName) as'Originator' FROM [User] u WHERE u.IDUser = upw.IDUser) Originator
    INNER JOIN levelXfunction lf ON lf.IDLevelXFunction= lc.IDLevelXFunction
    INNER JOIN [Function] f ON f.IDFunction = lf.IdFunction
    WHERE 
        gig.IDUPW= @IDUPW  
        AND upw.active=1 
        AND 1=(
                CASE WHEN @IDStatus!=0 AND f.IDFunctionXTab= @IDStatus THEN 1
                     WHEN @IDStatus=0 THEN 1 ELSE 0
                 END
              )
)
SELECT * 
FROM CTE_Result
WHERE Row_lifecycle <=2 -- Change this number as per your requirement. e.g. 1 will get you the latest record, <=2 will get you latest and previous

说明:           CTE_Results - 是派生表,可用于进一步查询处理。

现在,因为你需要第一个和第二个形式的一些数据,你应该在结果集上做一个自我加入...类似

SELECT 
     U.IDUPW
    ,L.IDGig
    ,L.Number
    ,L.DateCreated
    ,L.DateRequired 

FROM
    CTE_Result U
    LEFT JOIN CTE_Result L ON U.IdGIG=L.IdGIG -- KEY COLUMNS 
WHERE U.Row_lifecycle <=2