为什么我收到了无效的列引用' s'这个查询有错误吗?

时间:2016-01-06 20:57:14

标签: sql join hql

这里的内部联接可能有问题,因为我加入的两个查询如果单独运行就没问题了,但是我无法弄明白...... :(我很抱歉对于大多数人来说,这可能是一个简单的问题!

我尝试不将s引用为用户,但我仍然收到无效的列引用错误...

SELECT time_spent_bucket, totalrev
FROM
(
SELECT session_aggregate.app_timespent AS time_spent_bucket, COUNT(*) AS users
FROM
    (
    SELECT session_info.s, 
    case when SUM(session_info.session_length)/60 > 200 then "200+" 
    when SUM(session_info.session_length)/60 >= 100 then "100 <-> 200" 
    when SUM(session_info.session_length)/60 >= 50 then "50 <-> 99"
    when SUM(session_info.session_length)/60 >= 20 then "20 <-> 49"
    when SUM(session_info.session_length)/60 >= 10 then "10 <-> 19"
    when SUM(session_info.session_length)/60 >= 5 then "5 <-> 9"
    else "<5" end AS app_timespent
    FROM
        (
        SELECT kt_session(calc_session.s, calc_session.evt_lst, 5) AS (s, session_number, session_length)
        FROM 
            (
            SELECT session_set.s, collect_set(session_set.timestamps) evt_lst
            FROM 
                (
                SELECT total_list.s, total_list.timestamps
                FROM 
                    (
                    SELECT s, utc_timestamp AS timestamps
                    FROM appl9_evt
                    WHERE month = 201512
                    and s is not null
                    UNION ALL
                    SELECT s, utc_timestamp AS timestamps
                    FROM appl9_evt
                    WHERE month = 201512
                    and s is not null
                    ) total_list 
                )session_set
            GROUP BY session_set.s
            ) calc_session 
        ORDER BY s,session_number DESC
        )session_info
    GROUP BY session_info.s
    )session_aggregate
GROUP BY session_aggregate.app_timespent
ORDER BY time_spent_bucket) ts
INNER JOIN
(
SELECT s, v
FROM appl9_mtu
WHERE month = "201507"
GROUP BY s, v
        ) totalrev
ON totalrev.s = ts.s

1 个答案:

答案 0 :(得分:0)

您的加入引用了totalrev.s,但您将该列别名为“用户”#39;在totalrev子查询中。只需更改您的联接以引用用户列,如:

ON totalrev.users = ts.s

您也可以不在totalrev子查询中对s列进行别名。

除了上述内容之外,您的ts子查询也没有&#39;&#39;列,所以没有ts.s加入。您需要在ts子查询选择(以及分组依据)中包含此内容,例如:

SELECT session_aggregate.s, session_aggregate.app_timespent AS time_spent_bucket, COUNT(*) AS users
...
GROUP BY session_aggregate.s, session_aggregate.app_timespent