How to get sum & latest activity in same result?
up vote
2
down vote
favorite
Table_1
Name | Activity | LogTime
A | 0 | 2018-12-17 10:16:04.877
A | 1 | 2018-12-15 10:16:04.877
A | 0 | 2018-12-16 10:16:04.877
A | 0 | 2018-12-10 10:16:04.877
A | 0 | 2018-12-10 10:10:04.877
B | 1 | 2018-12-16 10:16:04.877
B | 0 | 2018-12-17 10:16:04.877
C | 1 | 2018-12-14 10:16:04.877
C | 1 | 2018-12-12 10:16:04.877
C | 1 | 2018-12-18 10:16:04.877
Desired Result
Name | TOTALActivity_0 | TOTALActivity_1 | LatestActivity_0_Logtime | LatestActivity_1_Logtime
A | 4 | 1 | 2018-12-17 10:16:04.877 | 2018-12-15 10:16:04.877
B | 1 | 1 | 2018-12-17 10:16:04.877 | 2018-12-16 10:16:04.877
C | 0 | 3 | NULL | 2018-12-18 10:16:04.877
I am able to get result as below
Name | TOTALActivity_0 | TOTALActivity_1
A | 4 | 1
B | 1 | 1
C | 0 | 3
Using below query
SELECT
NAME,
SUM(CASE WHEN Activity = 0 THEN 1 ELSE 0 END) TOTALActivity_0,
SUM(CASE WHEN Activity = 1 THEN 1 ELSE 0 END) TOTALActivity_1
FROM Table_1
GROUP BY NAME
If I tried as below
SELECT
NAME,
SUM(CASE WHEN Activity = 0 THEN 1 ELSE 0 END) TOTALActivity_0,
SUM(CASE WHEN Activity = 1 THEN 1 ELSE 0 END) TOTALActivity_1,
CASE WHEN Activity = 0 THEN LogTime ELSE NULL END AS LatestActivity_0_Logtime,
CASE WHEN Activity = 1 THEN LogTime ELSE NULL END AS LatestActivity_1_Logtime
FROM Table_1
GROUP BY NAME,Activity,LogTime
Then it is giving Single-single & redundant records.
Then I tried with again with below query
SELECT
NAME,
SUM(CASE WHEN Activity = 0 THEN 1 ELSE 0 END) TOTALActivity_0,
SUM(CASE WHEN Activity = 1 THEN 1 ELSE 0 END) TOTALActivity_1,
NULL AS LatestActivity_0_Logtime,
NULL AS LatestActivity_1_Logtime
FROM Table_1
GROUP BY NAME
UNION
SELECT NULL,NULL,NULL,CASE WHEN Activity = 0 THEN LogTime ELSE NULL END AS LatestActivity_0_Logtime,
CASE WHEN Activity = 1 THEN LogTime ELSE NULL END AS LatestActivity_1_Logtime
FROM Table_1
Then result is again undesirable as below
Name | TOTALActivity_0 | TOTALActivity_1 | LatestActivity_0_Logtime | LatestActivity_1_Logtime
A | 4 | 1 | NULL | NULL
B | 1 | 1 | NULL | NULL
B | 1 | 3 | NULL | NULL
NULL | NULL | NULL | 2018-12-17 10:16:04.877 | 2018-12-15 10:16:04.877
NULL | NULL | NULL | 2018-12-16 10:16:04.877 | 2018-12-16 10:16:04.877
NULL | NULL | NULL | 2018-12-10 10:16:04.877 | 2018-12-18 10:16:04.877
NULL | NULL | NULL | 2018-12-10 10:10:04.877 | 2018-12-15 10:16:04.877
NULL | NULL | NULL | 2018-12-17 10:16:04.877 | 2018-12-16 10:16:04.877
.
.
.
.
.
.
Please Help to get distinct & merged result as desired
sql-server sql-server-2017
add a comment |
up vote
2
down vote
favorite
Table_1
Name | Activity | LogTime
A | 0 | 2018-12-17 10:16:04.877
A | 1 | 2018-12-15 10:16:04.877
A | 0 | 2018-12-16 10:16:04.877
A | 0 | 2018-12-10 10:16:04.877
A | 0 | 2018-12-10 10:10:04.877
B | 1 | 2018-12-16 10:16:04.877
B | 0 | 2018-12-17 10:16:04.877
C | 1 | 2018-12-14 10:16:04.877
C | 1 | 2018-12-12 10:16:04.877
C | 1 | 2018-12-18 10:16:04.877
Desired Result
Name | TOTALActivity_0 | TOTALActivity_1 | LatestActivity_0_Logtime | LatestActivity_1_Logtime
A | 4 | 1 | 2018-12-17 10:16:04.877 | 2018-12-15 10:16:04.877
B | 1 | 1 | 2018-12-17 10:16:04.877 | 2018-12-16 10:16:04.877
C | 0 | 3 | NULL | 2018-12-18 10:16:04.877
I am able to get result as below
Name | TOTALActivity_0 | TOTALActivity_1
A | 4 | 1
B | 1 | 1
C | 0 | 3
Using below query
SELECT
NAME,
SUM(CASE WHEN Activity = 0 THEN 1 ELSE 0 END) TOTALActivity_0,
SUM(CASE WHEN Activity = 1 THEN 1 ELSE 0 END) TOTALActivity_1
FROM Table_1
GROUP BY NAME
If I tried as below
SELECT
NAME,
SUM(CASE WHEN Activity = 0 THEN 1 ELSE 0 END) TOTALActivity_0,
SUM(CASE WHEN Activity = 1 THEN 1 ELSE 0 END) TOTALActivity_1,
CASE WHEN Activity = 0 THEN LogTime ELSE NULL END AS LatestActivity_0_Logtime,
CASE WHEN Activity = 1 THEN LogTime ELSE NULL END AS LatestActivity_1_Logtime
FROM Table_1
GROUP BY NAME,Activity,LogTime
Then it is giving Single-single & redundant records.
Then I tried with again with below query
SELECT
NAME,
SUM(CASE WHEN Activity = 0 THEN 1 ELSE 0 END) TOTALActivity_0,
SUM(CASE WHEN Activity = 1 THEN 1 ELSE 0 END) TOTALActivity_1,
NULL AS LatestActivity_0_Logtime,
NULL AS LatestActivity_1_Logtime
FROM Table_1
GROUP BY NAME
UNION
SELECT NULL,NULL,NULL,CASE WHEN Activity = 0 THEN LogTime ELSE NULL END AS LatestActivity_0_Logtime,
CASE WHEN Activity = 1 THEN LogTime ELSE NULL END AS LatestActivity_1_Logtime
FROM Table_1
Then result is again undesirable as below
Name | TOTALActivity_0 | TOTALActivity_1 | LatestActivity_0_Logtime | LatestActivity_1_Logtime
A | 4 | 1 | NULL | NULL
B | 1 | 1 | NULL | NULL
B | 1 | 3 | NULL | NULL
NULL | NULL | NULL | 2018-12-17 10:16:04.877 | 2018-12-15 10:16:04.877
NULL | NULL | NULL | 2018-12-16 10:16:04.877 | 2018-12-16 10:16:04.877
NULL | NULL | NULL | 2018-12-10 10:16:04.877 | 2018-12-18 10:16:04.877
NULL | NULL | NULL | 2018-12-10 10:10:04.877 | 2018-12-15 10:16:04.877
NULL | NULL | NULL | 2018-12-17 10:16:04.877 | 2018-12-16 10:16:04.877
.
.
.
.
.
.
Please Help to get distinct & merged result as desired
sql-server sql-server-2017
add a comment |
up vote
2
down vote
favorite
up vote
2
down vote
favorite
Table_1
Name | Activity | LogTime
A | 0 | 2018-12-17 10:16:04.877
A | 1 | 2018-12-15 10:16:04.877
A | 0 | 2018-12-16 10:16:04.877
A | 0 | 2018-12-10 10:16:04.877
A | 0 | 2018-12-10 10:10:04.877
B | 1 | 2018-12-16 10:16:04.877
B | 0 | 2018-12-17 10:16:04.877
C | 1 | 2018-12-14 10:16:04.877
C | 1 | 2018-12-12 10:16:04.877
C | 1 | 2018-12-18 10:16:04.877
Desired Result
Name | TOTALActivity_0 | TOTALActivity_1 | LatestActivity_0_Logtime | LatestActivity_1_Logtime
A | 4 | 1 | 2018-12-17 10:16:04.877 | 2018-12-15 10:16:04.877
B | 1 | 1 | 2018-12-17 10:16:04.877 | 2018-12-16 10:16:04.877
C | 0 | 3 | NULL | 2018-12-18 10:16:04.877
I am able to get result as below
Name | TOTALActivity_0 | TOTALActivity_1
A | 4 | 1
B | 1 | 1
C | 0 | 3
Using below query
SELECT
NAME,
SUM(CASE WHEN Activity = 0 THEN 1 ELSE 0 END) TOTALActivity_0,
SUM(CASE WHEN Activity = 1 THEN 1 ELSE 0 END) TOTALActivity_1
FROM Table_1
GROUP BY NAME
If I tried as below
SELECT
NAME,
SUM(CASE WHEN Activity = 0 THEN 1 ELSE 0 END) TOTALActivity_0,
SUM(CASE WHEN Activity = 1 THEN 1 ELSE 0 END) TOTALActivity_1,
CASE WHEN Activity = 0 THEN LogTime ELSE NULL END AS LatestActivity_0_Logtime,
CASE WHEN Activity = 1 THEN LogTime ELSE NULL END AS LatestActivity_1_Logtime
FROM Table_1
GROUP BY NAME,Activity,LogTime
Then it is giving Single-single & redundant records.
Then I tried with again with below query
SELECT
NAME,
SUM(CASE WHEN Activity = 0 THEN 1 ELSE 0 END) TOTALActivity_0,
SUM(CASE WHEN Activity = 1 THEN 1 ELSE 0 END) TOTALActivity_1,
NULL AS LatestActivity_0_Logtime,
NULL AS LatestActivity_1_Logtime
FROM Table_1
GROUP BY NAME
UNION
SELECT NULL,NULL,NULL,CASE WHEN Activity = 0 THEN LogTime ELSE NULL END AS LatestActivity_0_Logtime,
CASE WHEN Activity = 1 THEN LogTime ELSE NULL END AS LatestActivity_1_Logtime
FROM Table_1
Then result is again undesirable as below
Name | TOTALActivity_0 | TOTALActivity_1 | LatestActivity_0_Logtime | LatestActivity_1_Logtime
A | 4 | 1 | NULL | NULL
B | 1 | 1 | NULL | NULL
B | 1 | 3 | NULL | NULL
NULL | NULL | NULL | 2018-12-17 10:16:04.877 | 2018-12-15 10:16:04.877
NULL | NULL | NULL | 2018-12-16 10:16:04.877 | 2018-12-16 10:16:04.877
NULL | NULL | NULL | 2018-12-10 10:16:04.877 | 2018-12-18 10:16:04.877
NULL | NULL | NULL | 2018-12-10 10:10:04.877 | 2018-12-15 10:16:04.877
NULL | NULL | NULL | 2018-12-17 10:16:04.877 | 2018-12-16 10:16:04.877
.
.
.
.
.
.
Please Help to get distinct & merged result as desired
sql-server sql-server-2017
Table_1
Name | Activity | LogTime
A | 0 | 2018-12-17 10:16:04.877
A | 1 | 2018-12-15 10:16:04.877
A | 0 | 2018-12-16 10:16:04.877
A | 0 | 2018-12-10 10:16:04.877
A | 0 | 2018-12-10 10:10:04.877
B | 1 | 2018-12-16 10:16:04.877
B | 0 | 2018-12-17 10:16:04.877
C | 1 | 2018-12-14 10:16:04.877
C | 1 | 2018-12-12 10:16:04.877
C | 1 | 2018-12-18 10:16:04.877
Desired Result
Name | TOTALActivity_0 | TOTALActivity_1 | LatestActivity_0_Logtime | LatestActivity_1_Logtime
A | 4 | 1 | 2018-12-17 10:16:04.877 | 2018-12-15 10:16:04.877
B | 1 | 1 | 2018-12-17 10:16:04.877 | 2018-12-16 10:16:04.877
C | 0 | 3 | NULL | 2018-12-18 10:16:04.877
I am able to get result as below
Name | TOTALActivity_0 | TOTALActivity_1
A | 4 | 1
B | 1 | 1
C | 0 | 3
Using below query
SELECT
NAME,
SUM(CASE WHEN Activity = 0 THEN 1 ELSE 0 END) TOTALActivity_0,
SUM(CASE WHEN Activity = 1 THEN 1 ELSE 0 END) TOTALActivity_1
FROM Table_1
GROUP BY NAME
If I tried as below
SELECT
NAME,
SUM(CASE WHEN Activity = 0 THEN 1 ELSE 0 END) TOTALActivity_0,
SUM(CASE WHEN Activity = 1 THEN 1 ELSE 0 END) TOTALActivity_1,
CASE WHEN Activity = 0 THEN LogTime ELSE NULL END AS LatestActivity_0_Logtime,
CASE WHEN Activity = 1 THEN LogTime ELSE NULL END AS LatestActivity_1_Logtime
FROM Table_1
GROUP BY NAME,Activity,LogTime
Then it is giving Single-single & redundant records.
Then I tried with again with below query
SELECT
NAME,
SUM(CASE WHEN Activity = 0 THEN 1 ELSE 0 END) TOTALActivity_0,
SUM(CASE WHEN Activity = 1 THEN 1 ELSE 0 END) TOTALActivity_1,
NULL AS LatestActivity_0_Logtime,
NULL AS LatestActivity_1_Logtime
FROM Table_1
GROUP BY NAME
UNION
SELECT NULL,NULL,NULL,CASE WHEN Activity = 0 THEN LogTime ELSE NULL END AS LatestActivity_0_Logtime,
CASE WHEN Activity = 1 THEN LogTime ELSE NULL END AS LatestActivity_1_Logtime
FROM Table_1
Then result is again undesirable as below
Name | TOTALActivity_0 | TOTALActivity_1 | LatestActivity_0_Logtime | LatestActivity_1_Logtime
A | 4 | 1 | NULL | NULL
B | 1 | 1 | NULL | NULL
B | 1 | 3 | NULL | NULL
NULL | NULL | NULL | 2018-12-17 10:16:04.877 | 2018-12-15 10:16:04.877
NULL | NULL | NULL | 2018-12-16 10:16:04.877 | 2018-12-16 10:16:04.877
NULL | NULL | NULL | 2018-12-10 10:16:04.877 | 2018-12-18 10:16:04.877
NULL | NULL | NULL | 2018-12-10 10:10:04.877 | 2018-12-15 10:16:04.877
NULL | NULL | NULL | 2018-12-17 10:16:04.877 | 2018-12-16 10:16:04.877
.
.
.
.
.
.
Please Help to get distinct & merged result as desired
sql-server sql-server-2017
sql-server sql-server-2017
edited 45 mins ago
asked 52 mins ago
Hina Khuman
576
576
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
up vote
2
down vote
This should work:
SELECT
Name,
COUNT(CASE WHEN Activity = 0 THEN 1 END) AS TOTALActivity_0,
COUNT(CASE WHEN Activity = 1 THEN 1 END) AS TOTALActivity_1,
MAX(CASE WHEN Activity = 0 THEN LogTime END) AS LatestActivity_0_Logtime,
MAX(CASE WHEN Activity = 1 THEN LogTime END) AS LatestActivity_1_Logtime
FROM
Table_1
GROUP BY
Name ;
Test in: dbfiddle.uk
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',
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%2f225145%2fhow-to-get-sum-latest-activity-in-same-result%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
up vote
2
down vote
This should work:
SELECT
Name,
COUNT(CASE WHEN Activity = 0 THEN 1 END) AS TOTALActivity_0,
COUNT(CASE WHEN Activity = 1 THEN 1 END) AS TOTALActivity_1,
MAX(CASE WHEN Activity = 0 THEN LogTime END) AS LatestActivity_0_Logtime,
MAX(CASE WHEN Activity = 1 THEN LogTime END) AS LatestActivity_1_Logtime
FROM
Table_1
GROUP BY
Name ;
Test in: dbfiddle.uk
add a comment |
up vote
2
down vote
This should work:
SELECT
Name,
COUNT(CASE WHEN Activity = 0 THEN 1 END) AS TOTALActivity_0,
COUNT(CASE WHEN Activity = 1 THEN 1 END) AS TOTALActivity_1,
MAX(CASE WHEN Activity = 0 THEN LogTime END) AS LatestActivity_0_Logtime,
MAX(CASE WHEN Activity = 1 THEN LogTime END) AS LatestActivity_1_Logtime
FROM
Table_1
GROUP BY
Name ;
Test in: dbfiddle.uk
add a comment |
up vote
2
down vote
up vote
2
down vote
This should work:
SELECT
Name,
COUNT(CASE WHEN Activity = 0 THEN 1 END) AS TOTALActivity_0,
COUNT(CASE WHEN Activity = 1 THEN 1 END) AS TOTALActivity_1,
MAX(CASE WHEN Activity = 0 THEN LogTime END) AS LatestActivity_0_Logtime,
MAX(CASE WHEN Activity = 1 THEN LogTime END) AS LatestActivity_1_Logtime
FROM
Table_1
GROUP BY
Name ;
Test in: dbfiddle.uk
This should work:
SELECT
Name,
COUNT(CASE WHEN Activity = 0 THEN 1 END) AS TOTALActivity_0,
COUNT(CASE WHEN Activity = 1 THEN 1 END) AS TOTALActivity_1,
MAX(CASE WHEN Activity = 0 THEN LogTime END) AS LatestActivity_0_Logtime,
MAX(CASE WHEN Activity = 1 THEN LogTime END) AS LatestActivity_1_Logtime
FROM
Table_1
GROUP BY
Name ;
Test in: dbfiddle.uk
edited 18 mins ago
answered 26 mins ago
ypercubeᵀᴹ
73.8k11124204
73.8k11124204
add a comment |
add a comment |
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.
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%2fdba.stackexchange.com%2fquestions%2f225145%2fhow-to-get-sum-latest-activity-in-same-result%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