How to count more than one time with different conditions?Select when group by contains more than one row...
Jumping Numbers
Isn't using the Extrusion Multiplier like cheating?
How would a Dictatorship make a country more successful?
Why zero tolerance on nudity in space?
Can a person refuse a presidential pardon?
How to deal with an incendiary email that was recalled
Can a dragon be stuck looking like a human?
Would a National Army of mercenaries be a feasible idea?
Is a debit card dangerous for an account with low balance and no overdraft protection?
Difference between two quite-similar Terminal commands
If I sold a PS4 game I owned the disc for, can I reinstall it digitally?
Can you combine War Caster, whip, and Warlock Features to Eldritch Blast enemies with reach?
How to explain planetary rings pulsating?
Lick explanation
Dilemma of explaining to interviewer that he is the reason for declining second interview
Are there neural networks with very few nodes that decently solve non-trivial problems?
How can animals be objects of ethics without being subjects as well?
What is a jet (unit) shown in Windows 10 calculator?
Solubility of a tribasic weak acid
It took me a lot of time to make this, pls like. (YouTube Comments #1)
Parsing a string of key-value pairs as a dictionary
Word or phrase for showing great skill at something without formal training in it
Can I become debt free or should I file for bankruptcy? How do I manage my debt and finances?
Can an insurance company drop you after receiving a bill and refusing to pay?
How to count more than one time with different conditions?
Select when group by contains more than one row with the same value?how can we merge sum and count?MySQL: Delete all rows older than 30 days, but only if count more than oneGroup COUNT by month less than variableSelect only one duplicate with different time columnHow to select random row with count()MySQL group by multiple columns on multiple tables only one result per groupHow to group a few row values together to put into a new table?How to write group by and having in a relational algebra interpreter?how to count distinct value per day with start time and end time
Having a table like this:

I want to get a result like this:

Something like:
SELECT ParentId, COUNT(SomeValue = 1) [CountMin], COUNT(SomeValue > 1) [CountRest]
FROM MyTable
GROUP BY ParentId
sql-server sql-server-2016 query group-by
add a comment |
Having a table like this:

I want to get a result like this:

Something like:
SELECT ParentId, COUNT(SomeValue = 1) [CountMin], COUNT(SomeValue > 1) [CountRest]
FROM MyTable
GROUP BY ParentId
sql-server sql-server-2016 query group-by
add a comment |
Having a table like this:

I want to get a result like this:

Something like:
SELECT ParentId, COUNT(SomeValue = 1) [CountMin], COUNT(SomeValue > 1) [CountRest]
FROM MyTable
GROUP BY ParentId
sql-server sql-server-2016 query group-by
Having a table like this:

I want to get a result like this:

Something like:
SELECT ParentId, COUNT(SomeValue = 1) [CountMin], COUNT(SomeValue > 1) [CountRest]
FROM MyTable
GROUP BY ParentId
sql-server sql-server-2016 query group-by
sql-server sql-server-2016 query group-by
asked 12 mins ago
Erick Asto OblitasErick Asto Oblitas
11614
11614
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
You can use SUM(CASE...)
SELECT
ParentID,
SUM(CASE WHEN SomeValue = 1 THEN 1 ELSE 0 END) AS CountMin,
SUM(CASE WHEN SomeValue <> 1 THEN 1 ELSE 0 END) AS CountRest
FROM
MyTable
WHERE
ParentID = 123;
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
});
}
});
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%2f231106%2fhow-to-count-more-than-one-time-with-different-conditions%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
You can use SUM(CASE...)
SELECT
ParentID,
SUM(CASE WHEN SomeValue = 1 THEN 1 ELSE 0 END) AS CountMin,
SUM(CASE WHEN SomeValue <> 1 THEN 1 ELSE 0 END) AS CountRest
FROM
MyTable
WHERE
ParentID = 123;
add a comment |
You can use SUM(CASE...)
SELECT
ParentID,
SUM(CASE WHEN SomeValue = 1 THEN 1 ELSE 0 END) AS CountMin,
SUM(CASE WHEN SomeValue <> 1 THEN 1 ELSE 0 END) AS CountRest
FROM
MyTable
WHERE
ParentID = 123;
add a comment |
You can use SUM(CASE...)
SELECT
ParentID,
SUM(CASE WHEN SomeValue = 1 THEN 1 ELSE 0 END) AS CountMin,
SUM(CASE WHEN SomeValue <> 1 THEN 1 ELSE 0 END) AS CountRest
FROM
MyTable
WHERE
ParentID = 123;
You can use SUM(CASE...)
SELECT
ParentID,
SUM(CASE WHEN SomeValue = 1 THEN 1 ELSE 0 END) AS CountMin,
SUM(CASE WHEN SomeValue <> 1 THEN 1 ELSE 0 END) AS CountRest
FROM
MyTable
WHERE
ParentID = 123;
answered 4 mins ago
McNetsMcNets
16k42161
16k42161
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.
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%2f231106%2fhow-to-count-more-than-one-time-with-different-conditions%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