quote: Originally posted by PeterNeo try this,Declare @T_InVoice Table (Id Int, UserID Int, TransType Int, TransNum Int, CreateDate DateTime, Total Int, Balance Int)Insert Into @T_InVoice Select 1, 21, 1, 100, '6/5/2008 0:00', 234, 234 Union AllSelect 2, 21, 2, 1, '6/7/2008 0:00', 40, 194 Union AllSelect 3, 23, 1, 101, '6/7/2008 0:00', 50, 50 Union AllSelect 4, 22, 1, 102, '6/7/2008 0:00', 456, 456 Union AllSelect 5, 21, 1, 103, '6/8/2008 0:00', 356, 550Select *, ( Select Balance From @T_InVoice Where UserID = T.UserID and Id < T.Id and TransType = 1) as 'PreviousInvoiceBalance'From @T_InVoice T
The statement above produces the same PreviousInvoiceBalance through out, yet every invoice should have different PreviousInvoiceBalance. Please see extension of the sample data belowIndex UserID TransType TransNum CreateDate Total Balance PreviousInvoiceBalance1 21 1 100 6/5/2008 0:00 234 234 02 21 2 1 6/7/2008 0:00 40 194 2343 23 1 101 6/7/2008 0:00 50 50 04 22 1 102 6/7/2008 0:00 456 456 05 21 1 103 6/8/2008 0:00 356 550 2346 21 1 104 6/9/2008 0:00 100 650 5507 21 1 105 6/11/2008 0:00 100 750 6508 21 1 106 6/15/2008 0:00 50 800 7509 21 2 2 6/16/2008 0:00 100 700 80010 21 2 3 6/17/2008 0:00 150 550 80011 21 1 107 6/20/2008 0:00 300 850 800 When I run the statement with the sample data above, I first get an error that readsquote: Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.
So I used the statement by adding TOP (1)Declare @T_InVoice Table (Id Int, UserID Int, TransType Int, TransNum Int, CreateDate DateTime,Total Int, Balance Int)Insert Into @T_InVoiceSelect 1, 21, 1, 100, '6/5/2008 0:00', 234, 234 Union AllSelect 2, 21, 2, 1, '6/7/2008 0:00', 40, 194 Union AllSelect 3, 23, 1, 101, '6/7/2008 0:00', 50, 50 Union AllSelect 4, 22, 1, 102, '6/7/2008 0:00', 456, 456 Union AllSelect 5, 21, 1, 103, '6/8/2008 0:00', 356, 550 Union AllSelect 6, 21, 1, 104, '6/9/2008 0:00', 100, 650 Union AllSelect 7, 21, 1, 105, '6/11/2008 0:00', 100, 750 Union AllSelect 8, 21, 1, 106, '6/15/2008 0:00', 50, 800 Union AllSelect 9, 21, 2, 2, '6/16/2008 0:00', 100, 700 Union AllSelect 10, 21, 2, 3, '6/17/2008 0:00', 150, 550 Union AllSelect 11, 21, 1, 107, '6/20/2008 0:00', 300, 850Select *,( Select TOP(1) Balance From @T_InVoice Where UserID = T.UserID and Id < T.Id and TransType = 1) as 'PreviousInvoiceBalance'From @T_InVoice T The result is below1 21 1 100 2008-06-05 00:00:00.000 234 234 NULL2 21 2 1 2008-06-07 00:00:00.000 40 194 2343 23 1 101 2008-06-07 00:00:00.000 50 50 NULL4 22 1 102 2008-06-07 00:00:00.000 456 456 NULL5 21 1 103 2008-06-08 00:00:00.000 356 550 2346 21 1 104 2008-06-09 00:00:00.000 100 650 2347 21 1 105 2008-06-11 00:00:00.000 100 750 2348 21 1 106 2008-06-15 00:00:00.000 50 800 2349 21 2 2 2008-06-16 00:00:00.000 100 700 23410 21 2 3 2008-06-17 00:00:00.000 150 550 23411 21 1 107 2008-06-20 00:00:00.000 300 850 234 As you can see above the PreviousInvoiceBalance field as 234 through. |