简体   繁体   中英

mysql sql select last record in each group

I have the following query that provides the results in the screenshot below. What I'm trying to accomplish is to have the final result only be the last record for each invoice_number.

I've looked at other questions related to selecting last record by group or subgroup and my sql skills just aren't at a level where I can figure out how I could apply the solutions that were provided in those other questions.

Any help is appreciated.

SELECT 
    `Invoice`.Amount AS amount,
    zuora.convertDateTimeToGMTOffsetDateTime(`Invoice`.CreatedDate) AS created_date,
    `Invoice`.InvoiceNumber AS invoice_number,
    RatePlanCharge.chargeModel,
    RatePlanCharge.chargeType,
    `InvoiceItem`.`RatePlanChargeId`,
    CASE
        WHEN
            PaymentMethod.type = 'CreditCard'
        THEN
            CONCAT(PaymentMethod.creditCardType,
                    ' (',
                    REPLACE(PaymentMethod.creditCardMaskNumber,
                        '*',
                        ''),
                    ')')
        WHEN
            PaymentMethod.type = 'PayPal'
                AND PaymentMethod.paypalEmail != ''
        THEN
            IF(PaymentMethod.paypalEmail != '',
                CONCAT(PaymentMethod.type,
                        ' (',
                        PaymentMethod.paypalEmail,
                        ')'),
                PaymentMethod.type)
        WHEN PaymentMethod.type = 'WireTransfer' THEN 'Wire Transfer'
        ELSE IF(Invoice.Amount = 0,
            'None Required',
            IF(PaymentMethod.type,
                PaymentMethod.type,
                'Unpaid'))
    END AS payment_method,
    IF(RatePlanCharge.chargeModel != 'Discount'
            AND (RatePlanCharge.chargeType = 'Recurring'
            OR RatePlanCharge.chargeType = 'Usage'),
        TRUE,
        FALSE) AS recurring
FROM
    `InvoiceItem`
        JOIN
    `Invoice` ON `InvoiceItem`.`InvoiceId` = `Invoice`.`id`
        LEFT JOIN
    `RatePlanCharge` ON `RatePlanCharge`.`id` = `InvoiceItem`.`RatePlanChargeId`
        LEFT JOIN
    `InvoicePayment` ON `InvoicePayment`.`invoiceId` = `Invoice`.`id`
        LEFT JOIN
    `Payment` ON `Payment`.`id` = `InvoicePayment`.`paymentId`
        LEFT JOIN
    `PaymentMethod` ON `Payment`.`paymentMethodId` = `PaymentMethod`.`id`
        LEFT JOIN
    `CreditBalanceAdjustment` ON `CreditBalanceAdjustment`.`sourceTransactionId` = `Invoice`.`id`
WHERE
    `Invoice`.`Status` = 'Posted'
        AND Invoice.accountid = '2c92c0f84b786da2014bb934ef664f1b'

which provides the following results. I'd like to limit the final results to just the records that are highlighted in yellow (the last record in each group of invoice numbers):

在此处输入图片说明

To get single row for each invoice, you can simply add at the end of the query
GROUP BY Invoice.InvoiceNumber
But it is not clear from your post what is the criterion/property for 'last' row. Is it the orinal created_date property (in the screenshot there are already converted dates which are not distinguishable)? If it is so, then you should first sort resultset by invoce number and then by the created date in descending order (so that the latest record for each invoice is listed as first one and group caluse will take that one), add this
ORDER BY Invoice.InvoiceNumber asc, Invoice.CreatedDate desc GROUP BY Invoice.InvoiceNumber

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