错误:子查询返回超过 1 个值。当子查询跟随时这是不允许的

发布于 2025-01-16 16:21:21 字数 1635 浏览 1 评论 0原文

我有这样的桌子 表 Actividades_Usuarios 我已经尝试获取以下查询,它工作正常

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
Table Actividades_Usuarios
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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

您的好友蓝忘机已上羡 2025-01-23 16:21:21

您需要关联所有子查询,即在子查询中添加 WHERE 子句,类似于“子查询的用户 id = 主查询的用户 id”,

用一个示例澄清我的答案......

SELECT DISTINCT 
A.USER_ID,
A.SESSION_ID,
(SELECT B.EVENT_TIME 
 FROM Actividades_Usuarios B
 WHERE B.EVENT_ID = 1 
 AND B.USER_ID = A.USER_ID) as START_TIME_SESSION,
(SELECT C.EVENT_TIME 
 FROM Actividades_Usuarios C
 WHERE C.EVENT_ID = 2 
 AND C.USER_ID = A.USER_ID) as END_TIME_SESSION
FROM Actividades_Usuarios A

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…

SELECT DISTINCT 
A.USER_ID,
A.SESSION_ID,
(SELECT B.EVENT_TIME 
 FROM Actividades_Usuarios B
 WHERE B.EVENT_ID = 1 
 AND B.USER_ID = A.USER_ID) as START_TIME_SESSION,
(SELECT C.EVENT_TIME 
 FROM Actividades_Usuarios C
 WHERE C.EVENT_ID = 2 
 AND C.USER_ID = A.USER_ID) as END_TIME_SESSION
FROM Actividades_Usuarios A
纸伞微斜 2025-01-23 16:21:21

您真正想要使用的是聚合,并首先避免所有子查询:

select USER_ID, SESSION_ID,
    min(case when EVENT_ID = 1 then EVENT_TIME end) as START_TIME_SESSION,
    min(case when EVENT_ID = 2 then EVENT_TIME end) as END_TIME_SESSION
from Actividades_Usuarios
where EVENT_ID in (1, 2)
group by USER_ID, SESSION_ID;

使用 CTE 将使计算变得轻而易举:

with data as (
    select USER_ID, SESSION_ID,
        min(case when EVENT_ID = 1 then EVENT_TIME end) as START_TIME_SESSION,
        min(case when EVENT_ID = 2 then EVENT_TIME end) as END_TIME_SESSION
    from Actividades_Usuarios
    where EVENT_ID in (1, 2)
    group by USER_ID, SESSION_ID
)
select *, datediff(minute, START_TIME_SESSION, END_TIME_SESSION) as TIME_SESSION
from data;

您还应该研究 datediff 仅计算间隔边界的方式。我怀疑你想要的结果更像是这样的:

datediff(millisecond, START_TIME_SESSION, END_TIME_SESSION) / 60000 as TIME_SESSION

What you really want to use is aggregation and avoid all the subqueries in the first place:

select USER_ID, SESSION_ID,
    min(case when EVENT_ID = 1 then EVENT_TIME end) as START_TIME_SESSION,
    min(case when EVENT_ID = 2 then EVENT_TIME end) as END_TIME_SESSION
from Actividades_Usuarios
where EVENT_ID in (1, 2)
group by USER_ID, SESSION_ID;

Using CTEs will make calculations a breeze:

with data as (
    select USER_ID, SESSION_ID,
        min(case when EVENT_ID = 1 then EVENT_TIME end) as START_TIME_SESSION,
        min(case when EVENT_ID = 2 then EVENT_TIME end) as END_TIME_SESSION
    from Actividades_Usuarios
    where EVENT_ID in (1, 2)
    group by USER_ID, SESSION_ID
)
select *, datediff(minute, START_TIME_SESSION, END_TIME_SESSION) as TIME_SESSION
from data;

You should also look into the way that datediff only counts interval boundaries. I suspect the result you want is more like this:

datediff(millisecond, START_TIME_SESSION, END_TIME_SESSION) / 60000 as TIME_SESSION
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文