Если он не должен быть динамичным, пользуясь случаем, когда или если оператор в SUM должен функционировать как PIVOT в MySQL:
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 или в другом месте.
Спасибо, что нашли время, чтобы ответить. Да, это работает, однако, поскольку вы отметили, что мой сценарий динамичен, а месяцы в столбце PmtDue не совпадают, поэтому требуется опорная точка в этом столбце. – Damon
Я немного поправлю свой ответ, но вы в основном ищете это: http://stackoverflow.com/questions/12004603/mysql-pivot-row-into-dynamic-number-of-columns Я обычно избегаю динамический SQL в запросе, так как его трудно читать, и я могу, как правило, более динамично перемещать этот динамический поворот в прикладной уровень (Excel, Tableau, Web Application и т. д.). – vanlee1987
Я на самом деле нормально использую Excel для поворота данных, если их можно суммировать так же. Excel позволит мне позже добавлять фильтры в данные. – Damon