SQL server 2016,子查询返回了多个值。
我运行以下查询:
Select word,
tabl2.nowdatetime,
tabl2.MAX_COUNT_ID,
tabl2.MIN_COUNT_ID,
tabl2.[border(%)],
tabl2.id
From (
Select word,
tabl.nowdatetime,
tabl.id,
Cast(tabl.MAX_COUNT_ID As Float) As MAX_COUNT_ID,
Cast(tabl.MIN_COUNT_ID As Float) As MIN_COUNT_ID,
((((Cast(tabl.MAX_COUNT_ID As Float)) - (Cast(tabl.MIN_COUNT_ID As Float))) / (Cast(tabl.MIN_COUNT_ID As Float))) * 100) As 'border(%)'
From (
Select Distinct word,
(
Select Max(nowdatetime)
From Presign_Problem
Where nowdatetime >= DateAdd(Hour, -4, GetDate())
) As nowdatetime,
(
Select Min(count_id)
From Presign_Problem pp2
Where pp1.word = pp2.word
And nowdatetime >= DateAdd(Hour, -4, GetDate())
Group By word
) As MIN_COUNT_ID,
(
Select Max(count_id)
From Presign_Problem pp2
Where pp1.word = pp2.word
And nowdatetime >= DateAdd(Hour, -4, GetDate())
Group By word
) As MAX_COUNT_ID,
(
Select id
From Presign_Problem pp2
Where pp1.word = pp2.word
And nowdatetime >= DateAdd(Hour, -4, GetDate())
And count_id = (
Select Max(count_id)
From Presign_Problem pp3
Where pp1.word = pp3.word
And nowdatetime >= DateAdd(Hour, -4, GetDate())
)
) As id
From Presign_Problem pp1
Where nowdatetime >= DateAdd(Hour, -4, GetDate())
) tabl
Where tabl.id Is Not Null
) tabl2;
我收到以下错误:
子查询返回的值超过1。当子查询遵循=,!=,<,< =,>,> =或子查询用作表达式时,不允许这样做。 下面的错误: ,(从Presign_Problem pp2中选择id,其中pp1.word = pp2.word和nowdatetime> = DATEADD(HOUR,-4,GETDATE())和count_id =(SELECT MAX(count_id)FROM Presign_Problem pp3其中pp1.word = pp3。 word和nowdatetime> = DATEADD(HOUR,-4,GETDATE())))作为id
以前,查询未显示错误。
以下是表中数据的示例:
word count_word count_id nowdatetime id
computer 73 61 22.08.2017 15:24 ID23679381,ID48525000,ID23597299
computer 74 62 22.08.2017 14:24 ID23682019,ID23499955,ID23534359
computer 80 70 22.08.2017 12:24 ID23629307,ID48529530,ID23612836
有什么想法吗?我该如何解决?
答案 0 :(得分:1)
试试这个
选择ID时使用Aggregate
函数或TOP
子句。
我在这里使用了TOP
子句。
Select word,
tabl2.nowdatetime,
tabl2.MAX_COUNT_ID,
tabl2.MIN_COUNT_ID,
tabl2.[border(%)],
tabl2.id
From (
Select word,
tabl.nowdatetime,
tabl.id,
Cast(tabl.MAX_COUNT_ID As Float) As MAX_COUNT_ID,
Cast(tabl.MIN_COUNT_ID As Float) As MIN_COUNT_ID,
((((Cast(tabl.MAX_COUNT_ID As Float)) - (Cast(tabl.MIN_COUNT_ID As Float))) / (Cast(tabl.MIN_COUNT_ID As Float))) * 100) As 'border(%)'
From (
Select Distinct word,
(
Select Max(nowdatetime)
From Presign_Problem
Where nowdatetime >= DateAdd(Hour, -4, GetDate())
) As nowdatetime,
(
Select Min(count_id)
From Presign_Problem pp2
Where pp1.word = pp2.word
And nowdatetime >= DateAdd(Hour, -4, GetDate())
Group By word
) As MIN_COUNT_ID,
(
Select Max(count_id)
From Presign_Problem pp2
Where pp1.word = pp2.word
And nowdatetime >= DateAdd(Hour, -4, GetDate())
Group By word
) As MAX_COUNT_ID,
(
Select TOP 1 id
From Presign_Problem pp2
Where pp1.word = pp2.word
And nowdatetime >= DateAdd(Hour, -4, GetDate())
And count_id = (
Select Max(count_id)
From Presign_Problem pp3
Where pp1.word = pp3.word
And nowdatetime >= DateAdd(Hour, -4, GetDate())
)
) As id
From Presign_Problem pp1
Where nowdatetime >= DateAdd(Hour, -4, GetDate())
) tabl
Where tabl.id Is Not Null
) tabl2;
答案 1 :(得分:0)
正如它在错误中所述,这个子查询:
Select id
From Presign_Problem pp2
Where pp1.word = pp2.word
And nowdatetime >= DateAdd(Hour, -4, GetDate())
And count_id = (
Select Max(count_id)
From Presign_Problem pp3
Where pp1.word = pp3.word
And nowdatetime >= DateAdd(Hour, -4, GetDate())
返回多个记录,在此上下文中不允许这样做。您需要id
的单个值,但SQL Server并不知道从找到的多个值中选择正确的值。您需要修复数据(例如id
只有唯一值)或更改此查询,以便只返回1条记录,并获得id的不同值(将DISTINCT添加到Select,使用聚合等)。
在不知道数据是什么的情况下,我们无法告诉您哪一个是最合适的选择。