sum only different id's values with any aggreagte function in postgresql

Multi tool use
I need your help. Let's assume that we have a table like below
+---------------+-------+--------+----------+
| barcode | shop | id | entrance |
+---------------+-------+--------+----------+
| 2014708941747 | shop1 | 3587 | 2 |
| 2014708941747 | shop2 | 3587 | 2 |
| 2014708941747 | shop3 | 3587 | 2 |
| 2014708941747 | shop4 | 3587 | 2 |
| 2014708941747 | shop5 | 3587 | 2 |
| 2014708941747 | shop6 | 3587 | 2 |
| 2014708941747 | shop7 | 3587 | 2 |
| 2014708941747 | shop1 | 44791 | 2 |
| 2014708941747 | shop8 | 65846 | 0 |
| 2014708941747 | shop9 | 83246 | 0 |
| 2014708941747 | shop3 | 92705 | 22 |
| 2014708941747 | shop4 | 98014 | 8 |
| 2014708941747 | shop2 | 103612 | 12 |
| 2014708941747 | shop5 | 109961 | 19 |
| 2014708941747 | shop6 | 115025 | 6 |
| 2014708941747 | shop7 | 126898 | 144 |
+---------------+-------+--------+----------+
Now I want to know how many quantities exist for the given barcode but id
must not be repeated. With the above example, our result must be like below
+---------------+----------+
| barcode | entrance |
+---------------+----------+
| 2014708941747 | 225 |
+---------------+----------+
If I do this with a traditional group by entrance=227
which is not correct. Is there any aggregate function to solve this kind of problem?
group-by aggregate postgresql-9.6 window-functions sum
New contributor
sakit is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
add a comment |
I need your help. Let's assume that we have a table like below
+---------------+-------+--------+----------+
| barcode | shop | id | entrance |
+---------------+-------+--------+----------+
| 2014708941747 | shop1 | 3587 | 2 |
| 2014708941747 | shop2 | 3587 | 2 |
| 2014708941747 | shop3 | 3587 | 2 |
| 2014708941747 | shop4 | 3587 | 2 |
| 2014708941747 | shop5 | 3587 | 2 |
| 2014708941747 | shop6 | 3587 | 2 |
| 2014708941747 | shop7 | 3587 | 2 |
| 2014708941747 | shop1 | 44791 | 2 |
| 2014708941747 | shop8 | 65846 | 0 |
| 2014708941747 | shop9 | 83246 | 0 |
| 2014708941747 | shop3 | 92705 | 22 |
| 2014708941747 | shop4 | 98014 | 8 |
| 2014708941747 | shop2 | 103612 | 12 |
| 2014708941747 | shop5 | 109961 | 19 |
| 2014708941747 | shop6 | 115025 | 6 |
| 2014708941747 | shop7 | 126898 | 144 |
+---------------+-------+--------+----------+
Now I want to know how many quantities exist for the given barcode but id
must not be repeated. With the above example, our result must be like below
+---------------+----------+
| barcode | entrance |
+---------------+----------+
| 2014708941747 | 225 |
+---------------+----------+
If I do this with a traditional group by entrance=227
which is not correct. Is there any aggregate function to solve this kind of problem?
group-by aggregate postgresql-9.6 window-functions sum
New contributor
sakit is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
What if there are different values for an ID?
– sticky bit
20 mins ago
@stickybit there is no such case
– sakit
16 mins ago
add a comment |
I need your help. Let's assume that we have a table like below
+---------------+-------+--------+----------+
| barcode | shop | id | entrance |
+---------------+-------+--------+----------+
| 2014708941747 | shop1 | 3587 | 2 |
| 2014708941747 | shop2 | 3587 | 2 |
| 2014708941747 | shop3 | 3587 | 2 |
| 2014708941747 | shop4 | 3587 | 2 |
| 2014708941747 | shop5 | 3587 | 2 |
| 2014708941747 | shop6 | 3587 | 2 |
| 2014708941747 | shop7 | 3587 | 2 |
| 2014708941747 | shop1 | 44791 | 2 |
| 2014708941747 | shop8 | 65846 | 0 |
| 2014708941747 | shop9 | 83246 | 0 |
| 2014708941747 | shop3 | 92705 | 22 |
| 2014708941747 | shop4 | 98014 | 8 |
| 2014708941747 | shop2 | 103612 | 12 |
| 2014708941747 | shop5 | 109961 | 19 |
| 2014708941747 | shop6 | 115025 | 6 |
| 2014708941747 | shop7 | 126898 | 144 |
+---------------+-------+--------+----------+
Now I want to know how many quantities exist for the given barcode but id
must not be repeated. With the above example, our result must be like below
+---------------+----------+
| barcode | entrance |
+---------------+----------+
| 2014708941747 | 225 |
+---------------+----------+
If I do this with a traditional group by entrance=227
which is not correct. Is there any aggregate function to solve this kind of problem?
group-by aggregate postgresql-9.6 window-functions sum
New contributor
sakit is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
I need your help. Let's assume that we have a table like below
+---------------+-------+--------+----------+
| barcode | shop | id | entrance |
+---------------+-------+--------+----------+
| 2014708941747 | shop1 | 3587 | 2 |
| 2014708941747 | shop2 | 3587 | 2 |
| 2014708941747 | shop3 | 3587 | 2 |
| 2014708941747 | shop4 | 3587 | 2 |
| 2014708941747 | shop5 | 3587 | 2 |
| 2014708941747 | shop6 | 3587 | 2 |
| 2014708941747 | shop7 | 3587 | 2 |
| 2014708941747 | shop1 | 44791 | 2 |
| 2014708941747 | shop8 | 65846 | 0 |
| 2014708941747 | shop9 | 83246 | 0 |
| 2014708941747 | shop3 | 92705 | 22 |
| 2014708941747 | shop4 | 98014 | 8 |
| 2014708941747 | shop2 | 103612 | 12 |
| 2014708941747 | shop5 | 109961 | 19 |
| 2014708941747 | shop6 | 115025 | 6 |
| 2014708941747 | shop7 | 126898 | 144 |
+---------------+-------+--------+----------+
Now I want to know how many quantities exist for the given barcode but id
must not be repeated. With the above example, our result must be like below
+---------------+----------+
| barcode | entrance |
+---------------+----------+
| 2014708941747 | 225 |
+---------------+----------+
If I do this with a traditional group by entrance=227
which is not correct. Is there any aggregate function to solve this kind of problem?
group-by aggregate postgresql-9.6 window-functions sum
group-by aggregate postgresql-9.6 window-functions sum
New contributor
sakit is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
sakit is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
edited 11 mins ago
sakit
New contributor
sakit is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
asked 30 mins ago


sakitsakit
11
11
New contributor
sakit is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
sakit is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
sakit is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
What if there are different values for an ID?
– sticky bit
20 mins ago
@stickybit there is no such case
– sakit
16 mins ago
add a comment |
What if there are different values for an ID?
– sticky bit
20 mins ago
@stickybit there is no such case
– sakit
16 mins ago
What if there are different values for an ID?
– sticky bit
20 mins ago
What if there are different values for an ID?
– sticky bit
20 mins ago
@stickybit there is no such case
– sakit
16 mins ago
@stickybit there is no such case
– sakit
16 mins ago
add a comment |
1 Answer
1
active
oldest
votes
If entrance is always the same per barcode and ID you could first group by barcode and id taking the max entrance and then get the sum grouping by barcode.
SELECT x.barcode,
sum(x.entrance) entrance
FROM (SELECT t.barcode,
t.id,
max(t.entrance) entrance
FROM elbat t
GROUP BY t.barcode,
t.id) x
GROUP BY x.barcode;
thanks for your response. But I search that is there any predefined functions in Postgres like window functions.
– sakit
6 mins ago
add a comment |
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
});
}
});
sakit is a new contributor. Be nice, and check out our Code of Conduct.
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%2f227000%2fsum-only-different-ids-values-with-any-aggreagte-function-in-postgresql%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
If entrance is always the same per barcode and ID you could first group by barcode and id taking the max entrance and then get the sum grouping by barcode.
SELECT x.barcode,
sum(x.entrance) entrance
FROM (SELECT t.barcode,
t.id,
max(t.entrance) entrance
FROM elbat t
GROUP BY t.barcode,
t.id) x
GROUP BY x.barcode;
thanks for your response. But I search that is there any predefined functions in Postgres like window functions.
– sakit
6 mins ago
add a comment |
If entrance is always the same per barcode and ID you could first group by barcode and id taking the max entrance and then get the sum grouping by barcode.
SELECT x.barcode,
sum(x.entrance) entrance
FROM (SELECT t.barcode,
t.id,
max(t.entrance) entrance
FROM elbat t
GROUP BY t.barcode,
t.id) x
GROUP BY x.barcode;
thanks for your response. But I search that is there any predefined functions in Postgres like window functions.
– sakit
6 mins ago
add a comment |
If entrance is always the same per barcode and ID you could first group by barcode and id taking the max entrance and then get the sum grouping by barcode.
SELECT x.barcode,
sum(x.entrance) entrance
FROM (SELECT t.barcode,
t.id,
max(t.entrance) entrance
FROM elbat t
GROUP BY t.barcode,
t.id) x
GROUP BY x.barcode;
If entrance is always the same per barcode and ID you could first group by barcode and id taking the max entrance and then get the sum grouping by barcode.
SELECT x.barcode,
sum(x.entrance) entrance
FROM (SELECT t.barcode,
t.id,
max(t.entrance) entrance
FROM elbat t
GROUP BY t.barcode,
t.id) x
GROUP BY x.barcode;
answered 11 mins ago


sticky bitsticky bit
1,713314
1,713314
thanks for your response. But I search that is there any predefined functions in Postgres like window functions.
– sakit
6 mins ago
add a comment |
thanks for your response. But I search that is there any predefined functions in Postgres like window functions.
– sakit
6 mins ago
thanks for your response. But I search that is there any predefined functions in Postgres like window functions.
– sakit
6 mins ago
thanks for your response. But I search that is there any predefined functions in Postgres like window functions.
– sakit
6 mins ago
add a comment |
sakit is a new contributor. Be nice, and check out our Code of Conduct.
sakit is a new contributor. Be nice, and check out our Code of Conduct.
sakit is a new contributor. Be nice, and check out our Code of Conduct.
sakit is a new contributor. Be nice, and check out our Code of Conduct.
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%2f227000%2fsum-only-different-ids-values-with-any-aggreagte-function-in-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
yu QV0GdKB 9TYz2t0p 8cZD7 51hg,q,J 4xtC3Z2Ss UxtQxJaZ4,Z8tEKqYNnpICJ5Bue x1IVGP3
What if there are different values for an ID?
– sticky bit
20 mins ago
@stickybit there is no such case
– sakit
16 mins ago