我写了一个查询,但是我收到了这个错误:
Msg 512,Level 16,State 1,Line 1
子查询返回的值超过1。当子查询遵循=,!=,<,< =,>,> =或子查询用作表达式时,不允许这样做。
这是我的疑问:
SELECT
d.Description,
s.Version,
d.UtiPrefix,
d.UTI,
d.PrimaryAC,
s.ReportingObb,
s.ReportingObb,
d.LEI,
d.LEI_Countp,
d.LEI,
s.ReportingDeleg,
d.Curr,
c.Trade_Party_Domicile,
c.LEI_SGR,
Price = (
SELECT Price
FROM Price p
WHERE p.DATE = '2014-09-15 00:00:00.000'
)
FROM
Derivatives AS d
INNER JOIN
Settings AS s ON d.LEI_SGR = s.LEI_SGR
INNER JOIN
Clients c ON d.LEI_SGR = c.LEI_SGR
WHERE
c.LEI_SGR = '8156008BA0ABD6B3AE15'
答案 0 :(得分:1)
正如错误消息所示,您的子查询返回多个结果。你可以这样解决它:
对于MySQL:
SELECT Price
FROM Price p
WHERE p.DATE = '2014-09-15 00:00:00.000'
LIMIT 1
对于T-SQL:
SELECT TOP 1 Price
FROM Price p
WHERE p.DATE = '2014-09-15 00:00:00.000'
希望这会对你有所帮助
答案 1 :(得分:1)
您需要强制子查询返回多行。
一种方法是使用MAX()
:
...
Price = (
SELECT MAX(Price)
FROM Price p
WHERE p.DATE = '2014-09-15 00:00:00.000'
)
...
其他选择:
AVG()
MIN()
TOP 1
/ LIMIT 1
(等等取决于数据库)答案 2 :(得分:0)
这是因为您的子查询SELECT Price FROM Price p WHERE p.DATE = '2014-09-15 00:00:00.000'
返回了多行。您的Price
表格price
有多个2014-09-15
。
更新:我在另一个答案的评论中看到您要在结果中显示该日期的所有价格。您需要将price
表加入查询中。不在SELECT子句中查询price
表。
SELECT d.Description,
s.Version,
d.UtiPrefix,
d.UTI,
d.PrimaryAC,
s.ReportingObb,
s.ReportingObb,
d.LEI,
d.LEI_Countp,
d.LEI,
s.ReportingDeleg,
d.Curr,
c.Trade_Party_Domicile,
c.LEI_SGR,
p.Price
FROM Derivatives AS d
INNER JOIN Settings AS s
ON d.LEI_SGR = s.LEI_SGR
INNER JOIN Clients c
ON d.LEI_SGR = c.LEI_SGR
,price p
WHERE c.LEI_SGR = '8156008BA0ABD6B3AE15' and p.date = '2014-09-15 00:00:00.000'