【问题标题】:For each order without invoice对于没有发票的每个订单
【发布时间】:2021-11-14 23:33:09
【问题描述】:

我用 2 个本机查询来解决这个任务,但想用一个本机查询来解决这个问题,如果可能的话,你能告诉我方法吗。

这是我的第一个查询:

select o.id as id,o.date as date,o.customer_id_id as customerId
from orders o left outer join
     invoice i
     on o.id = i.order_id_id
where i.order_id_id is null 
intersect 
select o.id as id,o.date as date,o.customer_id_id as customerId
from detail d join
     orders o
     on o.id = d.order_id_id

这是我的第二个查询,我从第一个查询中使用 o.id,它的意思是 ?1=o.id:

select sum(d.quantity*p.price)
from product p join
     detail d
     on p.id=d.product_id_id
where d.order_id_id=?1

这是项目的 grafic enter image description here

任务文本:对于每个没有发票的订单,列出其 ID、下达日期和总价 产品详细信息,考虑到每个订购产品的数量及其单位 价钱。没有详细信息的订单不得包含在答案中。

【问题讨论】:

    标签: java postgresql spring-boot


    【解决方案1】:

    如果我理解正确,这是连接三个表(ordersdetailproduct)后的聚合查询。您可以使用NOT EXISTSLEFT JOIN/WHERE 过滤掉没有发票的订单:

    select o.id, o.date, o.customer_id_id as customer_id, 
           sum(d.quantity*p.price)
    from orders o join
         detail d
         on o.id = d.order_id_id join
         product p 
         on p.id = d.product_id_id
    where not exists (select 1
                      from invoices i
                      where o.id = i.order_id_id
                     )
    group by o.id, o.date, o.customer_id_id;
    

    【讨论】:

    • 感谢兄弟,这是一个非常好的解决方案
    猜你喜欢
    • 1970-01-01
    • 2011-09-10
    • 2012-08-11
    • 1970-01-01
    • 1970-01-01
    • 2019-10-16
    • 1970-01-01
    • 2021-06-24
    • 1970-01-01
    相关资源
    最近更新 更多