How to perform count(), count(distinct) in the same output using POSTGRESQL
I am trying to perform both count(distinct(keyword)) and count(keyword) from same tables. Here are the two queries.
Query-1
SELECT t1.count(distinct(keyword)) from keyword t1, sent t2,tweets t3,users t4
WHERE t4.user_id='18839785'
AND t1.id=t2.id AND t2.id=t3.id AND t3.user_id=t4.user_id AND t2.sent='Pos';
count
-------
251
(1 row)
Query-2
SELECT t1.keyword,t1.count(keyword) FROM keyword t1, sent t2,tweets t3,users t4
WHERE t4.name='narendramodi' AND t3.id=t4.user_id AND t2.id=t3.id
AND t2.id=t1.id AND t2.sent='Pos' GROUP BY t1.nlp_keyword_name order by count(keyword) desc ;
nlp_keyword_name | count
------------------+-------
happy | 5
people | 4
handsome | 4
glad | 3
forward | 3
wishes | 3
proud | 2
(7 rows)
Query-3
SELECT distinct_drivers,positive_drivers,countt
from (
SELECT t1.count(distinct(keyword)) as distinct_drivers ,
t1.nlp_keyword_name as P_drivers,
t1.count(nlp_keyword_name) as countt
FROM keyword t1 LEFT JOIN sent t2 on t1.id=t2.id
LEFT JOIN tweets t3 on t3.id=t2.id
LEFT JOIN users t4 on t4.user_id=t3.user_id
WHERE (t4.user_id)=('18839785') AND t2.sent='Pos' GROUP BY t1.nlp_keyword_name order by countt desc) T;
But I am getting my output as
distinct_drivers | positive_drivers | countt
------------------+------------------+--------
1 | happy | 5
1 | people | 4
1 | handsome | 4
1 | glad | 3
1 | forward | 3
Here after executing query-3 my output has to be below way which I am not getting like that.
**desired_output:**
distinct_drivers | positive_drivers | countt
------------------+------------------+--------
251 | happy | 5
251 | people | 4
251 | handsome | 4
251 | glad | 3
251 | forward | 3
Can anyone help me out in achieving desired output. I will be thankful.
resources I have gone through Rolling sum / count / average over date interval
postgresql-9.4
add a comment |
I am trying to perform both count(distinct(keyword)) and count(keyword) from same tables. Here are the two queries.
Query-1
SELECT t1.count(distinct(keyword)) from keyword t1, sent t2,tweets t3,users t4
WHERE t4.user_id='18839785'
AND t1.id=t2.id AND t2.id=t3.id AND t3.user_id=t4.user_id AND t2.sent='Pos';
count
-------
251
(1 row)
Query-2
SELECT t1.keyword,t1.count(keyword) FROM keyword t1, sent t2,tweets t3,users t4
WHERE t4.name='narendramodi' AND t3.id=t4.user_id AND t2.id=t3.id
AND t2.id=t1.id AND t2.sent='Pos' GROUP BY t1.nlp_keyword_name order by count(keyword) desc ;
nlp_keyword_name | count
------------------+-------
happy | 5
people | 4
handsome | 4
glad | 3
forward | 3
wishes | 3
proud | 2
(7 rows)
Query-3
SELECT distinct_drivers,positive_drivers,countt
from (
SELECT t1.count(distinct(keyword)) as distinct_drivers ,
t1.nlp_keyword_name as P_drivers,
t1.count(nlp_keyword_name) as countt
FROM keyword t1 LEFT JOIN sent t2 on t1.id=t2.id
LEFT JOIN tweets t3 on t3.id=t2.id
LEFT JOIN users t4 on t4.user_id=t3.user_id
WHERE (t4.user_id)=('18839785') AND t2.sent='Pos' GROUP BY t1.nlp_keyword_name order by countt desc) T;
But I am getting my output as
distinct_drivers | positive_drivers | countt
------------------+------------------+--------
1 | happy | 5
1 | people | 4
1 | handsome | 4
1 | glad | 3
1 | forward | 3
Here after executing query-3 my output has to be below way which I am not getting like that.
**desired_output:**
distinct_drivers | positive_drivers | countt
------------------+------------------+--------
251 | happy | 5
251 | people | 4
251 | handsome | 4
251 | glad | 3
251 | forward | 3
Can anyone help me out in achieving desired output. I will be thankful.
resources I have gone through Rolling sum / count / average over date interval
postgresql-9.4
add a comment |
I am trying to perform both count(distinct(keyword)) and count(keyword) from same tables. Here are the two queries.
Query-1
SELECT t1.count(distinct(keyword)) from keyword t1, sent t2,tweets t3,users t4
WHERE t4.user_id='18839785'
AND t1.id=t2.id AND t2.id=t3.id AND t3.user_id=t4.user_id AND t2.sent='Pos';
count
-------
251
(1 row)
Query-2
SELECT t1.keyword,t1.count(keyword) FROM keyword t1, sent t2,tweets t3,users t4
WHERE t4.name='narendramodi' AND t3.id=t4.user_id AND t2.id=t3.id
AND t2.id=t1.id AND t2.sent='Pos' GROUP BY t1.nlp_keyword_name order by count(keyword) desc ;
nlp_keyword_name | count
------------------+-------
happy | 5
people | 4
handsome | 4
glad | 3
forward | 3
wishes | 3
proud | 2
(7 rows)
Query-3
SELECT distinct_drivers,positive_drivers,countt
from (
SELECT t1.count(distinct(keyword)) as distinct_drivers ,
t1.nlp_keyword_name as P_drivers,
t1.count(nlp_keyword_name) as countt
FROM keyword t1 LEFT JOIN sent t2 on t1.id=t2.id
LEFT JOIN tweets t3 on t3.id=t2.id
LEFT JOIN users t4 on t4.user_id=t3.user_id
WHERE (t4.user_id)=('18839785') AND t2.sent='Pos' GROUP BY t1.nlp_keyword_name order by countt desc) T;
But I am getting my output as
distinct_drivers | positive_drivers | countt
------------------+------------------+--------
1 | happy | 5
1 | people | 4
1 | handsome | 4
1 | glad | 3
1 | forward | 3
Here after executing query-3 my output has to be below way which I am not getting like that.
**desired_output:**
distinct_drivers | positive_drivers | countt
------------------+------------------+--------
251 | happy | 5
251 | people | 4
251 | handsome | 4
251 | glad | 3
251 | forward | 3
Can anyone help me out in achieving desired output. I will be thankful.
resources I have gone through Rolling sum / count / average over date interval
postgresql-9.4
I am trying to perform both count(distinct(keyword)) and count(keyword) from same tables. Here are the two queries.
Query-1
SELECT t1.count(distinct(keyword)) from keyword t1, sent t2,tweets t3,users t4
WHERE t4.user_id='18839785'
AND t1.id=t2.id AND t2.id=t3.id AND t3.user_id=t4.user_id AND t2.sent='Pos';
count
-------
251
(1 row)
Query-2
SELECT t1.keyword,t1.count(keyword) FROM keyword t1, sent t2,tweets t3,users t4
WHERE t4.name='narendramodi' AND t3.id=t4.user_id AND t2.id=t3.id
AND t2.id=t1.id AND t2.sent='Pos' GROUP BY t1.nlp_keyword_name order by count(keyword) desc ;
nlp_keyword_name | count
------------------+-------
happy | 5
people | 4
handsome | 4
glad | 3
forward | 3
wishes | 3
proud | 2
(7 rows)
Query-3
SELECT distinct_drivers,positive_drivers,countt
from (
SELECT t1.count(distinct(keyword)) as distinct_drivers ,
t1.nlp_keyword_name as P_drivers,
t1.count(nlp_keyword_name) as countt
FROM keyword t1 LEFT JOIN sent t2 on t1.id=t2.id
LEFT JOIN tweets t3 on t3.id=t2.id
LEFT JOIN users t4 on t4.user_id=t3.user_id
WHERE (t4.user_id)=('18839785') AND t2.sent='Pos' GROUP BY t1.nlp_keyword_name order by countt desc) T;
But I am getting my output as
distinct_drivers | positive_drivers | countt
------------------+------------------+--------
1 | happy | 5
1 | people | 4
1 | handsome | 4
1 | glad | 3
1 | forward | 3
Here after executing query-3 my output has to be below way which I am not getting like that.
**desired_output:**
distinct_drivers | positive_drivers | countt
------------------+------------------+--------
251 | happy | 5
251 | people | 4
251 | handsome | 4
251 | glad | 3
251 | forward | 3
Can anyone help me out in achieving desired output. I will be thankful.
resources I have gone through Rolling sum / count / average over date interval
postgresql-9.4
postgresql-9.4
asked 9 mins ago
bunny sunnybunny sunny
62
62
add a comment |
add a comment |
0
active
oldest
votes
Your Answer
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "182"
};
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: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
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%2fdba.stackexchange.com%2fquestions%2f232984%2fhow-to-perform-count-countdistinct-in-the-same-output-using-postgresql%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Database Administrators Stack Exchange!
- 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%2fdba.stackexchange.com%2fquestions%2f232984%2fhow-to-perform-count-countdistinct-in-the-same-output-using-postgresql%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