I have many queries similar to this where I am grouping account numbers and transaction ids by a max(date) or max(amount). For example, account_no = 101 with several transaction ids and a max(date). I end up with 2 columns in this query. BUT, I know that there was a transaction id and a payment amount for that line before it was selected, as it is obviously in the table. But I can't seem to figure out how to get it in a query. I have a script here that creates the scenario and shows a group by query and then another where are add the payment amount. But it groups by the larger subset. I just want the corresponding values that the 3 lines in the second query came from. At this point, I am just satisfied with payment amount, but would like a transaction id as well:
CREATE TABLE #a ( PMT_ID INT IDENTITY(1,1),ACCT_NO INT, PMT_TYPE CHAR(1), PMT_AMT MONEY, PMT_DATE DATE) INSERT INTO #a ( ACCT_NO, PMT_TYPE, PMT_AMT, PMT_DATE ) VALUES ( 1, 'P', 450, '2012-09-01') INSERT INTO #a ( ACCT_NO, PMT_TYPE, PMT_AMT, PMT_DATE ) VALUES ( 1, 'I', 100, '2012-09-01') INSERT INTO #a ( ACCT_NO, PMT_TYPE, PMT_AMT, PMT_DATE ) VALUES ( 1, 'P', 450, '2012-10-01') INSERT INTO #a ( ACCT_NO, PMT_TYPE, PMT_AMT, PMT_DATE ) VALUES ( 1, 'I', 100, '2012-11-01') INSERT INTO #a ( ACCT_NO, PMT_TYPE, PMT_AMT, PMT_DATE ) VALUES ( 1, 'P', 450, '2012-12-01') INSERT INTO #a ( ACCT_NO, PMT_TYPE, PMT_AMT, PMT_DATE ) VALUES ( 1, 'I', 100, '2012-12-02') INSERT INTO #a ( ACCT_NO, PMT_TYPE, PMT_AMT, PMT_DATE ) VALUES ( 2, 'P', 351, '2012-09-01') INSERT INTO #a ( ACCT_NO, PMT_TYPE, PMT_AMT, PMT_DATE ) VALUES ( 2, 'I', 102, '2012-09-01') INSERT INTO #a ( ACCT_NO, PMT_TYPE, PMT_AMT, PMT_DATE ) VALUES ( 2, 'P', 352, '2012-10-01') INSERT INTO #a ( ACCT_NO, PMT_TYPE, PMT_AMT, PMT_DATE ) VALUES ( 2, 'I', 102, '2012-11-01') INSERT INTO #a ( ACCT_NO, PMT_TYPE, PMT_AMT, PMT_DATE ) VALUES ( 2, 'P', 353, '2012-12-01') INSERT INTO #a ( ACCT_NO, PMT_TYPE, PMT_AMT, PMT_DATE ) VALUES ( 2, 'I', 102, '2012-12-03') INSERT INTO #a ( ACCT_NO, PMT_TYPE, PMT_AMT, PMT_DATE ) VALUES ( 3, 'P', 850, '2013-09-01') INSERT INTO #a ( ACCT_NO, PMT_TYPE, PMT_AMT, PMT_DATE ) VALUES ( 3, 'I', 200, '2013-09-01') INSERT INTO #a ( ACCT_NO, PMT_TYPE, PMT_AMT, PMT_DATE ) VALUES ( 3, 'P', 880, '2013-10-01') INSERT INTO #a ( ACCT_NO, PMT_TYPE, PMT_AMT, PMT_DATE ) VALUES ( 3, 'I', 201, '2013-11-01') INSERT INTO #a ( ACCT_NO, PMT_TYPE, PMT_AMT, PMT_DATE ) VALUES ( 3, 'P', 855, '2013-12-01') INSERT INTO #a ( ACCT_NO, PMT_TYPE, PMT_AMT, PMT_DATE ) VALUES ( 3, 'I', 203, '2013-12-04') INSERT INTO #a ( ACCT_NO, PMT_TYPE, PMT_AMT, PMT_DATE ) VALUES ( 1, 'P', 451, '2013-09-01') INSERT INTO #a ( ACCT_NO, PMT_TYPE, PMT_AMT, PMT_DATE ) VALUES ( 1, 'I', 99, '2013-09-01') INSERT INTO #a ( ACCT_NO, PMT_TYPE, PMT_AMT, PMT_DATE ) VALUES ( 1, 'P', 451, '2013-10-01') INSERT INTO #a ( ACCT_NO, PMT_TYPE, PMT_AMT, PMT_DATE ) VALUES ( 1, 'I', 99, '2013-11-01') INSERT INTO #a ( ACCT_NO, PMT_TYPE, PMT_AMT, PMT_DATE ) VALUES ( 1, 'P', 451, '2013-12-01') INSERT INTO #a ( ACCT_NO, PMT_TYPE, PMT_AMT, PMT_DATE ) VALUES ( 1, 'I', 99, '2013-12-15') INSERT INTO #a ( ACCT_NO, PMT_TYPE, PMT_AMT, PMT_DATE ) VALUES ( 2, 'P', 352, '2013-09-01') INSERT INTO #a ( ACCT_NO, PMT_TYPE, PMT_AMT, PMT_DATE ) VALUES ( 2, 'I', 101, '2013-09-01') INSERT INTO #a ( ACCT_NO, PMT_TYPE, PMT_AMT, PMT_DATE ) VALUES ( 2, 'P', 353, '2013-10-01') INSERT INTO #a ( ACCT_NO, PMT_TYPE, PMT_AMT, PMT_DATE ) VALUES ( 2, 'I', 103, '2013-11-01') INSERT INTO #a ( ACCT_NO, PMT_TYPE, PMT_AMT, PMT_DATE ) VALUES ( 2, 'P', 354, '2013-12-01') INSERT INTO #a ( ACCT_NO, PMT_TYPE, PMT_AMT, PMT_DATE ) VALUES ( 2, 'I', 103, '2013-12-17') INSERT INTO #a ( ACCT_NO, PMT_TYPE, PMT_AMT, PMT_DATE ) VALUES ( 3, 'P', 851, '2013-09-01') INSERT INTO #a ( ACCT_NO, PMT_TYPE, PMT_AMT, PMT_DATE ) VALUES ( 3, 'I', 199, '2013-09-01') INSERT INTO #a ( ACCT_NO, PMT_TYPE, PMT_AMT, PMT_DATE ) VALUES ( 3, 'P', 881, '2013-10-01') INSERT INTO #a ( ACCT_NO, PMT_TYPE, PMT_AMT, PMT_DATE ) VALUES ( 3, 'I', 201, '2013-11-01') INSERT INTO #a ( ACCT_NO, PMT_TYPE, PMT_AMT, PMT_DATE ) VALUES ( 3, 'P', 854, '2013-12-01') INSERT INTO #a ( ACCT_NO, PMT_TYPE, PMT_AMT, PMT_DATE ) VALUES ( 3, 'I', 205, '2013-12-28') SELECT PMT_ID, ACCT_NO, PMT_TYPE, PMT_AMT, PMT_DATE FROM #a SELECT ACCT_NO, MAX(PMT_DATE) MAX_PMT_DATE FROM #a GROUP BY ACCT_NO SELECT *-- PMT_ID, ACCT_NO, PMT_TYPE, PMT_AMT, PMT_DATE FROM ( SELECT ACCT_NO, MAX(PMT_DATE) MAX_PMT_DATE, PMT_AMT FROM #a t1 GROUP BY ACCT_NO, PMT_AMT ) t2 DROP TABLE #a