Is it possible to get total count of employees and employees in each department in a single query?



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








2















I have a table called employee like below:



emp dept
---- ----
1 HR
2 Accounts
3 HR
4 Dev
2 Dev


It is possible for an employee to belong to more than one department, for example in the case of employee 2.



I want an output like this :



dept empInDept totalCountofEmp
----- --------- ----------------
HR 2 4
Accounts 1 4


I can get a count of employees in specified departments like below:



select dept,count(*) as empInDept from employees where dept ='HR' or dept='Accounts' group by dept


But I am not sure if it is possible to create a single query where it is possible to get result of above select query and also get the overall distinct count of employees from this table.










share|improve this question
























  • Can you show your expected output?

    – Eray Balkanli
    Nov 15 '18 at 14:51











  • Tag your question with the database you are using.

    – Gordon Linoff
    Nov 15 '18 at 14:51






  • 1





    a UNION query to combine the two queries together is the first thing which occurs to me

    – ADyson
    Nov 15 '18 at 14:51











  • @ErayBalkanli I have edited my question with the expected output

    – A Beginner
    Nov 15 '18 at 14:55











  • @GordonLinoff I am using Postgres

    – A Beginner
    Nov 15 '18 at 15:18

















2















I have a table called employee like below:



emp dept
---- ----
1 HR
2 Accounts
3 HR
4 Dev
2 Dev


It is possible for an employee to belong to more than one department, for example in the case of employee 2.



I want an output like this :



dept empInDept totalCountofEmp
----- --------- ----------------
HR 2 4
Accounts 1 4


I can get a count of employees in specified departments like below:



select dept,count(*) as empInDept from employees where dept ='HR' or dept='Accounts' group by dept


But I am not sure if it is possible to create a single query where it is possible to get result of above select query and also get the overall distinct count of employees from this table.










share|improve this question
























  • Can you show your expected output?

    – Eray Balkanli
    Nov 15 '18 at 14:51











  • Tag your question with the database you are using.

    – Gordon Linoff
    Nov 15 '18 at 14:51






  • 1





    a UNION query to combine the two queries together is the first thing which occurs to me

    – ADyson
    Nov 15 '18 at 14:51











  • @ErayBalkanli I have edited my question with the expected output

    – A Beginner
    Nov 15 '18 at 14:55











  • @GordonLinoff I am using Postgres

    – A Beginner
    Nov 15 '18 at 15:18













2












2








2








I have a table called employee like below:



emp dept
---- ----
1 HR
2 Accounts
3 HR
4 Dev
2 Dev


It is possible for an employee to belong to more than one department, for example in the case of employee 2.



I want an output like this :



dept empInDept totalCountofEmp
----- --------- ----------------
HR 2 4
Accounts 1 4


I can get a count of employees in specified departments like below:



select dept,count(*) as empInDept from employees where dept ='HR' or dept='Accounts' group by dept


But I am not sure if it is possible to create a single query where it is possible to get result of above select query and also get the overall distinct count of employees from this table.










share|improve this question
















I have a table called employee like below:



emp dept
---- ----
1 HR
2 Accounts
3 HR
4 Dev
2 Dev


It is possible for an employee to belong to more than one department, for example in the case of employee 2.



I want an output like this :



dept empInDept totalCountofEmp
----- --------- ----------------
HR 2 4
Accounts 1 4


I can get a count of employees in specified departments like below:



select dept,count(*) as empInDept from employees where dept ='HR' or dept='Accounts' group by dept


But I am not sure if it is possible to create a single query where it is possible to get result of above select query and also get the overall distinct count of employees from this table.







sql postgresql






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 15 '18 at 14:55







A Beginner

















asked Nov 15 '18 at 14:49









A BeginnerA Beginner

457




457












  • Can you show your expected output?

    – Eray Balkanli
    Nov 15 '18 at 14:51











  • Tag your question with the database you are using.

    – Gordon Linoff
    Nov 15 '18 at 14:51






  • 1





    a UNION query to combine the two queries together is the first thing which occurs to me

    – ADyson
    Nov 15 '18 at 14:51











  • @ErayBalkanli I have edited my question with the expected output

    – A Beginner
    Nov 15 '18 at 14:55











  • @GordonLinoff I am using Postgres

    – A Beginner
    Nov 15 '18 at 15:18

















  • Can you show your expected output?

    – Eray Balkanli
    Nov 15 '18 at 14:51











  • Tag your question with the database you are using.

    – Gordon Linoff
    Nov 15 '18 at 14:51






  • 1





    a UNION query to combine the two queries together is the first thing which occurs to me

    – ADyson
    Nov 15 '18 at 14:51











  • @ErayBalkanli I have edited my question with the expected output

    – A Beginner
    Nov 15 '18 at 14:55











  • @GordonLinoff I am using Postgres

    – A Beginner
    Nov 15 '18 at 15:18
















Can you show your expected output?

– Eray Balkanli
Nov 15 '18 at 14:51





Can you show your expected output?

– Eray Balkanli
Nov 15 '18 at 14:51













Tag your question with the database you are using.

– Gordon Linoff
Nov 15 '18 at 14:51





Tag your question with the database you are using.

– Gordon Linoff
Nov 15 '18 at 14:51




1




1





a UNION query to combine the two queries together is the first thing which occurs to me

– ADyson
Nov 15 '18 at 14:51





a UNION query to combine the two queries together is the first thing which occurs to me

– ADyson
Nov 15 '18 at 14:51













@ErayBalkanli I have edited my question with the expected output

– A Beginner
Nov 15 '18 at 14:55





@ErayBalkanli I have edited my question with the expected output

– A Beginner
Nov 15 '18 at 14:55













@GordonLinoff I am using Postgres

– A Beginner
Nov 15 '18 at 15:18





@GordonLinoff I am using Postgres

– A Beginner
Nov 15 '18 at 15:18












2 Answers
2






active

oldest

votes


















1














Here an example of your request:



create table #temp
(id int, value int)

insert into #temp values (1,2),(2,3),(1,5)

select id, count(Value), (select count(distinct value) from #temp) as X
from #temp
group by id


For your specific task, please try the following:



select dept,
count(*) as empInDept
(select count(distinct emp) from employees) as TotalCount
from employees where dept ='HR' or dept='Accounts'
group by dept





share|improve this answer























  • That is exactly what I was looking for. ´:)

    – A Beginner
    Nov 16 '18 at 23:19


















0














You can use the ANSI/ISO standard rollup or grouping sets:



select dept, count(distinct emp)
from employees
where dept in ('HR', 'Accounts')
group by grouping sets (dept, ());


More databases support rollup, but the syntax can vary. One method is:



select dept, count(distinct emp)
from employees
where dept in ('HR', 'Accounts')
group by rollup(dept);





share|improve this answer

























  • will the rollup handle the overall **distinct** count of employees?

    – arahman
    Nov 15 '18 at 14:54












  • @arahman . . . Yes, count(distinct) works with grouping sets and rollup().

    – Gordon Linoff
    Nov 15 '18 at 15:47











  • Cool, I was just wondering as I have never tried count(distinct) with rollups. It was just for my knowledge :)

    – arahman
    Nov 15 '18 at 15:49











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%2f53322046%2fis-it-possible-to-get-total-count-of-employees-and-employees-in-each-department%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














Here an example of your request:



create table #temp
(id int, value int)

insert into #temp values (1,2),(2,3),(1,5)

select id, count(Value), (select count(distinct value) from #temp) as X
from #temp
group by id


For your specific task, please try the following:



select dept,
count(*) as empInDept
(select count(distinct emp) from employees) as TotalCount
from employees where dept ='HR' or dept='Accounts'
group by dept





share|improve this answer























  • That is exactly what I was looking for. ´:)

    – A Beginner
    Nov 16 '18 at 23:19















1














Here an example of your request:



create table #temp
(id int, value int)

insert into #temp values (1,2),(2,3),(1,5)

select id, count(Value), (select count(distinct value) from #temp) as X
from #temp
group by id


For your specific task, please try the following:



select dept,
count(*) as empInDept
(select count(distinct emp) from employees) as TotalCount
from employees where dept ='HR' or dept='Accounts'
group by dept





share|improve this answer























  • That is exactly what I was looking for. ´:)

    – A Beginner
    Nov 16 '18 at 23:19













1












1








1







Here an example of your request:



create table #temp
(id int, value int)

insert into #temp values (1,2),(2,3),(1,5)

select id, count(Value), (select count(distinct value) from #temp) as X
from #temp
group by id


For your specific task, please try the following:



select dept,
count(*) as empInDept
(select count(distinct emp) from employees) as TotalCount
from employees where dept ='HR' or dept='Accounts'
group by dept





share|improve this answer













Here an example of your request:



create table #temp
(id int, value int)

insert into #temp values (1,2),(2,3),(1,5)

select id, count(Value), (select count(distinct value) from #temp) as X
from #temp
group by id


For your specific task, please try the following:



select dept,
count(*) as empInDept
(select count(distinct emp) from employees) as TotalCount
from employees where dept ='HR' or dept='Accounts'
group by dept






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 15 '18 at 14:57









Eray BalkanliEray Balkanli

4,65852347




4,65852347












  • That is exactly what I was looking for. ´:)

    – A Beginner
    Nov 16 '18 at 23:19

















  • That is exactly what I was looking for. ´:)

    – A Beginner
    Nov 16 '18 at 23:19
















That is exactly what I was looking for. ´:)

– A Beginner
Nov 16 '18 at 23:19





That is exactly what I was looking for. ´:)

– A Beginner
Nov 16 '18 at 23:19













0














You can use the ANSI/ISO standard rollup or grouping sets:



select dept, count(distinct emp)
from employees
where dept in ('HR', 'Accounts')
group by grouping sets (dept, ());


More databases support rollup, but the syntax can vary. One method is:



select dept, count(distinct emp)
from employees
where dept in ('HR', 'Accounts')
group by rollup(dept);





share|improve this answer

























  • will the rollup handle the overall **distinct** count of employees?

    – arahman
    Nov 15 '18 at 14:54












  • @arahman . . . Yes, count(distinct) works with grouping sets and rollup().

    – Gordon Linoff
    Nov 15 '18 at 15:47











  • Cool, I was just wondering as I have never tried count(distinct) with rollups. It was just for my knowledge :)

    – arahman
    Nov 15 '18 at 15:49















0














You can use the ANSI/ISO standard rollup or grouping sets:



select dept, count(distinct emp)
from employees
where dept in ('HR', 'Accounts')
group by grouping sets (dept, ());


More databases support rollup, but the syntax can vary. One method is:



select dept, count(distinct emp)
from employees
where dept in ('HR', 'Accounts')
group by rollup(dept);





share|improve this answer

























  • will the rollup handle the overall **distinct** count of employees?

    – arahman
    Nov 15 '18 at 14:54












  • @arahman . . . Yes, count(distinct) works with grouping sets and rollup().

    – Gordon Linoff
    Nov 15 '18 at 15:47











  • Cool, I was just wondering as I have never tried count(distinct) with rollups. It was just for my knowledge :)

    – arahman
    Nov 15 '18 at 15:49













0












0








0







You can use the ANSI/ISO standard rollup or grouping sets:



select dept, count(distinct emp)
from employees
where dept in ('HR', 'Accounts')
group by grouping sets (dept, ());


More databases support rollup, but the syntax can vary. One method is:



select dept, count(distinct emp)
from employees
where dept in ('HR', 'Accounts')
group by rollup(dept);





share|improve this answer















You can use the ANSI/ISO standard rollup or grouping sets:



select dept, count(distinct emp)
from employees
where dept in ('HR', 'Accounts')
group by grouping sets (dept, ());


More databases support rollup, but the syntax can vary. One method is:



select dept, count(distinct emp)
from employees
where dept in ('HR', 'Accounts')
group by rollup(dept);






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 15 '18 at 15:46

























answered Nov 15 '18 at 14:52









Gordon LinoffGordon Linoff

799k37320426




799k37320426












  • will the rollup handle the overall **distinct** count of employees?

    – arahman
    Nov 15 '18 at 14:54












  • @arahman . . . Yes, count(distinct) works with grouping sets and rollup().

    – Gordon Linoff
    Nov 15 '18 at 15:47











  • Cool, I was just wondering as I have never tried count(distinct) with rollups. It was just for my knowledge :)

    – arahman
    Nov 15 '18 at 15:49

















  • will the rollup handle the overall **distinct** count of employees?

    – arahman
    Nov 15 '18 at 14:54












  • @arahman . . . Yes, count(distinct) works with grouping sets and rollup().

    – Gordon Linoff
    Nov 15 '18 at 15:47











  • Cool, I was just wondering as I have never tried count(distinct) with rollups. It was just for my knowledge :)

    – arahman
    Nov 15 '18 at 15:49
















will the rollup handle the overall **distinct** count of employees?

– arahman
Nov 15 '18 at 14:54






will the rollup handle the overall **distinct** count of employees?

– arahman
Nov 15 '18 at 14:54














@arahman . . . Yes, count(distinct) works with grouping sets and rollup().

– Gordon Linoff
Nov 15 '18 at 15:47





@arahman . . . Yes, count(distinct) works with grouping sets and rollup().

– Gordon Linoff
Nov 15 '18 at 15:47













Cool, I was just wondering as I have never tried count(distinct) with rollups. It was just for my knowledge :)

– arahman
Nov 15 '18 at 15:49





Cool, I was just wondering as I have never tried count(distinct) with rollups. It was just for my knowledge :)

– arahman
Nov 15 '18 at 15:49

















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%2f53322046%2fis-it-possible-to-get-total-count-of-employees-and-employees-in-each-department%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 how show current date and time by default on contact form 7 in WordPress without taking input from user in datetimepicker

Syphilis

Darth Vader #20