Count rows with specific entry for every distinct entry of another row
up vote
1
down vote
favorite
So my task is to calculate some simple KPIs.
I have already accumulated a view with all the data I need.
Year_CW Is Started Needs Help
-------------------------------------
2018/45 0 1
2018/43 1 1
2018/45 0 1
2018/42 1 0
2018/45 0 1
2018/45 1 1
2018/41 0 1
2018/43 0 0
2018/45 1 1
2018/45 0 0
I then wrote the following query:
SELECT DISTINCT YEAR_CW
FROM TestView
ORDER BY YEAR_CW DESC
Which returns this
Year_CW
--------
2018/45
2018/44
2018/43
2018/42
I would now like to count for each Year_CW how often there is a 1 and how often there is a zero for both of the other rows. This may be a fairly simple question but I'm just starting with SQL and I really don't know what the keyword is for a query based on an outer query.
The other queries would be
Select Count(Is Started)
from Testview
Where Is Started = 1
And so on for the others. But I really don't know how to put them together and base them on the first query.
Thanks for your help.
sql oracle11g
add a comment |
up vote
1
down vote
favorite
So my task is to calculate some simple KPIs.
I have already accumulated a view with all the data I need.
Year_CW Is Started Needs Help
-------------------------------------
2018/45 0 1
2018/43 1 1
2018/45 0 1
2018/42 1 0
2018/45 0 1
2018/45 1 1
2018/41 0 1
2018/43 0 0
2018/45 1 1
2018/45 0 0
I then wrote the following query:
SELECT DISTINCT YEAR_CW
FROM TestView
ORDER BY YEAR_CW DESC
Which returns this
Year_CW
--------
2018/45
2018/44
2018/43
2018/42
I would now like to count for each Year_CW how often there is a 1 and how often there is a zero for both of the other rows. This may be a fairly simple question but I'm just starting with SQL and I really don't know what the keyword is for a query based on an outer query.
The other queries would be
Select Count(Is Started)
from Testview
Where Is Started = 1
And so on for the others. But I really don't know how to put them together and base them on the first query.
Thanks for your help.
sql oracle11g
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
So my task is to calculate some simple KPIs.
I have already accumulated a view with all the data I need.
Year_CW Is Started Needs Help
-------------------------------------
2018/45 0 1
2018/43 1 1
2018/45 0 1
2018/42 1 0
2018/45 0 1
2018/45 1 1
2018/41 0 1
2018/43 0 0
2018/45 1 1
2018/45 0 0
I then wrote the following query:
SELECT DISTINCT YEAR_CW
FROM TestView
ORDER BY YEAR_CW DESC
Which returns this
Year_CW
--------
2018/45
2018/44
2018/43
2018/42
I would now like to count for each Year_CW how often there is a 1 and how often there is a zero for both of the other rows. This may be a fairly simple question but I'm just starting with SQL and I really don't know what the keyword is for a query based on an outer query.
The other queries would be
Select Count(Is Started)
from Testview
Where Is Started = 1
And so on for the others. But I really don't know how to put them together and base them on the first query.
Thanks for your help.
sql oracle11g
So my task is to calculate some simple KPIs.
I have already accumulated a view with all the data I need.
Year_CW Is Started Needs Help
-------------------------------------
2018/45 0 1
2018/43 1 1
2018/45 0 1
2018/42 1 0
2018/45 0 1
2018/45 1 1
2018/41 0 1
2018/43 0 0
2018/45 1 1
2018/45 0 0
I then wrote the following query:
SELECT DISTINCT YEAR_CW
FROM TestView
ORDER BY YEAR_CW DESC
Which returns this
Year_CW
--------
2018/45
2018/44
2018/43
2018/42
I would now like to count for each Year_CW how often there is a 1 and how often there is a zero for both of the other rows. This may be a fairly simple question but I'm just starting with SQL and I really don't know what the keyword is for a query based on an outer query.
The other queries would be
Select Count(Is Started)
from Testview
Where Is Started = 1
And so on for the others. But I really don't know how to put them together and base them on the first query.
Thanks for your help.
sql oracle11g
sql oracle11g
edited Nov 21 at 20:34
marc_s
567k12810971248
567k12810971248
asked Nov 21 at 13:12
Odatas
404
404
add a comment |
add a comment |
4 Answers
4
active
oldest
votes
up vote
0
down vote
accepted
You seems want conditional aggregation :
select Year_CW,
sum(case when col = 1 then 1 else 0 end) as one_count,
sum(case when col = 0 then 1 else 0 end) as zero_count
from (select Year_CW, IsStarted as col
from TestView tv
union all
select Year_CW, NeedsHelp
from TestView tv
) tv
group by Year_CW
order by Year_CW desc;
This does the work. Is there a way to sort the group "Year_CW" descending?
– Odatas
Nov 21 at 13:31
@Odatas. . . You can doorder by Year_CW desc.
– Yogesh Sharma
Nov 21 at 13:32
add a comment |
up vote
1
down vote
select Year_CW
, sum(case when Is_Started = 1 then 1 end) as Is_Started_1
, sum(case when Is_Started = 0 then 1 end) as Is_Started_0
, sum(case when Needs_Help = 1 then 1 end) as Needs_Help_1
, sum(case when Needs_Help = 0 then 1 end) as Needs_Help_0
from Test_View
group by Year_CW
So how I did it is I created 4 new fields for you. First one is giving value ‘1’ to every field where ‘Is_Started = 1’ and then I sum the instances. I did the same for 0 values and another two fields for values 1 and 0 for ‘Needs_ Help’ column. I believe this will give you your desired result.
Adding an explanation of the query provided would be good to help others understand what you have done.
– Dessus
Nov 21 at 21:02
Comment added brother. Sorry.
– Filip Kubiak
Nov 21 at 21:09
add a comment |
up vote
0
down vote
So, if I'm understanding the question correctly you're just looking for the SUM of the additional two columns GROUP BY the Year_CW field. That would be the following.
SELECT Year_CW, SUM([Is Started]), SUM([Needs Help])
FROM TestView
GROUP BY Year_CW
Not just the sum (This would only work for 1s) but the number of times specific entrys appear. But the "Group by" is the thing i was searching for.
– Odatas
Nov 21 at 13:32
add a comment |
up vote
0
down vote
If it's only 0 or 1, then both 0 or 1 can be summed up.
SELECT YEAR_CW,
SUM("Is Started") AS TotalStarted,
SUM(1 - "Is Started") AS TotalNotStarted,
SUM("Needs Help") AS TotalNeedsHelp,
SUM(1 - "Needs Help") AS TotalNoHelpNeeded
FROM TestView
GROUP BY YEAR_CW
ORDER BY YEAR_CW DESC
add a comment |
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
accepted
You seems want conditional aggregation :
select Year_CW,
sum(case when col = 1 then 1 else 0 end) as one_count,
sum(case when col = 0 then 1 else 0 end) as zero_count
from (select Year_CW, IsStarted as col
from TestView tv
union all
select Year_CW, NeedsHelp
from TestView tv
) tv
group by Year_CW
order by Year_CW desc;
This does the work. Is there a way to sort the group "Year_CW" descending?
– Odatas
Nov 21 at 13:31
@Odatas. . . You can doorder by Year_CW desc.
– Yogesh Sharma
Nov 21 at 13:32
add a comment |
up vote
0
down vote
accepted
You seems want conditional aggregation :
select Year_CW,
sum(case when col = 1 then 1 else 0 end) as one_count,
sum(case when col = 0 then 1 else 0 end) as zero_count
from (select Year_CW, IsStarted as col
from TestView tv
union all
select Year_CW, NeedsHelp
from TestView tv
) tv
group by Year_CW
order by Year_CW desc;
This does the work. Is there a way to sort the group "Year_CW" descending?
– Odatas
Nov 21 at 13:31
@Odatas. . . You can doorder by Year_CW desc.
– Yogesh Sharma
Nov 21 at 13:32
add a comment |
up vote
0
down vote
accepted
up vote
0
down vote
accepted
You seems want conditional aggregation :
select Year_CW,
sum(case when col = 1 then 1 else 0 end) as one_count,
sum(case when col = 0 then 1 else 0 end) as zero_count
from (select Year_CW, IsStarted as col
from TestView tv
union all
select Year_CW, NeedsHelp
from TestView tv
) tv
group by Year_CW
order by Year_CW desc;
You seems want conditional aggregation :
select Year_CW,
sum(case when col = 1 then 1 else 0 end) as one_count,
sum(case when col = 0 then 1 else 0 end) as zero_count
from (select Year_CW, IsStarted as col
from TestView tv
union all
select Year_CW, NeedsHelp
from TestView tv
) tv
group by Year_CW
order by Year_CW desc;
edited Nov 21 at 13:31
answered Nov 21 at 13:21
Yogesh Sharma
27k51335
27k51335
This does the work. Is there a way to sort the group "Year_CW" descending?
– Odatas
Nov 21 at 13:31
@Odatas. . . You can doorder by Year_CW desc.
– Yogesh Sharma
Nov 21 at 13:32
add a comment |
This does the work. Is there a way to sort the group "Year_CW" descending?
– Odatas
Nov 21 at 13:31
@Odatas. . . You can doorder by Year_CW desc.
– Yogesh Sharma
Nov 21 at 13:32
This does the work. Is there a way to sort the group "Year_CW" descending?
– Odatas
Nov 21 at 13:31
This does the work. Is there a way to sort the group "Year_CW" descending?
– Odatas
Nov 21 at 13:31
@Odatas. . . You can do
order by Year_CW desc.– Yogesh Sharma
Nov 21 at 13:32
@Odatas. . . You can do
order by Year_CW desc.– Yogesh Sharma
Nov 21 at 13:32
add a comment |
up vote
1
down vote
select Year_CW
, sum(case when Is_Started = 1 then 1 end) as Is_Started_1
, sum(case when Is_Started = 0 then 1 end) as Is_Started_0
, sum(case when Needs_Help = 1 then 1 end) as Needs_Help_1
, sum(case when Needs_Help = 0 then 1 end) as Needs_Help_0
from Test_View
group by Year_CW
So how I did it is I created 4 new fields for you. First one is giving value ‘1’ to every field where ‘Is_Started = 1’ and then I sum the instances. I did the same for 0 values and another two fields for values 1 and 0 for ‘Needs_ Help’ column. I believe this will give you your desired result.
Adding an explanation of the query provided would be good to help others understand what you have done.
– Dessus
Nov 21 at 21:02
Comment added brother. Sorry.
– Filip Kubiak
Nov 21 at 21:09
add a comment |
up vote
1
down vote
select Year_CW
, sum(case when Is_Started = 1 then 1 end) as Is_Started_1
, sum(case when Is_Started = 0 then 1 end) as Is_Started_0
, sum(case when Needs_Help = 1 then 1 end) as Needs_Help_1
, sum(case when Needs_Help = 0 then 1 end) as Needs_Help_0
from Test_View
group by Year_CW
So how I did it is I created 4 new fields for you. First one is giving value ‘1’ to every field where ‘Is_Started = 1’ and then I sum the instances. I did the same for 0 values and another two fields for values 1 and 0 for ‘Needs_ Help’ column. I believe this will give you your desired result.
Adding an explanation of the query provided would be good to help others understand what you have done.
– Dessus
Nov 21 at 21:02
Comment added brother. Sorry.
– Filip Kubiak
Nov 21 at 21:09
add a comment |
up vote
1
down vote
up vote
1
down vote
select Year_CW
, sum(case when Is_Started = 1 then 1 end) as Is_Started_1
, sum(case when Is_Started = 0 then 1 end) as Is_Started_0
, sum(case when Needs_Help = 1 then 1 end) as Needs_Help_1
, sum(case when Needs_Help = 0 then 1 end) as Needs_Help_0
from Test_View
group by Year_CW
So how I did it is I created 4 new fields for you. First one is giving value ‘1’ to every field where ‘Is_Started = 1’ and then I sum the instances. I did the same for 0 values and another two fields for values 1 and 0 for ‘Needs_ Help’ column. I believe this will give you your desired result.
select Year_CW
, sum(case when Is_Started = 1 then 1 end) as Is_Started_1
, sum(case when Is_Started = 0 then 1 end) as Is_Started_0
, sum(case when Needs_Help = 1 then 1 end) as Needs_Help_1
, sum(case when Needs_Help = 0 then 1 end) as Needs_Help_0
from Test_View
group by Year_CW
So how I did it is I created 4 new fields for you. First one is giving value ‘1’ to every field where ‘Is_Started = 1’ and then I sum the instances. I did the same for 0 values and another two fields for values 1 and 0 for ‘Needs_ Help’ column. I believe this will give you your desired result.
edited Nov 21 at 21:07
answered Nov 21 at 20:59
Filip Kubiak
112
112
Adding an explanation of the query provided would be good to help others understand what you have done.
– Dessus
Nov 21 at 21:02
Comment added brother. Sorry.
– Filip Kubiak
Nov 21 at 21:09
add a comment |
Adding an explanation of the query provided would be good to help others understand what you have done.
– Dessus
Nov 21 at 21:02
Comment added brother. Sorry.
– Filip Kubiak
Nov 21 at 21:09
Adding an explanation of the query provided would be good to help others understand what you have done.
– Dessus
Nov 21 at 21:02
Adding an explanation of the query provided would be good to help others understand what you have done.
– Dessus
Nov 21 at 21:02
Comment added brother. Sorry.
– Filip Kubiak
Nov 21 at 21:09
Comment added brother. Sorry.
– Filip Kubiak
Nov 21 at 21:09
add a comment |
up vote
0
down vote
So, if I'm understanding the question correctly you're just looking for the SUM of the additional two columns GROUP BY the Year_CW field. That would be the following.
SELECT Year_CW, SUM([Is Started]), SUM([Needs Help])
FROM TestView
GROUP BY Year_CW
Not just the sum (This would only work for 1s) but the number of times specific entrys appear. But the "Group by" is the thing i was searching for.
– Odatas
Nov 21 at 13:32
add a comment |
up vote
0
down vote
So, if I'm understanding the question correctly you're just looking for the SUM of the additional two columns GROUP BY the Year_CW field. That would be the following.
SELECT Year_CW, SUM([Is Started]), SUM([Needs Help])
FROM TestView
GROUP BY Year_CW
Not just the sum (This would only work for 1s) but the number of times specific entrys appear. But the "Group by" is the thing i was searching for.
– Odatas
Nov 21 at 13:32
add a comment |
up vote
0
down vote
up vote
0
down vote
So, if I'm understanding the question correctly you're just looking for the SUM of the additional two columns GROUP BY the Year_CW field. That would be the following.
SELECT Year_CW, SUM([Is Started]), SUM([Needs Help])
FROM TestView
GROUP BY Year_CW
So, if I'm understanding the question correctly you're just looking for the SUM of the additional two columns GROUP BY the Year_CW field. That would be the following.
SELECT Year_CW, SUM([Is Started]), SUM([Needs Help])
FROM TestView
GROUP BY Year_CW
answered Nov 21 at 13:20
jradich1234
1,11341725
1,11341725
Not just the sum (This would only work for 1s) but the number of times specific entrys appear. But the "Group by" is the thing i was searching for.
– Odatas
Nov 21 at 13:32
add a comment |
Not just the sum (This would only work for 1s) but the number of times specific entrys appear. But the "Group by" is the thing i was searching for.
– Odatas
Nov 21 at 13:32
Not just the sum (This would only work for 1s) but the number of times specific entrys appear. But the "Group by" is the thing i was searching for.
– Odatas
Nov 21 at 13:32
Not just the sum (This would only work for 1s) but the number of times specific entrys appear. But the "Group by" is the thing i was searching for.
– Odatas
Nov 21 at 13:32
add a comment |
up vote
0
down vote
If it's only 0 or 1, then both 0 or 1 can be summed up.
SELECT YEAR_CW,
SUM("Is Started") AS TotalStarted,
SUM(1 - "Is Started") AS TotalNotStarted,
SUM("Needs Help") AS TotalNeedsHelp,
SUM(1 - "Needs Help") AS TotalNoHelpNeeded
FROM TestView
GROUP BY YEAR_CW
ORDER BY YEAR_CW DESC
add a comment |
up vote
0
down vote
If it's only 0 or 1, then both 0 or 1 can be summed up.
SELECT YEAR_CW,
SUM("Is Started") AS TotalStarted,
SUM(1 - "Is Started") AS TotalNotStarted,
SUM("Needs Help") AS TotalNeedsHelp,
SUM(1 - "Needs Help") AS TotalNoHelpNeeded
FROM TestView
GROUP BY YEAR_CW
ORDER BY YEAR_CW DESC
add a comment |
up vote
0
down vote
up vote
0
down vote
If it's only 0 or 1, then both 0 or 1 can be summed up.
SELECT YEAR_CW,
SUM("Is Started") AS TotalStarted,
SUM(1 - "Is Started") AS TotalNotStarted,
SUM("Needs Help") AS TotalNeedsHelp,
SUM(1 - "Needs Help") AS TotalNoHelpNeeded
FROM TestView
GROUP BY YEAR_CW
ORDER BY YEAR_CW DESC
If it's only 0 or 1, then both 0 or 1 can be summed up.
SELECT YEAR_CW,
SUM("Is Started") AS TotalStarted,
SUM(1 - "Is Started") AS TotalNotStarted,
SUM("Needs Help") AS TotalNeedsHelp,
SUM(1 - "Needs Help") AS TotalNoHelpNeeded
FROM TestView
GROUP BY YEAR_CW
ORDER BY YEAR_CW DESC
edited Nov 21 at 13:40
answered Nov 21 at 13:23
LukStorms
11.1k31532
11.1k31532
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%2f53412834%2fcount-rows-with-specific-entry-for-every-distinct-entry-of-another-row%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