SQL 2008 R2 - Trouble Aggregating Data From Common Table Expression

  • Greetings,

    I am trying to create a small data set for use in a charting program. However, I am not getting the results that I expected and I was wondering if some of you guys could tell me where I am going wrong?

    ********QUERY BEGIN*****************

    DECLARE @BeginDate datetime

    DECLARE @EndDate datetime

    SET @BeginDate = '2014-01-01'

    SET @EndDate = '2014-12-31';

    WITH CTE AS (

    SELECT bkg_nbr

    ,STATE_NBR

    ,RANK() OVER (PARTITION BY STATE_NBR ORDER BY bk_datetime DESC) AS Ranking

    FROM Recidivism

    WHERE (bk_datetime BETWEEN @BeginDate AND DATEADD(d, 1, @EndDate))

    AND (ISNUMERIC(STATE_NBR) = 1)

    AND STATE_NBR IN ('00060000','00140000','00210000' )

    )

    SELECT 'Non-Recidivists' AS [Category],COUNT( bkg_nbr) AS RecCount

    FROM CTE

    WHERE bkg_nbr NOT IN (SELECT bkg_nbr FROM CTE WHERE Ranking > 1 )

    UNION

    SELECT 'Recidivists' AS [Category], COUNT(DISTINCT bkg_nbr) AS RecCount

    FROM CTE

    WHERE Ranking > 1

    *********QUERY END**************

    The raw data from the CTE is:[/b]

    Bkg_nbr STATE_NBR Ranking

    20140000943200060000 1

    20140000131500140000 1

    20140000682200140000 2

    20140000504100210000 1

    The result of the query above was:

    Category RecCount

    Non-Recidivists3

    Recidivists 1

    The outcome I was looking for was:

    Category RecCount

    Non-Recidivists2

    Recidivists 1

    What I am trying to do is count persons in buckets "non-recidivists" and "recidevists" based on how many bkg_nbr they have per STATE_NBR. If they have more than 1 bkg_nbr per STATE_NBR then put them in the "recdivists" bucket. If they only have a 1 to 1 then put them in the "non-recidivists" bucket.

    Sincerely,

    Jason 🙂

  • Could it be as simple as changing the where predicate in the first query?

    WHERE state_nbr NOT IN (SELECT state_nbr FROM CTE WHERE Ranking > 1 )

    _______________________________________________________________

    Need help? Help us help you.

    Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

    Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.

    Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
    Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
    Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
    Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/

  • Dohhh.....

    Classic example of looking at something too long and coding while I was tired. Thank you for the second set of eyes. :hehe:

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

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