Oracle error: ORA-00979: not a GROUP BY expression





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







0















Following is the result of my first query (in Oracle). Totals is the total number of employees in each department (so Employee 1-5 are on department#1, employees 6-8 on department#2 etc.)



select a.name, (select count(*) from employee b where a.dname = b.dname group by b.dname) as totals
from employee a;

NAME TOTALS
------------------------------ ----------------
Employee 1 5
Employee 2 5
Employee 3 5
Employee 4 5
Employee 5 5
Employee 6 3
Employee 7 3
Employee 8 3
Employee 9 4
Employee 10 4
Employee 11 4
Employee 12 4


And here is my second query on which employer has less taskhours than 5 (note "taskhours" is a different field than "totals" in the database)



select a.name
from job o, employee a, works w , task t
where w.name=a.name and w.menuid=o.menuid and w.worktype='office' and t.fname = o.fname
group by a.name
having sum(t.taskhours) <5
;

NAME
------------------------------
Employee 3


I want now combine the above, i.e query for the employees who have fewer task-hours than the total employees in the department. I am trying this query



select a.name
from job o, employee a, works w , task t
where w.name=a.name and w.menuid=o.menuid and w.worktype='office' and t.fname = o.fname
group by a.name
having sum(t.taskhours) <(select count(*) from employee b where a.sname = b.sname group by b.sname)


but I get the error:



ERROR at line 5:
ORA-00979: not a GROUP BY expression


Can someone help me find the right query?



Sample data for 1st query:



NAME                          DNAME
------------------------------ ------------------------------
Employee 1 Dep1
Employee 2 Dep1
Employee 3 Dep1
Employee 4 Dep1
Employee 5 Dep1
Employee 6 Dep2
Employee 7 Dep2
Employee 8 Dep2
Employee 9 Dep3
Employee 10 Dep3
Employee 11 Dep3
Employee 12 Dep3









share|improve this question

























  • Just remove the group by statement in the subquery in the HAVING clause.

    – Sam M
    Nov 23 '18 at 2:14











  • I don't understand your first query. Can you include sample data from the employee table?

    – Tim Biegeleisen
    Nov 23 '18 at 2:19











  • @sam Tried that already, if I remove the group by b.sname I get the same ERROR at line 5: ORA-00979: not a GROUP BY expression

    – bomberb17
    Nov 23 '18 at 2:19













  • @Tim added sample data in my question, sorry I had a typo in the first query

    – bomberb17
    Nov 23 '18 at 2:31


















0















Following is the result of my first query (in Oracle). Totals is the total number of employees in each department (so Employee 1-5 are on department#1, employees 6-8 on department#2 etc.)



select a.name, (select count(*) from employee b where a.dname = b.dname group by b.dname) as totals
from employee a;

NAME TOTALS
------------------------------ ----------------
Employee 1 5
Employee 2 5
Employee 3 5
Employee 4 5
Employee 5 5
Employee 6 3
Employee 7 3
Employee 8 3
Employee 9 4
Employee 10 4
Employee 11 4
Employee 12 4


And here is my second query on which employer has less taskhours than 5 (note "taskhours" is a different field than "totals" in the database)



select a.name
from job o, employee a, works w , task t
where w.name=a.name and w.menuid=o.menuid and w.worktype='office' and t.fname = o.fname
group by a.name
having sum(t.taskhours) <5
;

NAME
------------------------------
Employee 3


I want now combine the above, i.e query for the employees who have fewer task-hours than the total employees in the department. I am trying this query



select a.name
from job o, employee a, works w , task t
where w.name=a.name and w.menuid=o.menuid and w.worktype='office' and t.fname = o.fname
group by a.name
having sum(t.taskhours) <(select count(*) from employee b where a.sname = b.sname group by b.sname)


but I get the error:



ERROR at line 5:
ORA-00979: not a GROUP BY expression


Can someone help me find the right query?



Sample data for 1st query:



NAME                          DNAME
------------------------------ ------------------------------
Employee 1 Dep1
Employee 2 Dep1
Employee 3 Dep1
Employee 4 Dep1
Employee 5 Dep1
Employee 6 Dep2
Employee 7 Dep2
Employee 8 Dep2
Employee 9 Dep3
Employee 10 Dep3
Employee 11 Dep3
Employee 12 Dep3









share|improve this question

























  • Just remove the group by statement in the subquery in the HAVING clause.

    – Sam M
    Nov 23 '18 at 2:14











  • I don't understand your first query. Can you include sample data from the employee table?

    – Tim Biegeleisen
    Nov 23 '18 at 2:19











  • @sam Tried that already, if I remove the group by b.sname I get the same ERROR at line 5: ORA-00979: not a GROUP BY expression

    – bomberb17
    Nov 23 '18 at 2:19













  • @Tim added sample data in my question, sorry I had a typo in the first query

    – bomberb17
    Nov 23 '18 at 2:31














0












0








0








Following is the result of my first query (in Oracle). Totals is the total number of employees in each department (so Employee 1-5 are on department#1, employees 6-8 on department#2 etc.)



select a.name, (select count(*) from employee b where a.dname = b.dname group by b.dname) as totals
from employee a;

NAME TOTALS
------------------------------ ----------------
Employee 1 5
Employee 2 5
Employee 3 5
Employee 4 5
Employee 5 5
Employee 6 3
Employee 7 3
Employee 8 3
Employee 9 4
Employee 10 4
Employee 11 4
Employee 12 4


And here is my second query on which employer has less taskhours than 5 (note "taskhours" is a different field than "totals" in the database)



select a.name
from job o, employee a, works w , task t
where w.name=a.name and w.menuid=o.menuid and w.worktype='office' and t.fname = o.fname
group by a.name
having sum(t.taskhours) <5
;

NAME
------------------------------
Employee 3


I want now combine the above, i.e query for the employees who have fewer task-hours than the total employees in the department. I am trying this query



select a.name
from job o, employee a, works w , task t
where w.name=a.name and w.menuid=o.menuid and w.worktype='office' and t.fname = o.fname
group by a.name
having sum(t.taskhours) <(select count(*) from employee b where a.sname = b.sname group by b.sname)


but I get the error:



ERROR at line 5:
ORA-00979: not a GROUP BY expression


Can someone help me find the right query?



Sample data for 1st query:



NAME                          DNAME
------------------------------ ------------------------------
Employee 1 Dep1
Employee 2 Dep1
Employee 3 Dep1
Employee 4 Dep1
Employee 5 Dep1
Employee 6 Dep2
Employee 7 Dep2
Employee 8 Dep2
Employee 9 Dep3
Employee 10 Dep3
Employee 11 Dep3
Employee 12 Dep3









share|improve this question
















Following is the result of my first query (in Oracle). Totals is the total number of employees in each department (so Employee 1-5 are on department#1, employees 6-8 on department#2 etc.)



select a.name, (select count(*) from employee b where a.dname = b.dname group by b.dname) as totals
from employee a;

NAME TOTALS
------------------------------ ----------------
Employee 1 5
Employee 2 5
Employee 3 5
Employee 4 5
Employee 5 5
Employee 6 3
Employee 7 3
Employee 8 3
Employee 9 4
Employee 10 4
Employee 11 4
Employee 12 4


And here is my second query on which employer has less taskhours than 5 (note "taskhours" is a different field than "totals" in the database)



select a.name
from job o, employee a, works w , task t
where w.name=a.name and w.menuid=o.menuid and w.worktype='office' and t.fname = o.fname
group by a.name
having sum(t.taskhours) <5
;

NAME
------------------------------
Employee 3


I want now combine the above, i.e query for the employees who have fewer task-hours than the total employees in the department. I am trying this query



select a.name
from job o, employee a, works w , task t
where w.name=a.name and w.menuid=o.menuid and w.worktype='office' and t.fname = o.fname
group by a.name
having sum(t.taskhours) <(select count(*) from employee b where a.sname = b.sname group by b.sname)


but I get the error:



ERROR at line 5:
ORA-00979: not a GROUP BY expression


Can someone help me find the right query?



Sample data for 1st query:



NAME                          DNAME
------------------------------ ------------------------------
Employee 1 Dep1
Employee 2 Dep1
Employee 3 Dep1
Employee 4 Dep1
Employee 5 Dep1
Employee 6 Dep2
Employee 7 Dep2
Employee 8 Dep2
Employee 9 Dep3
Employee 10 Dep3
Employee 11 Dep3
Employee 12 Dep3






sql oracle






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 23 '18 at 2:27







bomberb17

















asked Nov 23 '18 at 2:04









bomberb17bomberb17

8429




8429













  • Just remove the group by statement in the subquery in the HAVING clause.

    – Sam M
    Nov 23 '18 at 2:14











  • I don't understand your first query. Can you include sample data from the employee table?

    – Tim Biegeleisen
    Nov 23 '18 at 2:19











  • @sam Tried that already, if I remove the group by b.sname I get the same ERROR at line 5: ORA-00979: not a GROUP BY expression

    – bomberb17
    Nov 23 '18 at 2:19













  • @Tim added sample data in my question, sorry I had a typo in the first query

    – bomberb17
    Nov 23 '18 at 2:31



















  • Just remove the group by statement in the subquery in the HAVING clause.

    – Sam M
    Nov 23 '18 at 2:14











  • I don't understand your first query. Can you include sample data from the employee table?

    – Tim Biegeleisen
    Nov 23 '18 at 2:19











  • @sam Tried that already, if I remove the group by b.sname I get the same ERROR at line 5: ORA-00979: not a GROUP BY expression

    – bomberb17
    Nov 23 '18 at 2:19













  • @Tim added sample data in my question, sorry I had a typo in the first query

    – bomberb17
    Nov 23 '18 at 2:31

















Just remove the group by statement in the subquery in the HAVING clause.

– Sam M
Nov 23 '18 at 2:14





Just remove the group by statement in the subquery in the HAVING clause.

– Sam M
Nov 23 '18 at 2:14













I don't understand your first query. Can you include sample data from the employee table?

– Tim Biegeleisen
Nov 23 '18 at 2:19





I don't understand your first query. Can you include sample data from the employee table?

– Tim Biegeleisen
Nov 23 '18 at 2:19













@sam Tried that already, if I remove the group by b.sname I get the same ERROR at line 5: ORA-00979: not a GROUP BY expression

– bomberb17
Nov 23 '18 at 2:19







@sam Tried that already, if I remove the group by b.sname I get the same ERROR at line 5: ORA-00979: not a GROUP BY expression

– bomberb17
Nov 23 '18 at 2:19















@Tim added sample data in my question, sorry I had a typo in the first query

– bomberb17
Nov 23 '18 at 2:31





@Tim added sample data in my question, sorry I had a typo in the first query

– bomberb17
Nov 23 '18 at 2:31












2 Answers
2






active

oldest

votes


















1














We can phrase this by putting each of your two queries into subqueries, and then joining them together:



SELECT
e.name,
e.dname,
t1.emp_cnt,
t2.hours_cnt
FROM employee e
LEFT JOIN
(
SELECT dname, COUNT(*) AS emp_cnt
FROM employee
GROUP BY dname
) t1
ON e.dname = t1.dname
LEFT JOIN
(
SELECT a.name, SUM(t.taskhours) AS hours_cnt
FROM works w
INNER JOIN employee a
ON w.name = a.name
INNER JOIN job o
ON w.menuid = o.menuid
INNER JOIN task t
ON t.fname = o.fname
WHERE w.worktype = 'office'
GROUP BY a.name
) t2
ON e.name = t2.name
WHERE
t1.emp_cnt < t2.hours_cnt;





share|improve this answer
























  • Worked perfectly, thanks!

    – bomberb17
    Nov 23 '18 at 2:54



















1














You can simplify the first query as follows



use that as a cte block



with tot_count
as (select dept_id,count(emp_id) as dept_cnt
from employee
group by dept_id)
,less_than_five
as (select a.name
,sum(t.taskhours) as sum_taskhours
,max(a.dept_id) as dept_id_of_emp
from job o,
employee a,
works w ,
task t
where w.name=a.name
and w.menuid=o.menuid
and w.worktype='office'
and t.fname = o.fname
group by a.name
)
select *
from less_than_five a
join tot_count b
on a.dept_id_of_emp=b.dept_id
where less_than_five.sum_taskhours<=b.dept_cnt





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',
    autoActivateHeartbeat: false,
    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%2f53439852%2foracle-error-ora-00979-not-a-group-by-expression%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    We can phrase this by putting each of your two queries into subqueries, and then joining them together:



    SELECT
    e.name,
    e.dname,
    t1.emp_cnt,
    t2.hours_cnt
    FROM employee e
    LEFT JOIN
    (
    SELECT dname, COUNT(*) AS emp_cnt
    FROM employee
    GROUP BY dname
    ) t1
    ON e.dname = t1.dname
    LEFT JOIN
    (
    SELECT a.name, SUM(t.taskhours) AS hours_cnt
    FROM works w
    INNER JOIN employee a
    ON w.name = a.name
    INNER JOIN job o
    ON w.menuid = o.menuid
    INNER JOIN task t
    ON t.fname = o.fname
    WHERE w.worktype = 'office'
    GROUP BY a.name
    ) t2
    ON e.name = t2.name
    WHERE
    t1.emp_cnt < t2.hours_cnt;





    share|improve this answer
























    • Worked perfectly, thanks!

      – bomberb17
      Nov 23 '18 at 2:54
















    1














    We can phrase this by putting each of your two queries into subqueries, and then joining them together:



    SELECT
    e.name,
    e.dname,
    t1.emp_cnt,
    t2.hours_cnt
    FROM employee e
    LEFT JOIN
    (
    SELECT dname, COUNT(*) AS emp_cnt
    FROM employee
    GROUP BY dname
    ) t1
    ON e.dname = t1.dname
    LEFT JOIN
    (
    SELECT a.name, SUM(t.taskhours) AS hours_cnt
    FROM works w
    INNER JOIN employee a
    ON w.name = a.name
    INNER JOIN job o
    ON w.menuid = o.menuid
    INNER JOIN task t
    ON t.fname = o.fname
    WHERE w.worktype = 'office'
    GROUP BY a.name
    ) t2
    ON e.name = t2.name
    WHERE
    t1.emp_cnt < t2.hours_cnt;





    share|improve this answer
























    • Worked perfectly, thanks!

      – bomberb17
      Nov 23 '18 at 2:54














    1












    1








    1







    We can phrase this by putting each of your two queries into subqueries, and then joining them together:



    SELECT
    e.name,
    e.dname,
    t1.emp_cnt,
    t2.hours_cnt
    FROM employee e
    LEFT JOIN
    (
    SELECT dname, COUNT(*) AS emp_cnt
    FROM employee
    GROUP BY dname
    ) t1
    ON e.dname = t1.dname
    LEFT JOIN
    (
    SELECT a.name, SUM(t.taskhours) AS hours_cnt
    FROM works w
    INNER JOIN employee a
    ON w.name = a.name
    INNER JOIN job o
    ON w.menuid = o.menuid
    INNER JOIN task t
    ON t.fname = o.fname
    WHERE w.worktype = 'office'
    GROUP BY a.name
    ) t2
    ON e.name = t2.name
    WHERE
    t1.emp_cnt < t2.hours_cnt;





    share|improve this answer













    We can phrase this by putting each of your two queries into subqueries, and then joining them together:



    SELECT
    e.name,
    e.dname,
    t1.emp_cnt,
    t2.hours_cnt
    FROM employee e
    LEFT JOIN
    (
    SELECT dname, COUNT(*) AS emp_cnt
    FROM employee
    GROUP BY dname
    ) t1
    ON e.dname = t1.dname
    LEFT JOIN
    (
    SELECT a.name, SUM(t.taskhours) AS hours_cnt
    FROM works w
    INNER JOIN employee a
    ON w.name = a.name
    INNER JOIN job o
    ON w.menuid = o.menuid
    INNER JOIN task t
    ON t.fname = o.fname
    WHERE w.worktype = 'office'
    GROUP BY a.name
    ) t2
    ON e.name = t2.name
    WHERE
    t1.emp_cnt < t2.hours_cnt;






    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Nov 23 '18 at 2:45









    Tim BiegeleisenTim Biegeleisen

    240k13100160




    240k13100160













    • Worked perfectly, thanks!

      – bomberb17
      Nov 23 '18 at 2:54



















    • Worked perfectly, thanks!

      – bomberb17
      Nov 23 '18 at 2:54

















    Worked perfectly, thanks!

    – bomberb17
    Nov 23 '18 at 2:54





    Worked perfectly, thanks!

    – bomberb17
    Nov 23 '18 at 2:54













    1














    You can simplify the first query as follows



    use that as a cte block



    with tot_count
    as (select dept_id,count(emp_id) as dept_cnt
    from employee
    group by dept_id)
    ,less_than_five
    as (select a.name
    ,sum(t.taskhours) as sum_taskhours
    ,max(a.dept_id) as dept_id_of_emp
    from job o,
    employee a,
    works w ,
    task t
    where w.name=a.name
    and w.menuid=o.menuid
    and w.worktype='office'
    and t.fname = o.fname
    group by a.name
    )
    select *
    from less_than_five a
    join tot_count b
    on a.dept_id_of_emp=b.dept_id
    where less_than_five.sum_taskhours<=b.dept_cnt





    share|improve this answer




























      1














      You can simplify the first query as follows



      use that as a cte block



      with tot_count
      as (select dept_id,count(emp_id) as dept_cnt
      from employee
      group by dept_id)
      ,less_than_five
      as (select a.name
      ,sum(t.taskhours) as sum_taskhours
      ,max(a.dept_id) as dept_id_of_emp
      from job o,
      employee a,
      works w ,
      task t
      where w.name=a.name
      and w.menuid=o.menuid
      and w.worktype='office'
      and t.fname = o.fname
      group by a.name
      )
      select *
      from less_than_five a
      join tot_count b
      on a.dept_id_of_emp=b.dept_id
      where less_than_five.sum_taskhours<=b.dept_cnt





      share|improve this answer


























        1












        1








        1







        You can simplify the first query as follows



        use that as a cte block



        with tot_count
        as (select dept_id,count(emp_id) as dept_cnt
        from employee
        group by dept_id)
        ,less_than_five
        as (select a.name
        ,sum(t.taskhours) as sum_taskhours
        ,max(a.dept_id) as dept_id_of_emp
        from job o,
        employee a,
        works w ,
        task t
        where w.name=a.name
        and w.menuid=o.menuid
        and w.worktype='office'
        and t.fname = o.fname
        group by a.name
        )
        select *
        from less_than_five a
        join tot_count b
        on a.dept_id_of_emp=b.dept_id
        where less_than_five.sum_taskhours<=b.dept_cnt





        share|improve this answer













        You can simplify the first query as follows



        use that as a cte block



        with tot_count
        as (select dept_id,count(emp_id) as dept_cnt
        from employee
        group by dept_id)
        ,less_than_five
        as (select a.name
        ,sum(t.taskhours) as sum_taskhours
        ,max(a.dept_id) as dept_id_of_emp
        from job o,
        employee a,
        works w ,
        task t
        where w.name=a.name
        and w.menuid=o.menuid
        and w.worktype='office'
        and t.fname = o.fname
        group by a.name
        )
        select *
        from less_than_five a
        join tot_count b
        on a.dept_id_of_emp=b.dept_id
        where less_than_five.sum_taskhours<=b.dept_cnt






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 23 '18 at 2:20









        George JosephGeorge Joseph

        1,590510




        1,590510






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53439852%2foracle-error-ora-00979-not-a-group-by-expression%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

            How to change which sound is reproduced for terminal bell?

            Can I use Tabulator js library in my java Spring + Thymeleaf project?

            Title Spacing in Bjornstrup Chapter, Removing Chapter Number From Contents