0

我正在尝试编写一个 GROUP BY 查询,但我正在努力实现预期的结果。我包括

  1. 一个 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"
    }
]
  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

任何帮助将不胜感激。

4

1 回答 1

2

如果您想按(以及显然其他一些列)payment_type的最后 3 位数字进行分组,您可以这样做gl_code

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 位数字,而不是gl_code从所有共享相同最后 3 位数字的所有数字中选择一个(任意)。

于 2018-02-15T19:56:35.403 回答