SQL Use ID to count dates but only can count each once
TABLE 1
ID DATE
1 . 1/1/2018
2 . 1/2/2018
2 . 1/2/2018
3 . 1/3/2018
4 . 1/2/2018
So I need to get a count of each date but there are cases where the same ID has the same date. I need to only count that one once.
For example my expected output is
1/1/2018 . 1
1/2/2018 . 2
1/3/2018 . 1
But the output i'm getting is
1/1/2018 . 1
1/2/2018 . 3
1/3/2018 . 1
sql

add a comment |
TABLE 1
ID DATE
1 . 1/1/2018
2 . 1/2/2018
2 . 1/2/2018
3 . 1/3/2018
4 . 1/2/2018
So I need to get a count of each date but there are cases where the same ID has the same date. I need to only count that one once.
For example my expected output is
1/1/2018 . 1
1/2/2018 . 2
1/3/2018 . 1
But the output i'm getting is
1/1/2018 . 1
1/2/2018 . 3
1/3/2018 . 1
sql

You should post your query in question as well in order to rectify issue.
– Yogesh Sharma
Nov 19 '18 at 17:38
So the row4 . 1/2/2018
does not count in your expected output?
– forpas
Nov 19 '18 at 18:02
It does count in my output but I don't want it to count one of the dates with the id of 2.
– Brandon Brown
Nov 19 '18 at 18:05
So there is an extra row1/2/2018 . 1
in your expected output?
– forpas
Nov 19 '18 at 18:07
yes an extra one is being counted. Yogesh solved it. Thank you.
– Brandon Brown
Nov 19 '18 at 18:10
add a comment |
TABLE 1
ID DATE
1 . 1/1/2018
2 . 1/2/2018
2 . 1/2/2018
3 . 1/3/2018
4 . 1/2/2018
So I need to get a count of each date but there are cases where the same ID has the same date. I need to only count that one once.
For example my expected output is
1/1/2018 . 1
1/2/2018 . 2
1/3/2018 . 1
But the output i'm getting is
1/1/2018 . 1
1/2/2018 . 3
1/3/2018 . 1
sql

TABLE 1
ID DATE
1 . 1/1/2018
2 . 1/2/2018
2 . 1/2/2018
3 . 1/3/2018
4 . 1/2/2018
So I need to get a count of each date but there are cases where the same ID has the same date. I need to only count that one once.
For example my expected output is
1/1/2018 . 1
1/2/2018 . 2
1/3/2018 . 1
But the output i'm getting is
1/1/2018 . 1
1/2/2018 . 3
1/3/2018 . 1
sql

sql

edited Nov 19 '18 at 17:39


Yogesh Sharma
28.3k51335
28.3k51335
asked Nov 19 '18 at 17:33


Brandon BrownBrandon Brown
144
144
You should post your query in question as well in order to rectify issue.
– Yogesh Sharma
Nov 19 '18 at 17:38
So the row4 . 1/2/2018
does not count in your expected output?
– forpas
Nov 19 '18 at 18:02
It does count in my output but I don't want it to count one of the dates with the id of 2.
– Brandon Brown
Nov 19 '18 at 18:05
So there is an extra row1/2/2018 . 1
in your expected output?
– forpas
Nov 19 '18 at 18:07
yes an extra one is being counted. Yogesh solved it. Thank you.
– Brandon Brown
Nov 19 '18 at 18:10
add a comment |
You should post your query in question as well in order to rectify issue.
– Yogesh Sharma
Nov 19 '18 at 17:38
So the row4 . 1/2/2018
does not count in your expected output?
– forpas
Nov 19 '18 at 18:02
It does count in my output but I don't want it to count one of the dates with the id of 2.
– Brandon Brown
Nov 19 '18 at 18:05
So there is an extra row1/2/2018 . 1
in your expected output?
– forpas
Nov 19 '18 at 18:07
yes an extra one is being counted. Yogesh solved it. Thank you.
– Brandon Brown
Nov 19 '18 at 18:10
You should post your query in question as well in order to rectify issue.
– Yogesh Sharma
Nov 19 '18 at 17:38
You should post your query in question as well in order to rectify issue.
– Yogesh Sharma
Nov 19 '18 at 17:38
So the row
4 . 1/2/2018
does not count in your expected output?– forpas
Nov 19 '18 at 18:02
So the row
4 . 1/2/2018
does not count in your expected output?– forpas
Nov 19 '18 at 18:02
It does count in my output but I don't want it to count one of the dates with the id of 2.
– Brandon Brown
Nov 19 '18 at 18:05
It does count in my output but I don't want it to count one of the dates with the id of 2.
– Brandon Brown
Nov 19 '18 at 18:05
So there is an extra row
1/2/2018 . 1
in your expected output?– forpas
Nov 19 '18 at 18:07
So there is an extra row
1/2/2018 . 1
in your expected output?– forpas
Nov 19 '18 at 18:07
yes an extra one is being counted. Yogesh solved it. Thank you.
– Brandon Brown
Nov 19 '18 at 18:10
yes an extra one is being counted. Yogesh solved it. Thank you.
– Brandon Brown
Nov 19 '18 at 18:10
add a comment |
3 Answers
3
active
oldest
votes
You need DISTINCT
inside COUNT()
to considered same id as once :
SELECT DATE, COUNT(DISTINCT ID)
FROM table1 t
GROUP BY DATE;
add a comment |
You need to GROUP
by ID and DATE:
SELECT ID, DATE, COUNT(DATE) AS COUNTER
FROM table1
GROUP BY ID, DATE
add a comment |
You could use a subquery where you concatenate the key and the date using distinct.
SELECT DATE, COUNT(DATE) FROM table1
where DATE + ID =
(select DATE + ID from table1)
group by DATE
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%2f53379889%2fsql-use-id-to-count-dates-but-only-can-count-each-once%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
You need DISTINCT
inside COUNT()
to considered same id as once :
SELECT DATE, COUNT(DISTINCT ID)
FROM table1 t
GROUP BY DATE;
add a comment |
You need DISTINCT
inside COUNT()
to considered same id as once :
SELECT DATE, COUNT(DISTINCT ID)
FROM table1 t
GROUP BY DATE;
add a comment |
You need DISTINCT
inside COUNT()
to considered same id as once :
SELECT DATE, COUNT(DISTINCT ID)
FROM table1 t
GROUP BY DATE;
You need DISTINCT
inside COUNT()
to considered same id as once :
SELECT DATE, COUNT(DISTINCT ID)
FROM table1 t
GROUP BY DATE;
answered Nov 19 '18 at 17:35


Yogesh SharmaYogesh Sharma
28.3k51335
28.3k51335
add a comment |
add a comment |
You need to GROUP
by ID and DATE:
SELECT ID, DATE, COUNT(DATE) AS COUNTER
FROM table1
GROUP BY ID, DATE
add a comment |
You need to GROUP
by ID and DATE:
SELECT ID, DATE, COUNT(DATE) AS COUNTER
FROM table1
GROUP BY ID, DATE
add a comment |
You need to GROUP
by ID and DATE:
SELECT ID, DATE, COUNT(DATE) AS COUNTER
FROM table1
GROUP BY ID, DATE
You need to GROUP
by ID and DATE:
SELECT ID, DATE, COUNT(DATE) AS COUNTER
FROM table1
GROUP BY ID, DATE
answered Nov 19 '18 at 18:28
forpasforpas
9,3091421
9,3091421
add a comment |
add a comment |
You could use a subquery where you concatenate the key and the date using distinct.
SELECT DATE, COUNT(DATE) FROM table1
where DATE + ID =
(select DATE + ID from table1)
group by DATE
add a comment |
You could use a subquery where you concatenate the key and the date using distinct.
SELECT DATE, COUNT(DATE) FROM table1
where DATE + ID =
(select DATE + ID from table1)
group by DATE
add a comment |
You could use a subquery where you concatenate the key and the date using distinct.
SELECT DATE, COUNT(DATE) FROM table1
where DATE + ID =
(select DATE + ID from table1)
group by DATE
You could use a subquery where you concatenate the key and the date using distinct.
SELECT DATE, COUNT(DATE) FROM table1
where DATE + ID =
(select DATE + ID from table1)
group by DATE
answered Nov 19 '18 at 18:56
KarlomanioKarlomanio
27528
27528
add a comment |
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.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- 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%2f53379889%2fsql-use-id-to-count-dates-but-only-can-count-each-once%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
You should post your query in question as well in order to rectify issue.
– Yogesh Sharma
Nov 19 '18 at 17:38
So the row
4 . 1/2/2018
does not count in your expected output?– forpas
Nov 19 '18 at 18:02
It does count in my output but I don't want it to count one of the dates with the id of 2.
– Brandon Brown
Nov 19 '18 at 18:05
So there is an extra row
1/2/2018 . 1
in your expected output?– forpas
Nov 19 '18 at 18:07
yes an extra one is being counted. Yogesh solved it. Thank you.
– Brandon Brown
Nov 19 '18 at 18:10