mariadb 从一个SQL查询中获取两个不同的值

eulz3vhy  于 2022-11-08  发布在  其他
关注(0)|答案(2)|浏览(163)

有没有方法可以简化/优化这个mysql/mariadb查询?最终我需要从一个查询中得到两个独立的数据:最新交易名称和所有交易支付金额的总和。
这是可行的,但很难看,因为它重复了JOINS和WHERE子句:

SELECT
        SUM(btp.allocated_amount),
        (
        SELECT
            bt.name
        FROM
            `tabBank Transaction Payments` as btp
        LEFT JOIN
            `tabBank Transaction` bt ON bt.name=btp.parent
        WHERE
            btp.payment_document = 'Journal Entry'
        AND
            bt.docstatus = 1
        ORDER BY
            bt.date desc
        LIMIT 1
        ) AS name
        FROM
            `tabBank Transaction Payments` as btp
        LEFT JOIN
            `tabBank Transaction` bt ON bt.name=btp.parent
        WHERE
            btp.payment_document = 'Journal Entry'
        AND
            bt.docstatus = 1;

我以为这样的东西会工作,但它没有。给出的名称是任意的,而不是第一个根据ORDER BY:

SELECT
        (SELECT SUM(allocated_amount)),
        (SELECT name LIMIT 1)
        FROM
        (
        SELECT
            btp.allocated_amount,
            bt.name
        FROM
            `tabBank Transaction Payments` as btp
        LEFT JOIN
            `tabBank Transaction` bt ON bt.name=btp.parent
        WHERE
            btp.payment_document = 'Journal Entry'
        AND
            bt.docstatus = 1
        ORDER BY
            bt.date desc
        ) AS temp;

编辑:
示例数据(注:示例已简化):
tab银行交易付款

| parent | payment_document | allocated_amount |
------------------------------------------------
| doc1   | Journal Entry    | 10.00            |
| doc1   | Journal Entry20  | 4000.00          |
| doc2   | Journal Entry    | 20.00            |
| doc2   | Journal Entry20  | 5000.00          |
| doc3   | Journal Entry    | 30.00            |
| doc3   | Journal Entry20  | 6000.00          |

tab银行交易

| name | date       | docstatus |
---------------------------------
| doc1 | 2022-01-01 | 1         |
| doc2 | 2022-02-01 | 1         |
| doc3 | 2022-03-01 | 1         |

所需结果:(60.00,文件3)
mariadb版本:10.2.27

k2fxgqgv

k2fxgqgv1#

我们可以试着用ROW_NUMBER & SUM窗口函数来制作它。
ROW_NUMBERbt.date desc获取名称的最后一行
OVER子句对于窗口函数是必不可少的,该函数基于一组记录执行计算,而PARTITION BY定义行被划分成的组。
更多详细信息,请参见Window Function Concepts and Syntax

SELECT total_allocated_amount,name
FROM (
    SELECT
        SUM(btp.allocated_amount) OVER() total_allocated_amount,
        ROW_NUMBER() OVER(ORDER BY bt.date desc) rn,
        bt.name
    FROM
        `tabBank Transaction Payments` as btp
    LEFT JOIN
        `tabBank Transaction` bt ON bt.name=btp.parent
    WHERE
        btp.payment_document = 'Journal Entry'
    AND
        bt.docstatus = 1
) t1
WHERE rn = 1

sqlfiddle

1l5u6lss

1l5u6lss2#

感谢D-Shih的建议和介绍窗口函数,我不知道存在。我简化和结束了这个。
对于由于分区而需要多个结果行的其他方案,将需要ROW_NUMBER()

SELECT *
FROM (
    SELECT
        SUM(btp.allocated_amount) OVER() total_allocated_amount,
        FIRST_VALUE(bt.name) OVER(ORDER BY bt.date desc) latest_name
    FROM
        `tabBank Transaction Payments` as btp
    LEFT JOIN
        `tabBank Transaction` bt ON bt.name=btp.parent
    WHERE
        btp.payment_document = 'Journal Entry'
    AND
        bt.docstatus = 1
) t1
LIMIT 1

相关问题