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;
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
add a comment |
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
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
add a comment |
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
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
sql postgresql
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
add a comment |
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
add a comment |
2 Answers
2
active
oldest
votes
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
That is exactly what I was looking for. ´:)
– A Beginner
Nov 16 '18 at 23:19
add a comment |
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);
will the rollup handle theoverall **distinct** count of employees
?
– arahman
Nov 15 '18 at 14:54
@arahman . . . Yes,count(distinct)
works withgrouping sets
androllup()
.
– 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
add a comment |
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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
That is exactly what I was looking for. ´:)
– A Beginner
Nov 16 '18 at 23:19
add a comment |
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
That is exactly what I was looking for. ´:)
– A Beginner
Nov 16 '18 at 23:19
add a comment |
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
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
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
add a comment |
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
add a comment |
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);
will the rollup handle theoverall **distinct** count of employees
?
– arahman
Nov 15 '18 at 14:54
@arahman . . . Yes,count(distinct)
works withgrouping sets
androllup()
.
– 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
add a comment |
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);
will the rollup handle theoverall **distinct** count of employees
?
– arahman
Nov 15 '18 at 14:54
@arahman . . . Yes,count(distinct)
works withgrouping sets
androllup()
.
– 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
add a comment |
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);
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);
edited Nov 15 '18 at 15:46
answered Nov 15 '18 at 14:52
Gordon LinoffGordon Linoff
799k37320426
799k37320426
will the rollup handle theoverall **distinct** count of employees
?
– arahman
Nov 15 '18 at 14:54
@arahman . . . Yes,count(distinct)
works withgrouping sets
androllup()
.
– 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
add a comment |
will the rollup handle theoverall **distinct** count of employees
?
– arahman
Nov 15 '18 at 14:54
@arahman . . . Yes,count(distinct)
works withgrouping sets
androllup()
.
– 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
add a comment |
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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