【问题标题】:Redshift this type of correlated subquery pattern is not supported due to internal error由于内部错误,不支持 Redshift 这种类型的相关子查询模式
【发布时间】:2015-12-10 18:04:23
【问题描述】:

当我运行第一个查询时,一切正常:

select uid,
(select top 1 city from UserData where UserData.uid = #uids.uid 
group by city)
from #uids;

问题是当我添加order by count(city)时,我得到一个错误:这种类型的相关子查询模式不支持,因为内部错误

select uid,
(select top 1 city from UserData where UserData.uid = #uids.uid 
group by city
order by count(city))
from #uids;

【问题讨论】:

    标签: amazon-redshift


    【解决方案1】:

    我会在 RedShift 中使用 row_number() 来做到这一点:

    select u.uid, ud.city
    from uids u left join
         (select uid, city, count(*) as cnt,
                 row_number() over (partition by uid order by count(*) desc) as seqnum
          from UserData ud
          group by uid, city
         ) ud
         on ud.uid = u.uid and seqnum = 1;
    

    您也可以在 SQL Server 中尝试此操作。

    【讨论】:

      【解决方案2】:

      错误表明您的子查询不起作用,这很明显,因为您不能在 SQL 的 ORDER 子句中使用函数。您只能在 SELECT 和 HAVING 子句中使用它们。

      如果您想按count(city) 排序,则应将其添加到带有别名的 SELECT 子句并按别名排序。所以你的子查询可以是这样的:

      (SELECT city, COUNT(*) AS city_count
      FROM UserData
      WHERE UserData.uid = #uids.uid
      GROUP BY city
      ORDER BY city_count
      LIMIT 1)
      

      【讨论】:

      • “因为您不能在 SQL 的 ORDER 子句中使用函数”。您可能需要重新检查您的声明
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多