gpt4 book ai didi

sql - 来自嵌套连接的单行

转载 作者:行者123 更新时间:2023-11-29 13:38:26 26 4
gpt4 key购买 nike

我有以下查询:

SELECT
COALESCE(purchase.entry.code,'') || coalesce(purchase.linentry.linecod,'') AS pkey,
purchase.entry.code,
purchase.linetry.linecod,
purchase.entry.date,
purchase.invoice.ninvoice,
purchase.invoice.dateinvoice,
gen.provider.code AS code1,
gen.provider.name,
gen.provider.name2,
gen.provider.vatno,
purchase.vlstinvoice.total,
purchase.vlstinvoice.vat,
purchase.vlstinvoice.totalVat
FROM
purchase.entry
JOIN
purchase.linentry ON purchase.linentry.codentry = purchase.entry.code
JOIN
purchase.lininvoice ON purchase.lininvoice.codlinentry = purchase.linentry.code
JOIN
purchase.invoice ON purchase.invoice.code = purchase.lininvoice.codinvoice
JOIN
gen.provider ON gen.provider.code = purchase.entry.codprovider
JOIN
purchase.vlstinvoice ON purchase.vlstinvoice.ninvoice = purchase.invoice.ninvoice

查询结果给我重复的entry.code项。我需要在每一行中使用第一个或最后一个 linentry.linecod 的唯一 entry.code(因为每个 entry.code 可以有多个 linentry.linecode)。

最佳答案

使用distinct on。以下获取第一个linecod:

select distinct on (purchase.entry.code)
. . . -- rest of columns
from . . .
order by purchase.entry.code, purchase.linentry.linecod;

关于sql - 来自嵌套连接的单行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58978784/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com