试图让CTE在PostgreSQL中工作

发布于 2025-02-06 10:48:59 字数 2499 浏览 2 评论 0原文

我是SQL的新手,我试图在没有任何运气的情况下进行递归查询来在Posttresql上工作。

I have a table tb_invoice with these columns:

invoice_nocust_nopayedtot_amount
"F0000C000400200""C0004""Y"28786.7
"F0000C000400201""C0004""N"5624.29
"F0000C000400202""C0004""Y"25675.54
"F0000C000400203""C0004"" y“35479.72
” F0000C000400207“”“ C0004”Y“ Y”23497.47
“ F0000C00050021214754.03
C0005“”F0000C000500213C0005 “ N”

“ 每个CUST_NO的一行28.000 eur。并订购发票数量的结果。它应该看起来像这样(在列中Nombre_cliente中应该出现cust_no)。

但我无法完成代码。我已经尝试了多种方法,但是它总是以重复值或每行的一张发票返回行。

这是我尝试过的一些代码。我知道我的错误是在我对工会条款的条件下,我找不到解决方案...

    WITH RECURSIVE lista_facturas AS (
    SELECT
        cust_no,
        ROW_NUMBER() OVER (PARTITION BY cust_no) AS numero_fila,
        COUNT(*) OVER (PARTITION BY cust_no) AS max_numero_fila,
        CAST (invoice_no AS TEXT) AS resultado
    FROM 
        factura_cliente
-- I have tried to add multiple WHERE clauses, but they all fail...
    UNION ALL
    SELECT
        f.cust_no,
        ROW_NUMBER() OVER (PARTITION BY f.cust_no) AS numero_fila,
        COUNT(*) OVER (PARTITION BY f.cust_no) AS max_numero_fila,
        CAST (l.resultado || ',' || f.invoice_no AS TEXT) AS resultado
    FROM
        factura_cliente f INNER JOIN lista_facturas l
            ON (l.cust_no = f.cust_no
               AND f.numero_fila = l.numero_fila + 1
               AND f.numero_fila <= l.max_numero_fila)

    -- Here I also tried 'l.invoice_no <> f.invoice_no' and other combinations
), factura_cliente AS(
    SELECT
        cust_no,
        invoice_no,
        ROW_NUMBER() OVER (PARTITION BY cust_no) AS numero_fila,
        COUNT(*) OVER (PARTITION BY cust_no) AS max_numero_fila
    FROM erp.tb_invoice i
    WHERE payed = 'N'
        AND tot_amount > 28000
)
SELECT 
    cust_no,
    resultado
FROM
    lista_facturas
;

I am new with SQL and I trying to make a recursive query to work on PostreSQL without any luck.

I have a table tb_invoice with these columns:

invoice_nocust_nopayedtot_amount
"F0000C000400200""C0004""Y"28786.7
"F0000C000400201""C0004""N"5624.29
"F0000C000400202""C0004""Y"25675.54
"F0000C000400203""C0004""Y"35479.72
"F0000C000400207""C0004""Y"23497.47
"F0000C000500212""C0005""N"14754.03
"F0000C000500213""C0005""N"3073.5

And I want to make a recursive CTE that gets the non-payed invoices bigger than 28.000eur on a single row for each cust_no. And order the result for number of invoices. It should look like something like this (in the column nombre_cliente should appear the cust_no).

enter image description here

But I cannot finish the code. I have tried multiple ways, but it always returns rows with repeated values or just one invoice per row.

This is some of the code I have tried. I am aware my mistake is with the conditions I give with the UNION clause, but I am unable to find a solution...

    WITH RECURSIVE lista_facturas AS (
    SELECT
        cust_no,
        ROW_NUMBER() OVER (PARTITION BY cust_no) AS numero_fila,
        COUNT(*) OVER (PARTITION BY cust_no) AS max_numero_fila,
        CAST (invoice_no AS TEXT) AS resultado
    FROM 
        factura_cliente
-- I have tried to add multiple WHERE clauses, but they all fail...
    UNION ALL
    SELECT
        f.cust_no,
        ROW_NUMBER() OVER (PARTITION BY f.cust_no) AS numero_fila,
        COUNT(*) OVER (PARTITION BY f.cust_no) AS max_numero_fila,
        CAST (l.resultado || ',' || f.invoice_no AS TEXT) AS resultado
    FROM
        factura_cliente f INNER JOIN lista_facturas l
            ON (l.cust_no = f.cust_no
               AND f.numero_fila = l.numero_fila + 1
               AND f.numero_fila <= l.max_numero_fila)

    -- Here I also tried 'l.invoice_no <> f.invoice_no' and other combinations
), factura_cliente AS(
    SELECT
        cust_no,
        invoice_no,
        ROW_NUMBER() OVER (PARTITION BY cust_no) AS numero_fila,
        COUNT(*) OVER (PARTITION BY cust_no) AS max_numero_fila
    FROM erp.tb_invoice i
    WHERE payed = 'N'
        AND tot_amount > 28000
)
SELECT 
    cust_no,
    resultado
FROM
    lista_facturas
;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦途 2025-02-13 10:48:59

使用String_agg函数创建昏迷的发票列表。

SELECT 
  cust_no, 
  STRING_AGG(invoice_no,',' ORDER BY invoice_no) invoice_list  
FROM tb_invoice
WHERE payed = 'N'
      AND tot_amount > 28000
GROUP BY cust_no;

Use STRING_AGG function to create coma separated invoice list.

SELECT 
  cust_no, 
  STRING_AGG(invoice_no,',' ORDER BY invoice_no) invoice_list  
FROM tb_invoice
WHERE payed = 'N'
      AND tot_amount > 28000
GROUP BY cust_no;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文