<i id='ap0jb'><tr id='ap0jb'><dt id='ap0jb'><q id='ap0jb'><span id='ap0jb'><b id='ap0jb'><form id='ap0jb'><ins id='ap0jb'></ins><ul id='ap0jb'></ul><sub id='ap0jb'></sub></form><legend id='ap0jb'></legend><bdo id='ap0jb'><pre id='ap0jb'><center id='ap0jb'></center></pre></bdo></b><th id='ap0jb'></th></span></q></dt></tr></i><div id='ap0jb'><tfoot id='ap0jb'></tfoot><dl id='ap0jb'><fieldset id='ap0jb'></fieldset></dl></div>
  1. <small id='ap0jb'></small><noframes id='ap0jb'>

      <tfoot id='ap0jb'></tfoot>
        <bdo id='ap0jb'></bdo><ul id='ap0jb'></ul>

      <legend id='ap0jb'><style id='ap0jb'><dir id='ap0jb'><q id='ap0jb'></q></dir></style></legend>

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

      时间:2023-08-22
        <tbody id='DrmMP'></tbody>
        • <bdo id='DrmMP'></bdo><ul id='DrmMP'></ul>
          <tfoot id='DrmMP'></tfoot>
          <i id='DrmMP'><tr id='DrmMP'><dt id='DrmMP'><q id='DrmMP'><span id='DrmMP'><b id='DrmMP'><form id='DrmMP'><ins id='DrmMP'></ins><ul id='DrmMP'></ul><sub id='DrmMP'></sub></form><legend id='DrmMP'></legend><bdo id='DrmMP'><pre id='DrmMP'><center id='DrmMP'></center></pre></bdo></b><th id='DrmMP'></th></span></q></dt></tr></i><div id='DrmMP'><tfoot id='DrmMP'></tfoot><dl id='DrmMP'><fieldset id='DrmMP'></fieldset></dl></div>

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

              1. <legend id='DrmMP'><style id='DrmMP'><dir id='DrmMP'><q id='DrmMP'></q></dir></style></legend>
                本文介绍了在“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模板网!

                上一篇:在 Apache Spark 中连接到 SQLite 下一篇:Spark SQL/Hive 查询永远需要加入

                相关文章

                最新文章

                  • <bdo id='pyZrq'></bdo><ul id='pyZrq'></ul>
                1. <small id='pyZrq'></small><noframes id='pyZrq'>

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