我对SQL还是很陌生,所以我收到了我的问题标题中所述的错误。
附件图像是我的sql命令。
如果我不使用IN, 我会收到另一个错误,那就是子查询返回的值超过1个...
我应该如何编写它,以免出现任何此类错误?
下面是SQLcode: 开始
SELECT [c].[ChargeSubCategoryOID] ,[c].[Description], [c].[Qty], [c].[Uom], [c].[BeforeGovtSubsidy], [c].[AfterGovtSubsidy],
[c].[ChargesIndicatorCode], [c].[ChargesIndicatorValue]
FROM ChargeItems AS [c]
WHERE
[c].[ChargeSubCategoryOID] IN
(
SELECT [c].[OID]
FROM ChargeSubCategories AS [c]
WHERE
[c].[ChargeCategoryOID] IN
(
SELECT [cg].[OID]
FROM ChargeCategories AS [cg]
WHERE
[cg].[ChargeGroupOID] IN
(
SELECT [cg].[OID]
FROM ChargeGroups AS [cg]
WHERE
[cg].[InvoiceOID] IN
(
SELECT TOP(1)
[p].[OID]
FROM [Invoices] AS [p]
LEFT JOIN [Payors] AS [p.Payors] ON [p].[OID] = [p.Payors].[InvoiceOID]
LEFT JOIN [Details] AS [p.Details] ON [p].[OID] = [p.Details].[InvoiceOID]
WHERE ([p].[InvoiceReferenceNo] = @BillRefNo) AND ([p.Details].[IdentificationNoValue] = @Id)
ORDER BY [p].[CreationTime] DESC, [p].[OID], [p.Payors].[OID]
)
)
)
)ORDER BY [c].[ChargeSubCategoryOID] ASC
END
感谢您的帮助!谢谢!
答案 0 :(得分:1)
我已将您的IN替换为INNER JOIN。请注意,我已经更改了别名,因此它们都不同,以防止歧义。
唯一重要的一点是最后一个JOIN,因为我尚未对表Invoices进行内部联接,但三个左联接表为其赋予了别名,因此您可以将联接条件设置为SQL语句的其余部分。
SELECT it.ChargeSubCategoryOID ,it.Description, it.Qty, it.Uom, it.BeforeGovtSubsidy,
it.AfterGovtSubsidy, it.ChargesIndicatorCode, it.ChargesIndicatorValue
FROM ChargeItems AS it
inner join ChargeSubcategories AS sc on sc.OID = it.ChargeSubCategoryOID
inner join ChageCategories AS ct on ct.OID = sc.ChargeCategoryOID
inner join ChargeGroups AS gr on gr.OID = ct.ChargeGroupOID
inner join (select top 1 in.OID
from Invoices AS in
left join Payors AS py on py.InvoiceOID = in.OID
left join Details AS dt on dt.InvoiceOID = in.OID
where in.InvoiceReferenceNo = @BillRefNo AND dt.IdentificationNoValue = @Id
order by in.CreationTime desc, in.OID, py.OID
) AS in2 on in2.OID = gr.Invoice.OID