我正在尝试编写一个 GROUP BY 查询,但我正在努力实现预期的结果。我包括
- 一个 JSON 对象,表示带有一些数据的 DB 表。
SELECT * FROM tb_transaction "tb_transaction": [ { "id": "121", "profile_id": "57", "event_id": "45", "activity_id": "67", "payment_type": "EFT", "transaction_type": "activity_registration", "gl_code": "1234-56-102-020", "description": "Golf", "amount": "1500", "paid": "f", "invoice_number": "0006" }, { "id": "117", "profile_id": "57", "event_id": "45", "activity_id": "65", "payment_type": "EFT", "transaction_type": "activity_registration", "gl_code": "1234-56-102-056", "description": "Cuppa", "amount": "100", "paid": "f", "invoice_number": "0006" }, { "id": "120", "profile_id": "57", "event_id": "45", "activity_id": "70", "payment_type": "EFT", "transaction_type": "activity_registration", "gl_code": "1234-13-102-064", "description": "Nutrition & Lifestyle", "amount": "510", "paid": "f", "invoice_number": "0006" }, { "id": "125", "profile_id": "207", "event_id": "45", "activity_id": "65", "payment_type": "Cash", "transaction_type": "activity_registration", "gl_code": "1234-56-102-056", "description": "Cuppa", "amount": "100", "paid": "f", "invoice_number": "0007" }, { "id": "126", "profile_id": "207", "event_id": "45", "activity_id": "65", "payment_type": "Cash", "transaction_type": "merchandise", "gl_code": "3400-56-102-056", "description": "Cap", "amount": "20", "paid": "f", "invoice_number": "0007" }, { "id": "128", "profile_id": "193", "event_id": "45", "activity_id": "70", "payment_type": "SnapScan", "transaction_type": "activity_registration", "gl_code": "1234-13-102-064", "description": "Nutrition & Lifestyle", "amount": "510", "paid": "f", "invoice_number": "0008" }, { "id": "131", "profile_id": "193", "event_id": "45", "activity_id": "65", "payment_type": "SnapScan", "transaction_type": "merchandise", "gl_code": "3400-56-102-056", "description": "Water Bottle", "amount": "10", "paid": "f", "invoice_number": "0008" }, { "id": "130", "profile_id": "193", "event_id": "45", "activity_id": "65", "payment_type": "SnapScan", "transaction_type": "activity_registration", "gl_code": "1234-56-102-056", "description": "Cuppa", "amount": "100", "paid": "f", "invoice_number": "0008" } ]
- 我当前的查询及其结果。
SELECT gl_code, transaction_type, activity_id, payment_type, description, SUM(amount) AS amount FROM tb_transaction WHERE event_id = 45 AND paid = false GROUP BY gl_code, transaction_type, activity_id, payment_type, description ORDER BY gl_code; "RECORDS": [ { "gl_code": "1234-13-102-064", "transaction_type": "activity_registration", "activity_id": "70", "payment_type": "EFT", "description": "Nutrition & Lifestyle", "amount": "510" }, { "gl_code": "1234-13-102-064", "transaction_type": "activity_registration", "activity_id": "70", "payment_type": "SnapScan", "description": "Nutrition & Lifestyle", "amount": "510" }, { "gl_code": "1234-56-102-056", "transaction_type": "activity_registration", "activity_id": "65", "payment_type": "Cash", "description": "Cuppa", "amount": "100" }, { "gl_code": "1234-56-102-056", "transaction_type": "activity_registration", "activity_id": "65", "payment_type": "EFT", "description": "Cuppa", "amount": "100" }, { "gl_code": "1234-56-102-056", "transaction_type": "activity_registration", "activity_id": "65", "payment_type": "SnapScan", "description": "Cuppa", "amount": "100" }, { "gl_code": "1234-56-102-020", "transaction_type": "activity_registration", "activity_id": "67", "payment_type": "EFT", "description": "Golf", "amount": "1500" }, { "gl_code": "3400-56-102-056", "transaction_type": "merchandise", "activity_id": "65", "payment_type": "Cash", "description": "Cap", "amount": "20" }, { "gl_code": "3400-56-102-056", "transaction_type": "merchandise", "activity_id": "65", "payment_type": "SnapScan", "description": "Water Bottle", "amount": "10" } ]
- 想要的结果。
`"RECORDS": [ { "gl_code": "1234-13-102-064", "transaction_type": "activity_registration", "activity_id": "70", "payment_type": "EFT", "description": "Nutrition & Lifestyle", "amount": "510" }, { "gl_code": "1234-13-102-064", "transaction_type": "activity_registration", "activity_id": "70", "payment_type": "SnapScan", "description": "Nutrition & Lifestyle", "amount": "510" }, { "gl_code": "1234-56-102-056", "transaction_type": "activity_registration", "activity_id": "65", "payment_type": "Cash", "description": "Cuppa", "amount": "120" }, { "gl_code": "1234-56-102-056", "transaction_type": "activity_registration", "activity_id": "65", "payment_type": "EFT", "description": "Cuppa", "amount": "100" }, { "gl_code": "1234-56-102-056", "transaction_type": "activity_registration", "activity_id": "65", "payment_type": "SnapScan", "description": "Cuppa", "amount": "110" }, { "gl_code": "1234-56-102-020", "transaction_type": "activity_registration", "activity_id": "67", "payment_type": "EFT", "description": "Golf", "amount": "1500" } ]'
差异非常微妙。但是您会看到,我基本上想将“payment_type”和“gl_code”的最后 3 位相同的记录相加。例如“payment_type”:“ Cash ”和“gl_code”:“1234-56-102-056 ”,
任何帮助将不胜感激。