試試這個:
DECLARE @EmployeeLog TABLE
(
EmployeeID int,
EmployeeName varchar(50),
LastModifiedBy varchar(50),
ModifiedDate datetime,
ColumnA varchar(50),
ColumnB varchar(50),
ColumnC varchar(50),
LogCreatedDate datetime
)
INSERT INTO @EmployeeLog VALUES (1, 'A', 'A', '2012-02-07', 'A', 'B', 'C', '2012-02-07')
INSERT INTO @EmployeeLog VALUES (1, 'B', 'B', '2012-02-06', 'AA', 'BB', 'CC', '2012-02-06')
;
WITH Basics AS
(
SELECT *, ROW_NUMBER() OVER (ORDER BY LogCreatedDate DESC) as Entry
FROM @EmployeeLog
), PivotTable AS
(
SELECT EmployeeID,
LastModifiedBy,
ModifiedDate,
'ColumnA' ColumnName,
ColumnA ColumnValue,
LogCreatedDate,
Entry
FROM Basics
UNION ALL
SELECT EmployeeID,
LastModifiedBy,
ModifiedDate,
'ColumnB' ColumnName,
ColumnB ColumnValue,
LogCreatedDate,
Entry
FROM Basics
UNION ALL
SELECT EmployeeID,
LastModifiedBy,
ModifiedDate,
'ColumnC' ColumnName,
ColumnC ColumnValue,
LogCreatedDate,
Entry
FROM Basics
)
SELECT [Current].LastModifiedBy, [Current].ModifiedDate,
Previous.EmployeeID, Previous.ColumnName,
Previous.ColumnValue [Previous Value],
[Current].ColumnValue [Current Value]
FROM PivotTable Previous
JOIN PivotTable [Current]
ON Previous.EmployeeID = [Current].EmployeeID AND
Previous.ColumnName = [Current].ColumnName AND
Previous.Entry = [Current].Entry + 1
我通過LogCreatedDate DESC有TOP 2創紀錄的訂單,現在怎麼這兩行比較,並安排爲每在圖片 – Sukhjeevan 2012-02-14 09:25:37