SQL Counting

  • ServerNameDrive%Value
    ServerAE:\21
    ServerAE:\21
    ServerAE:\13
    ServerAF:\12
    ServerAC:\12
    ServerAE:\2.4
    ServerAE:\3
    ServerAE:\0
    ServerAE:\0
    ServerBC:\21
    ServerBD:\21
    ServerBE:\13
    ServerBE:\24
    ServerBE:\25
    ServerBE:\9
    ServerBE:\4
    ServerBF:\4
    ServerBF:\5
    ServerCF:\50
    ServerCD:\70
    ServerCF:\90

    Column Names in select result: [ServerName]    [Drive]    [0-10%]    [11-20%]    [20-30%]    [30-100%]
    For the above values how can depict it in a select statement which provides data as attached.
    How to count values between various intervals(4 in here) and count the number of occurences for each drive by server.

    Thanks

  • Use a CASE statement. For example:
    CASE WHEN Value BETWEEN 0 AND 10 THEN 1 ELSE 0 END

    Then use a GROUP BY on your Server Name and Drive and sum your CASE statements. Have a go and if you get stuck show what you tried and we can get you back on track.

    Thom~

    Excuse my typos and sometimes awful grammar. My fingers work faster than my brain does.
    Larnu.uk

  • Quick pointer... looks like you want something like a cross tab.  See Jeff Moden's article "Cross Tabs and Pivots".

    Edit.

    Code would probably end up looking something like this:

    SELECT ServerName, Drive,
        SUM (CASE WHEN val >= 0 AND val < 10 THEN 1 ELSE NULL END) AS [0-10%],
        SUM (CASE WHEN val >= 10 AND val < 20 THEN 1 ELSE NULL END) AS [10-20%],
        SUM (CASE WHEN val>=20 AND val<30 THEN 1 ELSE NULL END) AS [20-30%],
        SUM (CASE WHEN val>=30 THEN 1 ELSE NULL END) AS [30-100%]
    FROM <<tablenamehere>>
    GROUP BY servername, drive
    ORDER BY servername, drive

    Thomas Rushton
    blog: https://thelonedba.wordpress.com

Viewing 3 posts - 1 through 2 (of 2 total)

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