Postgres GROUP BY、SUBSTRING 和 SUM 查询问题

Postgres GROUP BY, SUBSTRING and SUM Query Issue

我正在尝试编写 GROUP BY 查询,但我很难获得所需的结果。我包括

  1. 一个 JSON 对象,表示具有一些数据的数据库 table。
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"
    }
]
  1. 我当前的查询及其结果。
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"
        }
    ]
  1. 想要的结果。
`"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",

任何帮助将不胜感激。

如果你想按 payment_typegl_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;

但是请注意,这只会 select 最后 3 位数字,而不是所有共享相同最后 3 位数字的(任意)gl_code