• <small id='pSPaq'></small><noframes id='pSPaq'>

  • <legend id='pSPaq'><style id='pSPaq'><dir id='pSPaq'><q id='pSPaq'></q></dir></style></legend>
    <i id='pSPaq'><tr id='pSPaq'><dt id='pSPaq'><q id='pSPaq'><span id='pSPaq'><b id='pSPaq'><form id='pSPaq'><ins id='pSPaq'></ins><ul id='pSPaq'></ul><sub id='pSPaq'></sub></form><legend id='pSPaq'></legend><bdo id='pSPaq'><pre id='pSPaq'><center id='pSPaq'></center></pre></bdo></b><th id='pSPaq'></th></span></q></dt></tr></i><div id='pSPaq'><tfoot id='pSPaq'></tfoot><dl id='pSPaq'><fieldset id='pSPaq'></fieldset></dl></div>

    1. <tfoot id='pSPaq'></tfoot>
        • <bdo id='pSPaq'></bdo><ul id='pSPaq'></ul>

        在“GROUP BY"中重用选择表达式的结果;条款

        时间:2023-08-22

          • <legend id='M4oVT'><style id='M4oVT'><dir id='M4oVT'><q id='M4oVT'></q></dir></style></legend>
              <tbody id='M4oVT'></tbody>
            <i id='M4oVT'><tr id='M4oVT'><dt id='M4oVT'><q id='M4oVT'><span id='M4oVT'><b id='M4oVT'><form id='M4oVT'><ins id='M4oVT'></ins><ul id='M4oVT'></ul><sub id='M4oVT'></sub></form><legend id='M4oVT'></legend><bdo id='M4oVT'><pre id='M4oVT'><center id='M4oVT'></center></pre></bdo></b><th id='M4oVT'></th></span></q></dt></tr></i><div id='M4oVT'><tfoot id='M4oVT'></tfoot><dl id='M4oVT'><fieldset id='M4oVT'></fieldset></dl></div>
              • <bdo id='M4oVT'></bdo><ul id='M4oVT'></ul>

                <tfoot id='M4oVT'></tfoot>

                <small id='M4oVT'></small><noframes id='M4oVT'>

                  本文介绍了在“GROUP BY"中重用选择表达式的结果;条款?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  在 MySQL 中,我可以有这样的查询:

                  In MySQL, I can have a query like this:

                  select  
                      cast(from_unixtime(t.time, '%Y-%m-%d %H:00') as datetime) as timeHour
                      , ... 
                  from
                      some_table t 
                  group by
                      timeHour, ...
                  order by
                      timeHour, ...
                  

                  其中 GROUP BY 中的 timeHour 是选择表达式的结果.

                  where timeHour in the GROUP BY is the result of a select expression.

                  但是我刚刚尝试了一个类似于 Sqark SQL 中的查询,我得到了一个错误

                  But I just tried a query similar to that in Sqark SQL, and I got an error of

                  Error: org.apache.spark.sql.AnalysisException: 
                  cannot resolve '`timeHour`' given input columns: ...
                  

                  我对 Spark SQL 的查询是这样的:

                  My query for Spark SQL was this:

                  select  
                        cast(t.unixTime as timestamp) as timeHour
                      , ...
                  from
                      another_table as t
                  group by
                      timeHour, ...
                  order by
                      timeHour, ...
                  

                  这个结构在 Spark SQL 中可行吗?

                  Is this construct possible in Spark SQL?

                  推荐答案

                  这个结构在 Spark SQL 中可行吗?

                  Is this construct possible in Spark SQL?

                  是的,是.您可以通过两种方式使其在 Spark SQL 中工作,以在 GROUP BYORDER BY 子句中使用新列

                  Yes, It is. You can make it works in Spark SQL in 2 ways to use new column in GROUP BY and ORDER BY clauses

                  使用子查询的方法一:

                  SELECT timeHour, someThing FROM (SELECT  
                        from_unixtime((starttime/1000)) AS timeHour
                      , sum(...)                          AS someThing
                      , starttime
                  FROM
                      some_table) 
                  WHERE
                      starttime >= 1000*unix_timestamp('2017-09-16 00:00:00')
                        AND starttime <= 1000*unix_timestamp('2017-09-16 04:00:00')
                  GROUP BY
                      timeHour
                  ORDER BY
                      timeHour
                  LIMIT 10;
                  

                  方法 2 使用 WITH//优雅的方式:

                  -- create alias 
                  WITH table_aliase AS(SELECT  
                        from_unixtime((starttime/1000)) AS timeHour
                      , sum(...)                          AS someThing
                      , starttime
                  FROM
                      some_table)
                  
                  -- use the same alias as table
                  SELECT timeHour, someThing FROM table_aliase
                  WHERE
                      starttime >= 1000*unix_timestamp('2017-09-16 00:00:00')
                        AND starttime <= 1000*unix_timestamp('2017-09-16 04:00:00')
                  GROUP BY
                      timeHour
                  ORDER BY
                      timeHour
                  LIMIT 10;
                  

                  在 Scala 中使用 Spark DataFrame(wo SQL) API 的替代方法:

                  // This code may need additional import to work well
                  
                  val df = .... //load the actual table as df
                  
                  import org.apache.spark.sql.functions._
                  
                  df.withColumn("timeHour", from_unixtime($"starttime"/1000))
                    .groupBy($"timeHour")
                    .agg(sum("...").as("someThing"))
                    .orderBy($"timeHour")
                    .show()
                  
                  //another way - as per eliasah comment
                  df.groupBy(from_unixtime($"starttime"/1000).as("timeHour"))
                    .agg(sum("...").as("someThing"))
                    .orderBy($"timeHour")
                    .show()
                  

                  这篇关于在“GROUP BY"中重用选择表达式的结果;条款?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:Pyspark DataFrameWriter jdbc 函数的 ignore 选项是忽略整 下一篇:如何有效地使用窗口函数根据 N 个先前值来决定

                  相关文章

                  最新文章

                1. <small id='sWR4t'></small><noframes id='sWR4t'>

                  <tfoot id='sWR4t'></tfoot>
                  • <bdo id='sWR4t'></bdo><ul id='sWR4t'></ul>

                    <i id='sWR4t'><tr id='sWR4t'><dt id='sWR4t'><q id='sWR4t'><span id='sWR4t'><b id='sWR4t'><form id='sWR4t'><ins id='sWR4t'></ins><ul id='sWR4t'></ul><sub id='sWR4t'></sub></form><legend id='sWR4t'></legend><bdo id='sWR4t'><pre id='sWR4t'><center id='sWR4t'></center></pre></bdo></b><th id='sWR4t'></th></span></q></dt></tr></i><div id='sWR4t'><tfoot id='sWR4t'></tfoot><dl id='sWR4t'><fieldset id='sWR4t'></fieldset></dl></div>
                      <legend id='sWR4t'><style id='sWR4t'><dir id='sWR4t'><q id='sWR4t'></q></dir></style></legend>