我正在尝试编写GROUP BY查询,但我正在努力实现所需的结果。我包括
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":" 现金"和" gl_code":" 1234-56-102- 056 ",
任何帮助都将受到极大的赞赏。
答案 0 :(得分:1)
如果您想按payment_type
分组,gl_code
的最后3位数(显然还有其他一些列),则可以
SELECT substring(gl_code, length(gl_code) - 3),
transaction_type,
activity_id,
payment_type,
description,
SUM(amount) AS amount
FROM tb_transaction
WHERE event_id = 45 AND paid = false
GROUP BY substring(gl_code, length(gl_code) - 3),
transaction_type,
activity_id,
payment_type,
description
ORDER BY gl_code;
但请注意,这只会选择最后3位数字,而不是所有共享相同后3位数字的{任意] gl_code
。