How do I create a view to find the last 2 records for a company id and to display the last value along with the difference between the last and the second last?
Here is some example data:
CREATE TABLE #161689 (
Dt DATE NULL,
Value INTEGER NULL,
CompanyId INTEGER NULL
);
INSERT INTO #161689
VALUES
('12/01/2010', 10, 10),
('11/01/2010', 5, 10),
('10/01/2010', 2, 10),
('08/10/2010', 1, 10),
('12/01/2010', 7, 20),
('10/01/2010', 3, 20),
('09/01/2010', 2, 20),
('08/10/2010', 2, 20);
Here is the expected result:
Date - Value - CompanyId - Diff
12.1.2010 - 10 - 10 - 5
12.1.2010 - 7 - 20 - 4