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 |
+-----+------+---------+----------+-----------+----------+









share|improve this question
























  • 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















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 |
+-----+------+---------+----------+-----------+----------+









share|improve this question
























  • 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













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 |
+-----+------+---------+----------+-----------+----------+









share|improve this question















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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 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


















  • 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
















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












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






share|improve this answer




























    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;





    share|improve this answer



















    • 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


















    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





    share|improve this answer























      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',
      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
      });


      }
      });














      draft saved

      draft discarded


















      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

























      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






      share|improve this answer

























        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






        share|improve this answer























          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






          share|improve this answer












          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







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 22 at 4:38









          dwir182

          1,170318




          1,170318
























              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;





              share|improve this answer



















              • 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















              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;





              share|improve this answer



















              • 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













              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;





              share|improve this answer














              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;






              share|improve this answer














              share|improve this answer



              share|improve this answer








              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














              • 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










              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





              share|improve this answer



























                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





                share|improve this answer

























                  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





                  share|improve this answer














                  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






                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Nov 22 at 6:28

























                  answered Nov 22 at 6:23









                  Md. Mehedi Hassan

                  176




                  176






























                      draft saved

                      draft discarded




















































                      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.




                      draft saved


                      draft discarded














                      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





















































                      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







                      Popular posts from this blog

                      What visual should I use to simply compare current year value vs last year in Power BI desktop

                      Alexandru Averescu

                      Trompette piccolo