我有这个查询来显示我想要的数据。但它给了我错误[Err] 21000-[SQL Server]子查询返回了多个值。当子查询遵循=,!=,<,<=,>,> =或将子查询用作表达式时,不允许这样做。
数据:MCRX-MX023-LF-KGX,但是当我输入其他数据时没有错误
这是Sql Server 2008
SELECT
*
FROM
(
SELECT
SupplierID,
CurrencyAP AS Currency,
(
SELECT
UnitPrice
FROM
Ms_PartPriceSupplier
WHERE
SupplierID = x.SupplierID
AND PartID = x.PartID
AND LastUpdateDate = x.Tgl
) AS Price,
PartID,
(
SELECT
PartnerName
FROM
Ms_Partner
WHERE
PartnerID = x.SupplierID
) SupplierName
FROM
(
SELECT
PartID,
SupplierID,
CurrencyAP,
MAX (LastUpdateDate) AS Tgl
FROM
Ms_PartPriceSupplier
WHERE
PartID = 'MCRX-MX023-LF-KGX'
GROUP BY
PartID,
SupplierID,
CurrencyAP
) x
) y
UNION ALL
SELECT
PartnerID AS SupplierID,
'' AS Currency,
0 AS Price,
'' AS PartID,
PartnerName AS SupplierName
FROM
Ms_Partner
WHERE
PartnerID NOT IN (
SELECT DISTINCT
SupplierID
FROM
Ms_PartPriceSupplier
WHERE
PartID = 'MCRX-MX023-LF-KGX'
)
答案 0 :(得分:1)
查询中Price
和/或SupplierName
列的SELECT子查询返回多个值。他们应该只返回一个值(每行)。
您可以在这些子查询中提供TOP(1)或在其WHERE / HAVING子句中调整条件。
答案 1 :(得分:0)
您的某些子查询返回的值大于1
train_config:{
freeze_variables: ".*FirstStageFeatureExtractor/resnet_v1_50/conv1.*"
freeze_variables: ".*FirstStageFeatureExtractor/resnet_v1_50/block1.*"
freeze_variables: ".*FirstStageFeatureExtractor/resnet_v1_50/block2.*"
freeze_variables: ".*FirstStageFeatureExtractor/resnet_v1_50/block3.*"
}
在设备中根据需要添加 SELECT
UnitPrice
FROM
Ms_PartPriceSupplier
WHERE
SupplierID = x.SupplierID
AND PartID = x.PartID
AND LastUpdateDate = x.Tgl
SELECT
PartnerName
FROM
Ms_Partner
WHERE
PartnerID = x.SupplierID
或某些聚合功能,例如TOP (1)
,MIN
,例如:
MAX
答案 2 :(得分:0)
错误消息很清楚。当您使用子查询代替表而不是值时,必须确保仅返回一个值,否则将收到此错误消息。
对查询的进一步调查显示,您...做错了。例如,
(
SELECT
PartnerName
FROM
Ms_Partner
WHERE
PartnerID = x.SupplierID
) SupplierName
在这里您似乎想要获取与每个SupplierID关联的名称。您不应该以这种方式在SELECT
部分中进行子查询。请记住:首先评估FROM
,然后评估WHERE
,然后评估SELECT
。您实际上在这里指示数据库的是尝试返回一个SINGLE值作为SupplierName:仅当您的查询中只有一个SupplierID时,这才起作用。
您需要开始思考联接。这是您应该如何编写查询的方法:
SELECT
*
FROM
(
SELECT
SupplierID,
CurrencyAP AS Currency,
Ms_PartPriceSupplier.UnitPrice AS Price,
PartID,
Ms_Partner.PartnerName as SupplierName
FROM
(
SELECT
PartID,
SupplierID,
CurrencyAP,
MAX (LastUpdateDate) AS Tgl
FROM
Ms_PartPriceSupplier
WHERE
PartID = 'MCRX-MX023-LF-KGX'
GROUP BY
PartID,
SupplierID,
CurrencyAP
) x
inner join Ms_Partner on PartnerID = x.SupplierID
inner join Ms_PartPriceSupplier on SupplierID = x.SupplierID AND PartID = x.PartID AND LastUpdateDate = x.Tgl
) y
UNION ALL
SELECT
PartnerID AS SupplierID,
'' AS Currency,
0 AS Price,
'' AS PartID,
PartnerName AS SupplierName
FROM
Ms_Partner
WHERE
PartnerID NOT IN (
SELECT DISTINCT
SupplierID
FROM
Ms_PartPriceSupplier
WHERE
PartID = 'MCRX-MX023-LF-KGX'
)