I amt trying to get Principal, Interest, Principal YTD, Interest YTD from a transaction table to get them all on one line, but I can't seem to get it. I have a script here that can run without modification to see what I have here. The very last query is
incorrect, as it puts each value on a separate line, but it demonstrates what I am getting so far. What I want is one account number with 4 columns, as above mentioned. Keep in mind, in this illustration, YTD is last year and forward:
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-01') 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-01') 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-01') 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-01') 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-01') 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-01') SELECT PMT_ID, ACCT_NO, PMT_TYPE, PMT_AMT, PMT_DATE FROM #a SELECT ACCT_NO, SUM(PMT_AMT) TOT_PRINCIPAL_PAID FROM #a WHERE PMT_TYPE = 'P' GROUP BY ACCT_NO SELECT ACCT_NO, SUM(PMT_AMT) TOTAL_INTEREST_PAID FROM #a WHERE PMT_TYPE = 'I' GROUP BY ACCT_NO SELECT ACCT_NO, SUM(PMT_AMT) TOTAL_PRINCIPAL_PAID_YTD FROM #a WHERE PMT_TYPE = 'P' AND DATEPART(year,[PMT_DATE])=DATEPART(year,DATEADD(year,-1,GETDATE())) GROUP BY ACCT_NO SELECT ACCT_NO, SUM(PMT_AMT) TOTAL_INTEREST_PAID_YTD FROM #a WHERE PMT_TYPE = 'I' AND DATEPART(year,[PMT_DATE])=DATEPART(year,DATEADD(year,-1,GETDATE())) GROUP BY ACCT_NO SELECT ACCT_NO ,SUM(PMT_AMT) AS TOTAL_PRINCIPAL_PAID ,NULL AS TOTAL_INTEREST_PAID ,NULL AS TOTAL_PRINCIPAL_PAID_YTD ,NULL AS TOTAL_INTEREST_PAID_YTD FROM #a WHERE PMT_TYPE = 'P' GROUP BY ACCT_NO UNION SELECT ACCT_NO ,NULL AS TOTAL_PRINCIPAL_PAID ,SUM(PMT_AMT) AS TOTAL_INTEREST_PAID ,NULL AS TOTAL_PRINCIPAL_PAID_YTD ,NULL AS TOTAL_INTEREST_PAID_YTD FROM #a WHERE PMT_TYPE = 'I' GROUP BY ACCT_NO UNION SELECT ACCT_NO ,NULL AS TOTAL_PRINCIPAL_PAID ,NULL AS TOTAL_INTEREST_PAID ,SUM(PMT_AMT) AS TOTAL_PRINCIPAL_PAID_YTD ,NULL AS TOTAL_INTEREST_PAID_YTD FROM #a WHERE PMT_TYPE = 'P' AND DATEPART(year,[PMT_DATE])=DATEPART(year,DATEADD(year,-1,GETDATE())) GROUP BY ACCT_NO UNION SELECT ACCT_NO ,NULL AS TOTAL_PRINCIPAL_PAID ,NULL AS TOTAL_INTEREST_PAID ,NULL AS TOTAL_PRINCIPAL_PAID_YTD ,SUM(PMT_AMT) AS TOTAL_INTEREST_PAID_YTD FROM #a WHERE PMT_TYPE = 'I' AND DATEPART(year,[PMT_DATE])=DATEPART(year,DATEADD(year,-1,GETDATE())) GROUP BY ACCT_NO -- Query to put this all these 4 columns on the same line, ending up with 3 lines. -- ?? -- DROP TABLE #a