简体   繁体   中英

MySQL Pivot Table get first and last rows within grouping records

I have banking database written on mysql. I'm Using MySQL pivot table for generation group wise records / reports.

This is my sample table for account maintenance,

 ----------------------------------------------------------------------------------
account_number   | description   | Transaction Amount | Balance  | transaction_date
----------------------------------------------------------------------------------
1021212              1             0                    0          2013-04-02
1021213              1             100                 100         2013-04-01
1021212              1             1000                1000        2013-04-02
1021213              1             100                 200         2013-04-01
1021214              1             500                 500         2013-04-02
1021212              2             100                 900         2013-04-09

I need to run full report with displays daily (monthly) transactions.

This is my required report format,

-----------------------------------------------------------------------------------
account_number   | init_balance   | total_banking | total_withdraw | final_balance
----------------------------------------------------------------------------------
1021212              0               1000            100                900
1021213              100             100             0                  200
1021214              0             500             0                    600

I'm trying to generate this report with pivot table query and I could get all information except initial and final balance fields.

This is my sample query,

SELECT account_number,
**xxxxxxxxxx AS init_balance,**
SUM(CASE WHEN m.description = '1' THEN m.amount END) AS total_banking,
SUM(CASE WHEN m.description = '2' THEN m.amount END) AS total_withdraw,
**xxxxxxxxxx AS final_balance**
FROM account
WHERE transaction_date BETWEEN '2013-04-01' AND '2013-04-30'
GROUP BY account_number
ORDER BY account_number

if Anyone can help, Kindly let me know the pattern of writing pivot table for get first and last row of the table withing grouping results.

Try

CASE WHEN MIN(record_id_field) THEN Balance   END AS initial_amount

AND

CASE WHEN MAX(record_id_field) THEN Balance   END AS final_amount

You need something like that:

    SELECT
    m.account_number,
    SUM(IF(id = t.min_id, balance, 0)) AS init_balance,
    SUM(CASE WHEN m.description = '1' THEN m.amount END) AS total_banking,
    SUM(CASE WHEN m.description = '2' THEN m.amount END) AS total_withdraw,
    SUM(IF(id = t.max_id, balance, 0)) AS final_balance
FROM
    account m
INNER JOIN
    (
        SELECT
            account_number,
            MIN(id) AS min_id,
            MAX(id) AS max_id
        FROM
            account
        GROUP BY account_number
    ) AS t
ON
    m.account_number = t.account_number
WHERE
    transaction_date BETWEEN '2013-04-01' AND '2013-04-30'
GROUP BY
    account_number
ORDER BY
    account_number

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM