Help With Query

  • Hello. I have the following table and sample data:

    Id | CacheString

    -----------------

    1 | 'Cache miss: Entity 1'

    2 | 'Cache hit: Entity 1'

    3 | 'Cache miss: Entity 4'

    4 | 'Cache miss: Entity 3'

    5 | 'Cache hit: Entity 10'

    What I am trying to do is generate a quick report of the "Cache hit ratio". I cannot change how the data goes into this table, so I cannot parse the Cache string into different columns. I would like to see the count of 'Cache hit's divided by the Sum of cache entries grouped by the 'Entity'.

    For example, I would like to see:

    Entity | Hit Ratio

    -----------------

    'Entity 1' | .5

    'Entity 3' | 0

    'Entity 4' | 0

    'Entity 10' | 1

    Any help is appreciated.

    Regards,

    Aaron

  • Aaron,

    This should do it... also, take a look at how I created the test table and data... would help a lot if you could post sample data like that in the future, please.

    [font="Courier New"]--=====&nbspCreate&nbspand&nbsppopulate&nbspa&nbsptable&nbspvariable&nbspwith&nbsptest&nbspdata...

    &nbsp&nbsp&nbsp&nbsp&nbsp--&nbspTHIS&nbspIS&nbspNOT&nbspPART&nbspOF&nbspTHE&nbspSOLUTION!!!!

    DECLARE&nbsp@yourtable&nbspTABLE

    &nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp(

    &nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbspID&nbspINT&nbspIDENTITY(1,1)&nbspPRIMARY&nbspKEY&nbspCLUSTERED,

    &nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbspCacheString&nbspVARCHAR(40)

    &nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp)

    &nbspINSERT&nbspINTO&nbsp@yourtable&nbsp(CacheString)

    &nbspSELECT&nbsp'Cache&nbspmiss:&nbspEntity&nbsp1'&nbspUNION&nbspALL

    &nbspSELECT&nbsp'Cache&nbsphit:&nbspEntity&nbsp1'&nbspUNION&nbspALL

    &nbspSELECT&nbsp'Cache&nbspmiss:&nbspEntity&nbsp4'&nbspUNION&nbspALL

    &nbspSELECT&nbsp'Cache&nbspmiss:&nbspEntity&nbsp3'&nbspUNION&nbspALL

    &nbspSELECT&nbsp'Cache&nbsphit:&nbspEntity&nbsp10'

    --=====&nbspSolve&nbspthe&nbspproblem&nbspwith&nbspan&nbspon-the-fly&nbspparse...

    &nbspSELECT&nbspEntity&nbsp=&nbsp'Entity'

    &nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp+&nbspSTR(CAST(SUBSTRING(CacheString,CHARINDEX(':&nbsp',CacheString)+9,40)&nbspAS&nbspINT),3),

    &nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbspHitRatio&nbsp=&nbspSTR(SUM(CASE&nbspWHEN&nbspLEFT(CacheString,10)&nbsp=&nbsp'Cache&nbsphit:'&nbspTHEN&nbsp1.0&nbspELSE&nbsp0.0&nbspEND)&nbsp/&nbspCOUNT(*)&nbsp,&nbsp5,1)

    &nbsp&nbsp&nbspFROM&nbsp@yourtable

    &nbsp&nbspGROUP&nbspBY&nbsp'Entity'

    &nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp+&nbspSTR(CAST(SUBSTRING(CacheString,CHARINDEX(':&nbsp',CacheString)+9,40)&nbspAS&nbspINT),3)

    [/font]

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

  • Looks like you beat me to it Jeff. My solution is a little different than yours and I dont know how the performance is different but there is more than one way to skin a cat.

    DECLARE @t TABLE(

    Id INT,

    CacheString VARCHAR(25)

    )

    INSERT INTO @t

    SELECT 1 , 'Cache miss: Entity 1' UNION ALL

    SELECT 2 , 'Cache hit: Entity 1' UNION ALL

    SELECT 3 , 'Cache miss: Entity 4' UNION ALL

    SELECT 4 , 'Cache miss: Entity 3' UNION ALL

    SELECT 5 , 'Cache hit: Entity 10'

    SELECT a.Entitity,AVG(a.hit)

    FROM(

    SELECT

    CASE WHEN CacheString LIKE '%miss%' THEN

    RIGHT(CacheString,LEN(CacheString) - 18)

    ELSE

    RIGHT(CacheString,LEN(CacheString) - 17)

    END AS [Entitity],

    CASE WHEN CacheString LIKE '%hit%' THEN

    CAST(1 AS DECIMAL(5,3))

    ELSE

    CAST(0 AS DECIMAL(5,3))

    END AS [hit]

    FROM @t

    ) AS a

    GROUP BY a.Entitity

  • I pasted your solution in Jeff and it seems both queries use the same execution plan.

  • Agreed... basically the same... I've got an extra "Compute Scalar" 'cause of the formatting I did...

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

  • Thank you both for your help! Jeff, I will do that in the future.

    -Aaron

  • Thanks, Aaron...

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

Viewing 7 posts - 1 through 6 (of 6 total)

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