通过内部联接进行分组不返回任何结果

时间:2020-02-18 08:23:24

标签: sql sql-server

SELECT TOP 6 *
from vws_entryline el
inner join vws_customer c on el.GeneralAccountNumber = c.AccountNumber
group by el.VoucherNumber

如您所见,我有一个根据客户帐号与表vws_entryline关联的表vws_customer。我想按另一个字段VoucherNumber(仅在vws_entryline中显示)对结果进行分组。

例如,我希望得到这些结果(这不是JSON格式,只是我脑海中的一个示例,它与分组依据不完全对应...但是我想要的实际上是一个SQL分组依据) :

{AccountNumber:“ PO200”,field_1_vws_customer:“ foo”, GeneralAccountNumber:“ PO200”,“ VoucherNumber”:“ N45PO”, field_1_vws_entryline:150.25},

{帐户号码:“ PO200”, field_1_vws_customer:“ foo”,GeneralAccountNumber:“ PO200”, “ VoucherNumber”:“ N45PO”,field_1_vws_entryline:25.75},

{AccountNumber:“ PO200”,field_1_vws_customer:“ foo”, GeneralAccountNumber:“ PO200”,“ VoucherNumber”:“ AZERTY”, field_1_vws_entryline:50,50},

{AccountNumber:“ SW7”, field_1_vws_customer:“ foo”,GeneralAccountNumber:“ SW7”, “ VoucherNumber”:“ 42HaveAGoodDay24”,field_1_vws_entryline:50,50}

如您所见,结果按VoucherNumber字段正确分组。

但是,我的SQL请求没有返回任何结果。为什么?

编辑

我尝试过:

SELECT el.EntryLineDate, el.VoucherNumber, el.label, el.GeneralAccountNumber,
       el.Debit, el.Credit, el.type, el.amount, el.PaymentTypeid, el.EntryNumber,
       c.ThirdId, c.civility, c.name, c.NaturalPerson, c.ThirdLanguage, c.siren,
       c.nic, c.naf, c.AccountNumber
from vws_entryline el
inner join vws_customer c on el.GeneralAccountNumber = c.AccountNumber
group by el.VoucherNumber

但这不起作用

2 个答案:

答案 0 :(得分:1)

您没有JSON结果集中的凭证编号聚合。它显示为普通的JSON文档,每个凭证编号每一行。

由于您尚未提供架构,因此下面是粗略查询。您需要使用FOR JSON子句来实现JSON结果集。 Read more on FOR JSON

SELECT v_c.AccountNumber, field_1_vws_customer,GeneralAccountNumber, 
VoucherNumber, field_1_vws_entryline
FROM vws_entryline AS v_e
JOIN vws_customer AS v_c
ON v_e.accountNumber = v_c.accountNumber
FOR JSON PATH;

答案 1 :(得分:1)

尝试以下方法:

SELECT TOP 6 c.AccountNumber,field_1_vws_customer,el.GeneralAccountNumber,el.VoucherNumber,
field_1_vws_entryline from vws_entryline el inner join vws_customer c on el.GeneralAccountNumber = c.AccountNumber group by c.AccountNumber,field_1_vws_customer,el.GeneralAccountNumber,el.VoucherNumber,
field_1_vws_entryline