Group data and count by interval data

  • Hello,

    I need to group and count data values from a table to get a result like:

    Limits Total

    [0-5] 2

    [6-10] 3

    [10-100] 1

    how can I do this?

    Thank you,

    Augusto

  • Hi Augusto

    This is usually straightforward - what does your source table look like? Can you post DDL with some sample data please?

    โ€œWrite the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.โ€ - Gail Shaw

    For fast, accurate and documented assistance in answering your questions, please read this article.
    Understanding and using APPLY, (I) and (II) Paul White
    Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden

  • Try

    selectcase

    when col between 0 and 5 then '[0-5]'

    when col between 6 and 10 then '[6-10]'

    when col between 11 and 100 then '[11-100]'

    end as Limits,

    count(*) as Total

    from

    your_table

    group by

    case

    when col between 0 and 5 then '[0-5]'

    when col between 6 and 10 then '[6-10]'

    when col between 11 and 100 then '[11-100]'

    end


    Madhivanan

    Failing to plan is Planning to fail

  • Hello,

    Madhivanan, your example help me to solve my problem.

    Thank you,

    Augusto

  • amfps (10/3/2008)


    Hello,

    Madhivanan, your example help me to solve my problem.

    Thank you,

    Augusto

    You are welcome ๐Ÿ™‚


    Madhivanan

    Failing to plan is Planning to fail

Viewing 5 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic. Login to reply