sql:count how many um_no by different section and cumulative by hour
up vote
0
down vote
favorite
how to count how many um_no by different section and cumulative by hour with no specific function
original:
+-------+------+------+-----+
| um_no | nums | hour | day |
+-------+------+------+-----+
| mary | 3 | 8 | 1 |
| john | 6 | 8 | 1 |
| peter | 2 | 8 | 1 |
| jason | 1 | 8 | 1 |
| mary | 5 | 9 | 1 |
| john | 6 | 9 | 1 |
| peter | 6 | 9 | 1 |
| jason | 1 | 9 | 1 |
| mary | 5 | 10 | 1 |
| john | 4 | 10 | 1 |
| peter | 2 | 10 | 1 |
| jason | 4 | 10 | 1 |
+-------+------+------+-----+
want:
+-----+------+---------+----------+-----------+----------+
| day | hour | nums0_5 | nums5_10 | nums10_15 | nums15up |
+-----+------+---------+----------+-----------+----------+
| 1 | 8 | 3 | 1 | 0 | 0 |
| 1 | 9 | 1 | 2 | 1 | 0 |
| 1 | 10 | 0 | 2 | 1 | 1 |
+-----+------+---------+----------+-----------+----------+
sql postgresql
|
show 3 more comments
up vote
0
down vote
favorite
how to count how many um_no by different section and cumulative by hour with no specific function
original:
+-------+------+------+-----+
| um_no | nums | hour | day |
+-------+------+------+-----+
| mary | 3 | 8 | 1 |
| john | 6 | 8 | 1 |
| peter | 2 | 8 | 1 |
| jason | 1 | 8 | 1 |
| mary | 5 | 9 | 1 |
| john | 6 | 9 | 1 |
| peter | 6 | 9 | 1 |
| jason | 1 | 9 | 1 |
| mary | 5 | 10 | 1 |
| john | 4 | 10 | 1 |
| peter | 2 | 10 | 1 |
| jason | 4 | 10 | 1 |
+-------+------+------+-----+
want:
+-----+------+---------+----------+-----------+----------+
| day | hour | nums0_5 | nums5_10 | nums10_15 | nums15up |
+-----+------+---------+----------+-----------+----------+
| 1 | 8 | 3 | 1 | 0 | 0 |
| 1 | 9 | 1 | 2 | 1 | 0 |
| 1 | 10 | 0 | 2 | 1 | 1 |
+-----+------+---------+----------+-----------+----------+
sql postgresql
mysql
<>postgresql
.. Choose 1 you use..
– dwir182
Nov 22 at 3:51
Google for "SQL pivot table"
– Tim Biegeleisen
Nov 22 at 3:53
@dwir182 postgresql;preferably with no specific functions..thanks
– Leon
Nov 22 at 3:57
What's behind the logicnums
this?
– dwir182
Nov 22 at 4:11
@dwir182 nums is the number of cases handled by certain people at certain hour
– Leon
Nov 22 at 4:18
|
show 3 more comments
up vote
0
down vote
favorite
up vote
0
down vote
favorite
how to count how many um_no by different section and cumulative by hour with no specific function
original:
+-------+------+------+-----+
| um_no | nums | hour | day |
+-------+------+------+-----+
| mary | 3 | 8 | 1 |
| john | 6 | 8 | 1 |
| peter | 2 | 8 | 1 |
| jason | 1 | 8 | 1 |
| mary | 5 | 9 | 1 |
| john | 6 | 9 | 1 |
| peter | 6 | 9 | 1 |
| jason | 1 | 9 | 1 |
| mary | 5 | 10 | 1 |
| john | 4 | 10 | 1 |
| peter | 2 | 10 | 1 |
| jason | 4 | 10 | 1 |
+-------+------+------+-----+
want:
+-----+------+---------+----------+-----------+----------+
| day | hour | nums0_5 | nums5_10 | nums10_15 | nums15up |
+-----+------+---------+----------+-----------+----------+
| 1 | 8 | 3 | 1 | 0 | 0 |
| 1 | 9 | 1 | 2 | 1 | 0 |
| 1 | 10 | 0 | 2 | 1 | 1 |
+-----+------+---------+----------+-----------+----------+
sql postgresql
how to count how many um_no by different section and cumulative by hour with no specific function
original:
+-------+------+------+-----+
| um_no | nums | hour | day |
+-------+------+------+-----+
| mary | 3 | 8 | 1 |
| john | 6 | 8 | 1 |
| peter | 2 | 8 | 1 |
| jason | 1 | 8 | 1 |
| mary | 5 | 9 | 1 |
| john | 6 | 9 | 1 |
| peter | 6 | 9 | 1 |
| jason | 1 | 9 | 1 |
| mary | 5 | 10 | 1 |
| john | 4 | 10 | 1 |
| peter | 2 | 10 | 1 |
| jason | 4 | 10 | 1 |
+-------+------+------+-----+
want:
+-----+------+---------+----------+-----------+----------+
| day | hour | nums0_5 | nums5_10 | nums10_15 | nums15up |
+-----+------+---------+----------+-----------+----------+
| 1 | 8 | 3 | 1 | 0 | 0 |
| 1 | 9 | 1 | 2 | 1 | 0 |
| 1 | 10 | 0 | 2 | 1 | 1 |
+-----+------+---------+----------+-----------+----------+
sql postgresql
sql postgresql
edited Nov 22 at 6:58
dwir182
1,170318
1,170318
asked Nov 22 at 3:49
Leon
947
947
mysql
<>postgresql
.. Choose 1 you use..
– dwir182
Nov 22 at 3:51
Google for "SQL pivot table"
– Tim Biegeleisen
Nov 22 at 3:53
@dwir182 postgresql;preferably with no specific functions..thanks
– Leon
Nov 22 at 3:57
What's behind the logicnums
this?
– dwir182
Nov 22 at 4:11
@dwir182 nums is the number of cases handled by certain people at certain hour
– Leon
Nov 22 at 4:18
|
show 3 more comments
mysql
<>postgresql
.. Choose 1 you use..
– dwir182
Nov 22 at 3:51
Google for "SQL pivot table"
– Tim Biegeleisen
Nov 22 at 3:53
@dwir182 postgresql;preferably with no specific functions..thanks
– Leon
Nov 22 at 3:57
What's behind the logicnums
this?
– dwir182
Nov 22 at 4:11
@dwir182 nums is the number of cases handled by certain people at certain hour
– Leon
Nov 22 at 4:18
mysql
<> postgresql
.. Choose 1 you use..– dwir182
Nov 22 at 3:51
mysql
<> postgresql
.. Choose 1 you use..– dwir182
Nov 22 at 3:51
Google for "SQL pivot table"
– Tim Biegeleisen
Nov 22 at 3:53
Google for "SQL pivot table"
– Tim Biegeleisen
Nov 22 at 3:53
@dwir182 postgresql;preferably with no specific functions..thanks
– Leon
Nov 22 at 3:57
@dwir182 postgresql;preferably with no specific functions..thanks
– Leon
Nov 22 at 3:57
What's behind the logic
nums
this?– dwir182
Nov 22 at 4:11
What's behind the logic
nums
this?– dwir182
Nov 22 at 4:11
@dwir182 nums is the number of cases handled by certain people at certain hour
– Leon
Nov 22 at 4:18
@dwir182 nums is the number of cases handled by certain people at certain hour
– Leon
Nov 22 at 4:18
|
show 3 more comments
3 Answers
3
active
oldest
votes
up vote
2
down vote
You can try.. Use with suggestion by gordon which are really great..
select
day,
hour,
count(*) filter (where sum_nums >= 0 and sum_nums <= 5) as nums0_5,
count(*) filter (where sum_nums >= 5 and sum_nums <= 10) as nums5_10,
count(*) filter (where sum_nums >= 10 and sum_nums <= 15) as nums10_15,
count(*) filter (where sum_nums >= 15) as nums15up
from
(select
*,
sum(nums) over (partition by um_no, day order by hour) as sum_nums
from
tbl) t
group by
day,
hour
You can see in here DEMO
add a comment |
up vote
1
down vote
Use conditional aggregation:
select day, hour,
sum( case when nums >= 0 and nums < 5 then 1 else 0 end ) as nums_0_5,
sum( case when nums >= 5 and nums < 10 then 1 else 0 end ) as nums_5_10,
. . .
from t
group by day, hour;
In Postgres, the sums can be simplified to:
count(*) filter (where nums >= 0 and nums < 5) as nums_0_5,
In MySQL:
sum( nums >= 0 and nums < 5 ) as nums_0_5,
EDIT:
You can get the cumulative numbers using window functions and then aggregate:
select day, hour,
sum( case when cume_nums >= 0 and cume_nums < 5 then 1 else 0 end ) as nums_0_5,
sum( case when cume_nums >= 5 and cume_nums < 10 then 1 else 0 end ) as nums_5_10,
. . .
from (select t.*,
sum(nums) over (partition by um_no, date order by hour) as cume_nums
from t
) t
group by day, hour;
2
that's not cumulative by hour
– Leon
Nov 22 at 4:16
Possible duplicate of MySQL pivot table.
– Tim Biegeleisen
Nov 22 at 4:17
add a comment |
up vote
0
down vote
select tt.day, tt.hour,
sum(tt.nums_0_5) as nums_0_5 ,
sum(tt.nums_5_10) as nums_5_10 ,
sum(tt.nums_10_15) as nums_10_15,
sum(tt.nums15up) as nums15up
from
(select sum(case when nums >= 0 and nums < 5 then 1 else 0 end ) as nums_0_5,
sum( case when nums >= 5 and nums < 10 then 1 else 0 end ) as nums_5_10,
sum( case when nums >= 10 and nums < 15 then 1 else 0 end ) as nums_10_15,
sum( case when nums >= 15 then 1 else 0 end ) as nums15up,
t.day,
t.hour,
t.um_no
from (select tbl.day,
tbl.hour,
tbl.um_no,
tbl.nums,
Row_Number() over(partition by hour order by hour asc) as row_nums
from tblTest tbl group by hour, um_no, nums, day) t
group by t.day, t.hour, t.um_no
) tt group by tt.hour, tt.day
add a comment |
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
You can try.. Use with suggestion by gordon which are really great..
select
day,
hour,
count(*) filter (where sum_nums >= 0 and sum_nums <= 5) as nums0_5,
count(*) filter (where sum_nums >= 5 and sum_nums <= 10) as nums5_10,
count(*) filter (where sum_nums >= 10 and sum_nums <= 15) as nums10_15,
count(*) filter (where sum_nums >= 15) as nums15up
from
(select
*,
sum(nums) over (partition by um_no, day order by hour) as sum_nums
from
tbl) t
group by
day,
hour
You can see in here DEMO
add a comment |
up vote
2
down vote
You can try.. Use with suggestion by gordon which are really great..
select
day,
hour,
count(*) filter (where sum_nums >= 0 and sum_nums <= 5) as nums0_5,
count(*) filter (where sum_nums >= 5 and sum_nums <= 10) as nums5_10,
count(*) filter (where sum_nums >= 10 and sum_nums <= 15) as nums10_15,
count(*) filter (where sum_nums >= 15) as nums15up
from
(select
*,
sum(nums) over (partition by um_no, day order by hour) as sum_nums
from
tbl) t
group by
day,
hour
You can see in here DEMO
add a comment |
up vote
2
down vote
up vote
2
down vote
You can try.. Use with suggestion by gordon which are really great..
select
day,
hour,
count(*) filter (where sum_nums >= 0 and sum_nums <= 5) as nums0_5,
count(*) filter (where sum_nums >= 5 and sum_nums <= 10) as nums5_10,
count(*) filter (where sum_nums >= 10 and sum_nums <= 15) as nums10_15,
count(*) filter (where sum_nums >= 15) as nums15up
from
(select
*,
sum(nums) over (partition by um_no, day order by hour) as sum_nums
from
tbl) t
group by
day,
hour
You can see in here DEMO
You can try.. Use with suggestion by gordon which are really great..
select
day,
hour,
count(*) filter (where sum_nums >= 0 and sum_nums <= 5) as nums0_5,
count(*) filter (where sum_nums >= 5 and sum_nums <= 10) as nums5_10,
count(*) filter (where sum_nums >= 10 and sum_nums <= 15) as nums10_15,
count(*) filter (where sum_nums >= 15) as nums15up
from
(select
*,
sum(nums) over (partition by um_no, day order by hour) as sum_nums
from
tbl) t
group by
day,
hour
You can see in here DEMO
answered Nov 22 at 4:38
dwir182
1,170318
1,170318
add a comment |
add a comment |
up vote
1
down vote
Use conditional aggregation:
select day, hour,
sum( case when nums >= 0 and nums < 5 then 1 else 0 end ) as nums_0_5,
sum( case when nums >= 5 and nums < 10 then 1 else 0 end ) as nums_5_10,
. . .
from t
group by day, hour;
In Postgres, the sums can be simplified to:
count(*) filter (where nums >= 0 and nums < 5) as nums_0_5,
In MySQL:
sum( nums >= 0 and nums < 5 ) as nums_0_5,
EDIT:
You can get the cumulative numbers using window functions and then aggregate:
select day, hour,
sum( case when cume_nums >= 0 and cume_nums < 5 then 1 else 0 end ) as nums_0_5,
sum( case when cume_nums >= 5 and cume_nums < 10 then 1 else 0 end ) as nums_5_10,
. . .
from (select t.*,
sum(nums) over (partition by um_no, date order by hour) as cume_nums
from t
) t
group by day, hour;
2
that's not cumulative by hour
– Leon
Nov 22 at 4:16
Possible duplicate of MySQL pivot table.
– Tim Biegeleisen
Nov 22 at 4:17
add a comment |
up vote
1
down vote
Use conditional aggregation:
select day, hour,
sum( case when nums >= 0 and nums < 5 then 1 else 0 end ) as nums_0_5,
sum( case when nums >= 5 and nums < 10 then 1 else 0 end ) as nums_5_10,
. . .
from t
group by day, hour;
In Postgres, the sums can be simplified to:
count(*) filter (where nums >= 0 and nums < 5) as nums_0_5,
In MySQL:
sum( nums >= 0 and nums < 5 ) as nums_0_5,
EDIT:
You can get the cumulative numbers using window functions and then aggregate:
select day, hour,
sum( case when cume_nums >= 0 and cume_nums < 5 then 1 else 0 end ) as nums_0_5,
sum( case when cume_nums >= 5 and cume_nums < 10 then 1 else 0 end ) as nums_5_10,
. . .
from (select t.*,
sum(nums) over (partition by um_no, date order by hour) as cume_nums
from t
) t
group by day, hour;
2
that's not cumulative by hour
– Leon
Nov 22 at 4:16
Possible duplicate of MySQL pivot table.
– Tim Biegeleisen
Nov 22 at 4:17
add a comment |
up vote
1
down vote
up vote
1
down vote
Use conditional aggregation:
select day, hour,
sum( case when nums >= 0 and nums < 5 then 1 else 0 end ) as nums_0_5,
sum( case when nums >= 5 and nums < 10 then 1 else 0 end ) as nums_5_10,
. . .
from t
group by day, hour;
In Postgres, the sums can be simplified to:
count(*) filter (where nums >= 0 and nums < 5) as nums_0_5,
In MySQL:
sum( nums >= 0 and nums < 5 ) as nums_0_5,
EDIT:
You can get the cumulative numbers using window functions and then aggregate:
select day, hour,
sum( case when cume_nums >= 0 and cume_nums < 5 then 1 else 0 end ) as nums_0_5,
sum( case when cume_nums >= 5 and cume_nums < 10 then 1 else 0 end ) as nums_5_10,
. . .
from (select t.*,
sum(nums) over (partition by um_no, date order by hour) as cume_nums
from t
) t
group by day, hour;
Use conditional aggregation:
select day, hour,
sum( case when nums >= 0 and nums < 5 then 1 else 0 end ) as nums_0_5,
sum( case when nums >= 5 and nums < 10 then 1 else 0 end ) as nums_5_10,
. . .
from t
group by day, hour;
In Postgres, the sums can be simplified to:
count(*) filter (where nums >= 0 and nums < 5) as nums_0_5,
In MySQL:
sum( nums >= 0 and nums < 5 ) as nums_0_5,
EDIT:
You can get the cumulative numbers using window functions and then aggregate:
select day, hour,
sum( case when cume_nums >= 0 and cume_nums < 5 then 1 else 0 end ) as nums_0_5,
sum( case when cume_nums >= 5 and cume_nums < 10 then 1 else 0 end ) as nums_5_10,
. . .
from (select t.*,
sum(nums) over (partition by um_no, date order by hour) as cume_nums
from t
) t
group by day, hour;
edited Nov 22 at 4:22
answered Nov 22 at 4:04
Gordon Linoff
748k34285391
748k34285391
2
that's not cumulative by hour
– Leon
Nov 22 at 4:16
Possible duplicate of MySQL pivot table.
– Tim Biegeleisen
Nov 22 at 4:17
add a comment |
2
that's not cumulative by hour
– Leon
Nov 22 at 4:16
Possible duplicate of MySQL pivot table.
– Tim Biegeleisen
Nov 22 at 4:17
2
2
that's not cumulative by hour
– Leon
Nov 22 at 4:16
that's not cumulative by hour
– Leon
Nov 22 at 4:16
Possible duplicate of MySQL pivot table.
– Tim Biegeleisen
Nov 22 at 4:17
Possible duplicate of MySQL pivot table.
– Tim Biegeleisen
Nov 22 at 4:17
add a comment |
up vote
0
down vote
select tt.day, tt.hour,
sum(tt.nums_0_5) as nums_0_5 ,
sum(tt.nums_5_10) as nums_5_10 ,
sum(tt.nums_10_15) as nums_10_15,
sum(tt.nums15up) as nums15up
from
(select sum(case when nums >= 0 and nums < 5 then 1 else 0 end ) as nums_0_5,
sum( case when nums >= 5 and nums < 10 then 1 else 0 end ) as nums_5_10,
sum( case when nums >= 10 and nums < 15 then 1 else 0 end ) as nums_10_15,
sum( case when nums >= 15 then 1 else 0 end ) as nums15up,
t.day,
t.hour,
t.um_no
from (select tbl.day,
tbl.hour,
tbl.um_no,
tbl.nums,
Row_Number() over(partition by hour order by hour asc) as row_nums
from tblTest tbl group by hour, um_no, nums, day) t
group by t.day, t.hour, t.um_no
) tt group by tt.hour, tt.day
add a comment |
up vote
0
down vote
select tt.day, tt.hour,
sum(tt.nums_0_5) as nums_0_5 ,
sum(tt.nums_5_10) as nums_5_10 ,
sum(tt.nums_10_15) as nums_10_15,
sum(tt.nums15up) as nums15up
from
(select sum(case when nums >= 0 and nums < 5 then 1 else 0 end ) as nums_0_5,
sum( case when nums >= 5 and nums < 10 then 1 else 0 end ) as nums_5_10,
sum( case when nums >= 10 and nums < 15 then 1 else 0 end ) as nums_10_15,
sum( case when nums >= 15 then 1 else 0 end ) as nums15up,
t.day,
t.hour,
t.um_no
from (select tbl.day,
tbl.hour,
tbl.um_no,
tbl.nums,
Row_Number() over(partition by hour order by hour asc) as row_nums
from tblTest tbl group by hour, um_no, nums, day) t
group by t.day, t.hour, t.um_no
) tt group by tt.hour, tt.day
add a comment |
up vote
0
down vote
up vote
0
down vote
select tt.day, tt.hour,
sum(tt.nums_0_5) as nums_0_5 ,
sum(tt.nums_5_10) as nums_5_10 ,
sum(tt.nums_10_15) as nums_10_15,
sum(tt.nums15up) as nums15up
from
(select sum(case when nums >= 0 and nums < 5 then 1 else 0 end ) as nums_0_5,
sum( case when nums >= 5 and nums < 10 then 1 else 0 end ) as nums_5_10,
sum( case when nums >= 10 and nums < 15 then 1 else 0 end ) as nums_10_15,
sum( case when nums >= 15 then 1 else 0 end ) as nums15up,
t.day,
t.hour,
t.um_no
from (select tbl.day,
tbl.hour,
tbl.um_no,
tbl.nums,
Row_Number() over(partition by hour order by hour asc) as row_nums
from tblTest tbl group by hour, um_no, nums, day) t
group by t.day, t.hour, t.um_no
) tt group by tt.hour, tt.day
select tt.day, tt.hour,
sum(tt.nums_0_5) as nums_0_5 ,
sum(tt.nums_5_10) as nums_5_10 ,
sum(tt.nums_10_15) as nums_10_15,
sum(tt.nums15up) as nums15up
from
(select sum(case when nums >= 0 and nums < 5 then 1 else 0 end ) as nums_0_5,
sum( case when nums >= 5 and nums < 10 then 1 else 0 end ) as nums_5_10,
sum( case when nums >= 10 and nums < 15 then 1 else 0 end ) as nums_10_15,
sum( case when nums >= 15 then 1 else 0 end ) as nums15up,
t.day,
t.hour,
t.um_no
from (select tbl.day,
tbl.hour,
tbl.um_no,
tbl.nums,
Row_Number() over(partition by hour order by hour asc) as row_nums
from tblTest tbl group by hour, um_no, nums, day) t
group by t.day, t.hour, t.um_no
) tt group by tt.hour, tt.day
edited Nov 22 at 6:28
answered Nov 22 at 6:23
Md. Mehedi Hassan
176
176
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%2f53423613%2fsqlcount-how-many-um-no-by-different-section-and-cumulative-by-hour%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
mysql
<>postgresql
.. Choose 1 you use..– dwir182
Nov 22 at 3:51
Google for "SQL pivot table"
– Tim Biegeleisen
Nov 22 at 3:53
@dwir182 postgresql;preferably with no specific functions..thanks
– Leon
Nov 22 at 3:57
What's behind the logic
nums
this?– dwir182
Nov 22 at 4:11
@dwir182 nums is the number of cases handled by certain people at certain hour
– Leon
Nov 22 at 4:18