2016-02-10 88 views
1

這看起來很簡單,但我把頭靠在牆上以找出答案。SQL查詢 - 按月到期的數據透視貸款總結

我有一張貸款數據表,我希望每個月都要支付並彙總付款。這與「靜態池分析」類似,每個貸款池以行和月份作爲列。你可以使用這個SQL Fiddle看到樣本數據。

我創建了一個90 second screencast以更好地解釋我需要的數據透視和彙總結果,如果有幫助的話。謝謝你的幫助!

回答

1

如果它並不需要是動態的,使用CASE何時或是否SUM內聲明應作爲MySQL中的PIVOT:

SELECT 
    PoolMonth, 
    SUM(OriginationAmt) AS Origination, 
    SUM(IF(PmtDue = 201512, AmtPaid, 0)) AS `201512`, 
    SUM(IF(PmtDue BETWEEN 201512 AND 201601, AmtPaid, 0)) AS `201601`, 
    SUM(IF(PmtDue BETWEEN 201512 AND 201602, AmtPaid, 0)) AS `201602` 
FROM 
    Loans 
GROUP BY 
    PoolMonth; 

http://sqlfiddle.com/#!9/47701/16

這是一個有點冗長,以得到總OriginationAmt,運行總通過PoolMonth/PmtDue,只有輸出最新的運行總計,沒有硬編碼任何東西,但在這裏我們去:-)

SELECT 
    t.PoolMonth, 
    t.TtlOriginationAmt, 
    t.PmtDue, 
    t.RtAmtPaid 
FROM 
    (
    SELECT 
     l.PoolMonth, 
     l.OriginationAmt, 
     orig.TtlOriginationAmt, 
     l.PmtDue, 
     /* Row_Number() equivalent for MySQL http://blog.sqlauthority.com/2014/03/09/mysql-reset-row-number-for-each-group-partition-by-row-number/ */ 
     /* Assign a Row Number for each Payment Due month for the individual Pool month in ascending order (ORDER BY clause important in this subquery) */ 
     @RowNumber := CASE WHEN @PoolMonth = l.PoolMonth AND @PmtDue = l.PmtDue THEN @RowNumber + 1 ELSE 1 END AS PoolPmtRowNumber, 
     /* Use the total count of PmtDue month rows for each PoolMonth so we can limit our results to the final row */ 
     lr.PoolPmtLastRow, 
     l.AmtPaid, 
     /* Running total of Amount Paid for the individual Pool month in order of Payment Due month (ORDER BY clause important in this subquery) */ 
     @RtAmtPaid := CASE WHEN @PoolMonth = l.PoolMonth THEN @RtAmtPaid + l.AmtPaid ELSE l.AmtPaid END AS RtAmtPaid, 
     /* Keep track of the Pool month we're totalling */ 
     @PoolMonth := l.PoolMonth, 
     /* Keep track of the Payment Due month we're ordering */ 
     @PmtDue := l.PmtDue 
    FROM 
     Loans l 
    JOIN 
     /* Get the Total Origination Amount */ 
     (SELECT PoolMonth, SUM(OriginationAmt) AS TtlOriginationAmt FROM Loans GROUP BY PoolMonth) orig ON orig.PoolMonth = l.PoolMonth 
    JOIN 
     /* Get the total number of records by Pool/Payment due month so we can filter to the last row */ 
     (SELECT PoolMonth, PmtDue, COUNT(1) AS PoolPmtLastRow FROM Loans GROUP BY PoolMonth, PmtDue) AS lr ON lr.PoolMonth = l.PoolMonth AND lr.PmtDue = l.PmtDue 
    CROSS JOIN 
     /* Reset the variables we need for tracking */ 
     (SELECT @RtAmtPaid:=0,@PoolMonth:=0,@PmtDue:=0,@RowNumber:=0) var 
    /* Order by Pool/Payment Due month */ 
    ORDER BY 
     l.PoolMonth, 
     l.PmtDue 
)t 
WHERE 
    /* Filter to the last row */ 
    t.PoolPmtRowNumber = t.PoolPmtLastRow; 

http://sqlfiddle.com/#!9/47701/45

從那裏,它應該很容易在Excel或其他任何地方轉換您的結果。

+0

謝謝您花時間回答。是的,這是有效的,但是,正如您注意到的,我的場景是動態的,而PmtDue列中的月份並不相同,這就是爲什麼需要該列上的數據透視表。 – Damon

+0

我會稍微更新我的答案,但您基本上正在尋找這個:http://stackoverflow.com/questions/12004603/mysql-pivot-row-into-dynamic-number-of-columns我通常避免查詢中的動態SQL,因爲它很難閱讀,並且我通常可以更清楚地將這種動態透視移動到應用程序層(Excel,Tableau,Web應用程序等)。 – vanlee1987

+0

只要可以用相同的方式總結數據,我其實可以使用Excel來轉發數據。 Excel將允許我稍後在數據上添加過濾器。 – Damon