拨开荷叶行,寻梦已然成。仙女莲花里,翩翩白鹭情。
IMG-LOGO
主页 文章列表 子查询回传超过1个值-错误讯息

子查询回传超过1个值-错误讯息

白鹭 - 2022-01-26 1950 0 0

我收到错误讯息:

下面是一个较大查询的一小部分,但确定Test_Col的查询部分基本上是我遇到这个问题的地方。查询本身有效,但是当我将其用作较大查询中的子查询时,我在 SQL Server 中收到此错误讯息。关于我哪里出错的任何想法?

select 
    distinct(nml.scode) Loan_Num,
    (select isnull(sum(isnull(t.smtd, 0)   isnull(t.sbeginbudget, 0)), 0) 
     from nmloan nml
     left join property p on nml.hprop = p.hmy
     left join total t on p.hmy = t.hppty
     where nml.hprop in (2380, 3348) 
       and t.umonth between '1/1/1900' and '9/30/2021' 
       and t.ibook = 1 and t.hacct in (1349, 1348, 1347, 1345, 1343, 1342, 1341, 1339, 1338, 1337, 1336, 1334, 1332, 1690, 1682, 1331) 
     group by nml.hprop) Test_Col
from 
    nmloan nml
left join 
    property p on nml.hprop = p.hmy
left join 
    total t on p.hmy = t.hppty
left join 
    acct ac on ac.hmy = t.hacct
left join 
    nmborrower nmb on nml.hmy = nmb.hnmloan
left join 
    person ps on nmb.hmyperson = ps.hmy
left join 
    nmloanterms nmt on nml.hmy = nmt.hloan
left join 
    nmcollateralxref nmx on nml.hmy = nmx.hnmloan
left join 
    nmcollateral nmc on nmx.hnmcollateral = nmc.hmy
left join 
    loanbut1 lb1 on nml.hmy = lb1.hcode
left join 
    NMLedger l ON nml.hmy = l.hNMLoan
left join 
    nmLedgerDetail d on l.hmy = d.hNMLedger
left join 
    loanbut7 lb on nml.hmy = lb.hcode
left join 
    loanbut8 lb8 on nml.hmy = lb8.hcode
left join 
    loanbut9 lb9 on nml.hmy = lb9.hcode
where
    nml.hprop in (2380, 3348) 
    and lb.lrPeriod in ('9/30/2021') 
    and lb9.lrnDate in ('9/30/2021')
group by 
    nml.hprop, nml.scode

uj5u.com热心网友回复:

在 SQL Server DB 中,如果您的子查询是select在我们撰写栏位名称串列命令之后撰写的,那么您的子查询必须只回传一条记录和一个栏位,否则会出错。在您的脚本中,您在from命令之前撰写了子查询,在 this 之后Loan_Num,我对您的子查询做了一些研究。在大多数情况下,您的子查询将回传超过 1 条记录。原因是您group by nml.hpropwhere命令之后撰写了此条件nml.hprop in (2380, 3348)我会自己为您撰写此查询,但我不知道您的业务逻辑以及您需要什么。如果您的子查询必须回传多于 1 条记录,那么您必须将此子查询连接到主查询,使用inner joinor left join,您不能在栏位串列中写入此子查询。

uj5u.com热心网友回复:

事实证明,由于我的子查询的别名 nml 与父查询 nml(对于 nmLoan 表)具有相同的别名,因此它不起作用。

在将我的子查询的别名更改为 nl 并将父查询的别名保留为 nml 后,这确实有效,并且我能够生成多个结果。

标签:

0 评论

发表评论

您的电子邮件地址不会被公开。 必填的字段已做标记 *