错误:子查询返回超过 1 个值。当子查询跟随时这是不允许的
我有这样的桌子 我已经尝试获取以下查询,它工作正常
SELECT DISTINCT
USER_ID, SESSION_ID,
(SELECT EVENT_TIME FROM Actividades_Usuarios
WHERE EVENT_ID = 1 AND USER_ID = 407) AS START_TIME_SESSION,
(SELECT EVENT_TIME FROM Actividades_Usuarios
WHERE EVENT_ID = 2 AND USER_ID = 407) AS END_TIME_SESSION,
DATEDIFF(mi, (SELECT EVENT_TIME FROM Actividades_Usuarios
WHERE EVENT_ID = 1 AND USER_ID = 407),
(SELECT EVENT_TIME FROM Actividades_Usuarios
WHERE EVENT_ID = 2 AND USER_ID = 407)) AS TIME_SESSION
FROM
Actividades_Usuarios
WHERE
USER_ID = 407
,但对于单个用户来说,例如:
然后我尝试使对于一般情况,当 EVENT_ID 为 1 时,将 EVENT*_*TIME 设置为 START_TIME_SESSION,当 EVENT_ID 为 2 时,将 EVENT*_*TIME 设置为 START_TIME_SESSION,并使用以下查询:
SELECT DISTINCT USER_ID,
SESSION_ID,
(SELECT EVENT_TIME FROM Actividades_Usuarios
WHERE EVENT_ID = 1) as START_TIME_SESSION,
(SELECT EVENT_TIME FROM Actividades_Usuarios
WHERE EVENT_ID = 2) as END_TIME_SESSION
FROM Actividades_Usuarios
但会出现常见错误:
子查询返回超过 1 个值...
当我正在查看该查询结果时。事实上,如果我将其中一列作为简单查询运行,则返回相同数量的行,无论选择什么...
SELECT USER_ID,SESSION_ID,
EVENT_TIME as START_TIME_SESSION
FROM Actividades_Usuarios
WHERE EVENT_ID = 1
但是,如何在这两者之间添加来自 DATEDIFF
的另一列 (END_TIME_SESSION) 和最终目标列结果 (TIME_SESSION)?
I got this kind of table
I already tried to obtain the following query and it works fine
SELECT DISTINCT
USER_ID, SESSION_ID,
(SELECT EVENT_TIME FROM Actividades_Usuarios
WHERE EVENT_ID = 1 AND USER_ID = 407) AS START_TIME_SESSION,
(SELECT EVENT_TIME FROM Actividades_Usuarios
WHERE EVENT_ID = 2 AND USER_ID = 407) AS END_TIME_SESSION,
DATEDIFF(mi, (SELECT EVENT_TIME FROM Actividades_Usuarios
WHERE EVENT_ID = 1 AND USER_ID = 407),
(SELECT EVENT_TIME FROM Actividades_Usuarios
WHERE EVENT_ID = 2 AND USER_ID = 407)) AS TIME_SESSION
FROM
Actividades_Usuarios
WHERE
USER_ID = 407
but for a single user, for example:
Then I try to make it out for the general case, setting EVENT*_*TIME as START_TIME_SESSION when EVENT_ID is 1, instead END_TIME_SESSION when EVENT_ID is 2, with the follow query:
SELECT DISTINCT USER_ID,
SESSION_ID,
(SELECT EVENT_TIME FROM Actividades_Usuarios
WHERE EVENT_ID = 1) as START_TIME_SESSION,
(SELECT EVENT_TIME FROM Actividades_Usuarios
WHERE EVENT_ID = 2) as END_TIME_SESSION
FROM Actividades_Usuarios
but get the common error:
Subquery returned more than 1 value...
when I'm looking exactly that query result. In fact, if I go run one of the column as a simple query, returns the same quantity of rows, whatever select...
SELECT USER_ID,SESSION_ID,
EVENT_TIME as START_TIME_SESSION
FROM Actividades_Usuarios
WHERE EVENT_ID = 1
But, how can I do this adding the other column (END_TIME_SESSION) and the final goal column result (TIME_SESSION) from DATEDIFF
between these two?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要关联所有子查询,即在子查询中添加 WHERE 子句,类似于“子查询的用户 id = 主查询的用户 id”,
用一个示例澄清我的答案......
You need to correlate all of your subqueries i.e. add to you WHERE clauses in your subqueries something along the lines of “user id of subquery = user id of main query”
Clarifying my answer with an example…
您真正想要使用的是聚合,并首先避免所有子查询:
使用 CTE 将使计算变得轻而易举:
您还应该研究
datediff
仅计算间隔边界的方式。我怀疑你想要的结果更像是这样的:What you really want to use is aggregation and avoid all the subqueries in the first place:
Using CTEs will make calculations a breeze:
You should also look into the way that
datediff
only counts interval boundaries. I suspect the result you want is more like this: