Getting ParseException when running Hive query









up vote
0
down vote

favorite












I'm trying to find the number of employees who are paid less than average wage.



I'm pretty new to hive and struggling a bit, could someone explain whats wrong with my statement and help me out please?



My statement -



SELECT COUNT(*) FROM(SELECT wage, AVG(wage) AS avgWage FROM emp_wages) WHERE wage < avgWage;


The error -



ParseException line 1:82 cannot recognize input near 'where' 'wage' '<' in subquery source



Any help appreciated!










share|improve this question

























    up vote
    0
    down vote

    favorite












    I'm trying to find the number of employees who are paid less than average wage.



    I'm pretty new to hive and struggling a bit, could someone explain whats wrong with my statement and help me out please?



    My statement -



    SELECT COUNT(*) FROM(SELECT wage, AVG(wage) AS avgWage FROM emp_wages) WHERE wage < avgWage;


    The error -



    ParseException line 1:82 cannot recognize input near 'where' 'wage' '<' in subquery source



    Any help appreciated!










    share|improve this question























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      I'm trying to find the number of employees who are paid less than average wage.



      I'm pretty new to hive and struggling a bit, could someone explain whats wrong with my statement and help me out please?



      My statement -



      SELECT COUNT(*) FROM(SELECT wage, AVG(wage) AS avgWage FROM emp_wages) WHERE wage < avgWage;


      The error -



      ParseException line 1:82 cannot recognize input near 'where' 'wage' '<' in subquery source



      Any help appreciated!










      share|improve this question













      I'm trying to find the number of employees who are paid less than average wage.



      I'm pretty new to hive and struggling a bit, could someone explain whats wrong with my statement and help me out please?



      My statement -



      SELECT COUNT(*) FROM(SELECT wage, AVG(wage) AS avgWage FROM emp_wages) WHERE wage < avgWage;


      The error -



      ParseException line 1:82 cannot recognize input near 'where' 'wage' '<' in subquery source



      Any help appreciated!







      hadoop hive hql parseexception






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 9 at 19:52









      Gus

      766




      766






















          4 Answers
          4






          active

          oldest

          votes

















          up vote
          1
          down vote



          accepted










          A syntax error. Derived table should be aliased.



          SELECT COUNT(*) 
          FROM (SELECT wage, AVG(wage) AS avgWage FROM emp_wages group by wage) t --alias needed here
          WHERE wage < avgWage;


          Query wise, it needs a change.



          select count(*)
          from (SELECT wage, AVG(wage) over() AS avgWage
          FROM emp_wages
          ) t
          where wage < avgWage





          share|improve this answer






















          • Your 2nd HQL is right. But isn't your 1st HQL need group by for aggregation
            – saravanatn
            Nov 10 at 1:40






          • 1




            You’re right..edited it to be syntactically correct.
            – Vamsi Prabhala
            Nov 10 at 4:34

















          up vote
          0
          down vote













          SELECT COUNT(*) 
          FROM (SELECT wage, AVG(wage) AS avgWage FROM emp_wages group by wage)avg --group by needed
          WHERE wage < avgWage;





          share|improve this answer



























            up vote
            0
            down vote













            The problem is AVG is an aggregation function. If you want to map one to many relations, you need to use a cross join function:



            select 
            count(*), avg(v1.wage),
            sum(case when v.wage < v2.avgwage then 1 else 0 end) below_average
            from
            emp_wages v cross join (select avg(wage) as avgwage from emp_wages) as v2





            share|improve this answer





























              up vote
              0
              down vote













              The correct query would be:



              select count(*) where wage <(select avg(wage) from emp_wages);


              You are getting a parsing error as wage and avgWage is in subquery.






              share|improve this answer




















                Your Answer






                StackExchange.ifUsing("editor", function ()
                StackExchange.using("externalEditor", function ()
                StackExchange.using("snippets", function ()
                StackExchange.snippets.init();
                );
                );
                , "code-snippets");

                StackExchange.ready(function()
                var channelOptions =
                tags: "".split(" "),
                id: "1"
                ;
                initTagRenderer("".split(" "), "".split(" "), channelOptions);

                StackExchange.using("externalEditor", function()
                // Have to fire editor after snippets, if snippets enabled
                if (StackExchange.settings.snippets.snippetsEnabled)
                StackExchange.using("snippets", function()
                createEditor();
                );

                else
                createEditor();

                );

                function createEditor()
                StackExchange.prepareEditor(
                heartbeatType: 'answer',
                convertImagesToLinks: true,
                noModals: true,
                showLowRepImageUploadWarning: true,
                reputationToPostImages: 10,
                bindNavPrevention: true,
                postfix: "",
                imageUploader:
                brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
                contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
                allowUrls: true
                ,
                onDemand: true,
                discardSelector: ".discard-answer"
                ,immediatelyShowMarkdownHelp:true
                );



                );













                 

                draft saved


                draft discarded


















                StackExchange.ready(
                function ()
                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53232448%2fgetting-parseexception-when-running-hive-query%23new-answer', 'question_page');

                );

                Post as a guest















                Required, but never shown

























                4 Answers
                4






                active

                oldest

                votes








                4 Answers
                4






                active

                oldest

                votes









                active

                oldest

                votes






                active

                oldest

                votes








                up vote
                1
                down vote



                accepted










                A syntax error. Derived table should be aliased.



                SELECT COUNT(*) 
                FROM (SELECT wage, AVG(wage) AS avgWage FROM emp_wages group by wage) t --alias needed here
                WHERE wage < avgWage;


                Query wise, it needs a change.



                select count(*)
                from (SELECT wage, AVG(wage) over() AS avgWage
                FROM emp_wages
                ) t
                where wage < avgWage





                share|improve this answer






















                • Your 2nd HQL is right. But isn't your 1st HQL need group by for aggregation
                  – saravanatn
                  Nov 10 at 1:40






                • 1




                  You’re right..edited it to be syntactically correct.
                  – Vamsi Prabhala
                  Nov 10 at 4:34














                up vote
                1
                down vote



                accepted










                A syntax error. Derived table should be aliased.



                SELECT COUNT(*) 
                FROM (SELECT wage, AVG(wage) AS avgWage FROM emp_wages group by wage) t --alias needed here
                WHERE wage < avgWage;


                Query wise, it needs a change.



                select count(*)
                from (SELECT wage, AVG(wage) over() AS avgWage
                FROM emp_wages
                ) t
                where wage < avgWage





                share|improve this answer






















                • Your 2nd HQL is right. But isn't your 1st HQL need group by for aggregation
                  – saravanatn
                  Nov 10 at 1:40






                • 1




                  You’re right..edited it to be syntactically correct.
                  – Vamsi Prabhala
                  Nov 10 at 4:34












                up vote
                1
                down vote



                accepted







                up vote
                1
                down vote



                accepted






                A syntax error. Derived table should be aliased.



                SELECT COUNT(*) 
                FROM (SELECT wage, AVG(wage) AS avgWage FROM emp_wages group by wage) t --alias needed here
                WHERE wage < avgWage;


                Query wise, it needs a change.



                select count(*)
                from (SELECT wage, AVG(wage) over() AS avgWage
                FROM emp_wages
                ) t
                where wage < avgWage





                share|improve this answer














                A syntax error. Derived table should be aliased.



                SELECT COUNT(*) 
                FROM (SELECT wage, AVG(wage) AS avgWage FROM emp_wages group by wage) t --alias needed here
                WHERE wage < avgWage;


                Query wise, it needs a change.



                select count(*)
                from (SELECT wage, AVG(wage) over() AS avgWage
                FROM emp_wages
                ) t
                where wage < avgWage






                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Nov 10 at 4:33

























                answered Nov 9 at 20:16









                Vamsi Prabhala

                39.6k41939




                39.6k41939











                • Your 2nd HQL is right. But isn't your 1st HQL need group by for aggregation
                  – saravanatn
                  Nov 10 at 1:40






                • 1




                  You’re right..edited it to be syntactically correct.
                  – Vamsi Prabhala
                  Nov 10 at 4:34
















                • Your 2nd HQL is right. But isn't your 1st HQL need group by for aggregation
                  – saravanatn
                  Nov 10 at 1:40






                • 1




                  You’re right..edited it to be syntactically correct.
                  – Vamsi Prabhala
                  Nov 10 at 4:34















                Your 2nd HQL is right. But isn't your 1st HQL need group by for aggregation
                – saravanatn
                Nov 10 at 1:40




                Your 2nd HQL is right. But isn't your 1st HQL need group by for aggregation
                – saravanatn
                Nov 10 at 1:40




                1




                1




                You’re right..edited it to be syntactically correct.
                – Vamsi Prabhala
                Nov 10 at 4:34




                You’re right..edited it to be syntactically correct.
                – Vamsi Prabhala
                Nov 10 at 4:34












                up vote
                0
                down vote













                SELECT COUNT(*) 
                FROM (SELECT wage, AVG(wage) AS avgWage FROM emp_wages group by wage)avg --group by needed
                WHERE wage < avgWage;





                share|improve this answer
























                  up vote
                  0
                  down vote













                  SELECT COUNT(*) 
                  FROM (SELECT wage, AVG(wage) AS avgWage FROM emp_wages group by wage)avg --group by needed
                  WHERE wage < avgWage;





                  share|improve this answer






















                    up vote
                    0
                    down vote










                    up vote
                    0
                    down vote









                    SELECT COUNT(*) 
                    FROM (SELECT wage, AVG(wage) AS avgWage FROM emp_wages group by wage)avg --group by needed
                    WHERE wage < avgWage;





                    share|improve this answer












                    SELECT COUNT(*) 
                    FROM (SELECT wage, AVG(wage) AS avgWage FROM emp_wages group by wage)avg --group by needed
                    WHERE wage < avgWage;






                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Nov 10 at 1:39









                    saravanatn

                    37617




                    37617




















                        up vote
                        0
                        down vote













                        The problem is AVG is an aggregation function. If you want to map one to many relations, you need to use a cross join function:



                        select 
                        count(*), avg(v1.wage),
                        sum(case when v.wage < v2.avgwage then 1 else 0 end) below_average
                        from
                        emp_wages v cross join (select avg(wage) as avgwage from emp_wages) as v2





                        share|improve this answer


























                          up vote
                          0
                          down vote













                          The problem is AVG is an aggregation function. If you want to map one to many relations, you need to use a cross join function:



                          select 
                          count(*), avg(v1.wage),
                          sum(case when v.wage < v2.avgwage then 1 else 0 end) below_average
                          from
                          emp_wages v cross join (select avg(wage) as avgwage from emp_wages) as v2





                          share|improve this answer
























                            up vote
                            0
                            down vote










                            up vote
                            0
                            down vote









                            The problem is AVG is an aggregation function. If you want to map one to many relations, you need to use a cross join function:



                            select 
                            count(*), avg(v1.wage),
                            sum(case when v.wage < v2.avgwage then 1 else 0 end) below_average
                            from
                            emp_wages v cross join (select avg(wage) as avgwage from emp_wages) as v2





                            share|improve this answer














                            The problem is AVG is an aggregation function. If you want to map one to many relations, you need to use a cross join function:



                            select 
                            count(*), avg(v1.wage),
                            sum(case when v.wage < v2.avgwage then 1 else 0 end) below_average
                            from
                            emp_wages v cross join (select avg(wage) as avgwage from emp_wages) as v2






                            share|improve this answer














                            share|improve this answer



                            share|improve this answer








                            edited Nov 10 at 3:07









                            Pang

                            6,8011563101




                            6,8011563101










                            answered Nov 9 at 21:58









                            VIN

                            7411




                            7411




















                                up vote
                                0
                                down vote













                                The correct query would be:



                                select count(*) where wage <(select avg(wage) from emp_wages);


                                You are getting a parsing error as wage and avgWage is in subquery.






                                share|improve this answer
























                                  up vote
                                  0
                                  down vote













                                  The correct query would be:



                                  select count(*) where wage <(select avg(wage) from emp_wages);


                                  You are getting a parsing error as wage and avgWage is in subquery.






                                  share|improve this answer






















                                    up vote
                                    0
                                    down vote










                                    up vote
                                    0
                                    down vote









                                    The correct query would be:



                                    select count(*) where wage <(select avg(wage) from emp_wages);


                                    You are getting a parsing error as wage and avgWage is in subquery.






                                    share|improve this answer












                                    The correct query would be:



                                    select count(*) where wage <(select avg(wage) from emp_wages);


                                    You are getting a parsing error as wage and avgWage is in subquery.







                                    share|improve this answer












                                    share|improve this answer



                                    share|improve this answer










                                    answered Nov 12 at 6:18









                                    Ruben Bhattacharya

                                    465




                                    465



























                                         

                                        draft saved


                                        draft discarded















































                                         


                                        draft saved


                                        draft discarded














                                        StackExchange.ready(
                                        function ()
                                        StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53232448%2fgetting-parseexception-when-running-hive-query%23new-answer', 'question_page');

                                        );

                                        Post as a guest















                                        Required, but never shown





















































                                        Required, but never shown














                                        Required, but never shown












                                        Required, but never shown







                                        Required, but never shown

































                                        Required, but never shown














                                        Required, but never shown












                                        Required, but never shown







                                        Required, but never shown







                                        Popular posts from this blog

                                        Use pre created SQLite database for Android project in kotlin

                                        Darth Vader #20

                                        Ondo