Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8801463
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T00:50:23+00:00 2026-06-14T00:50:23+00:00

A Case in CRM has a field called Status with four options. I’m trying

  • 0

A “Case” in CRM has a field called “Status” with four options.

I’m trying to
build a report in CRM that fills a table with every week of the year (each row is a different week), and then counts the number of cases that have each Status option (the columns would be each of the Status options).

The table would look like this

         Status 1    Status 2    Status 3
Week 1       3         55          4
Week 2       5         23          5
Week 3       14        11          33

So far I have the following:

SELECT 
    SUM(case WHEN status = 1 then 1 else 0 end) Status1,
    SUM(case WHEN status = 2 then 1 else 0 end) Status2,
    SUM(case WHEN status = 3 then 1 else 0 end) Status3,
    SUM(case WHEN status = 4 then 1 else 0 end) Status4,
    SUM(case WHEN status = 5 then 1 else 0 end) Status5
FROM [DB].[dbo].[Contact]

Which gives me the following:

Status 1   Status 2   Status 3  
   2         43          53

Now I need to somehow split this into 52 rows for the past year and filter these results by date (columns in the Contact table). I’m a bit new to SQL queries and CRM – any help here would be much appreciated.

Here is a SQLFiddle with my progress and sample data: http://sqlfiddle.com/#!2/85b19/1

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T00:50:24+00:00Added an answer on June 14, 2026 at 12:50 am

    So, let’s break this down:

    You want to make a report that shows, for each contact, a breakdown, week by week, of the number of cases registered to that contact, which is divided into three columns, one for each StateCode.

    If this is the case, then you would need to have 52 date records (or so) for each contact. For calendar like requests, it’s always good to have a separate calendar table that lets you query from it. Dan Guzman has a blog entry that creates a useful calendar table which I’ll use in the query.

    WITH WeekNumbers AS
    (
        SELECT
            FirstDateOfWeek,
             -- order by first date of week, grouping calendar year to produce week numbers
            WeekNumber = row_number() OVER (PARTITION BY CalendarYear ORDER BY FirstDateOfWeek)
        FROM
            master.dbo.Calendar -- created from script
        GROUP BY
            FirstDateOfWeek,
            CalendarYear
    ), Calendar AS
    (
        SELECT
            WeekNumber =
            (
                SELECT
                    WeekNumber
                FROM
                    WeekNumbers WN
                WHERE
                    C.FirstDateOfWeek = WN.FirstDateOfWeek
            ),
            *
        FROM
            master.dbo.Calendar C
        WHERE
            CalendarDate BETWEEN '1/1/2012' AND getutcdate()
    )
    
    SELECT
        C.FullName,
        ----include the below if the data is necessary
        --Cl.WeekNumber,
        --Cl.CalendarYear,
        --Cl.FirstDateOfWeek,
        --Cl.LastDateOfWeek,
        'Week: ' + CAST(Cl.WeekNumber AS VARCHAR(20))
        + ', Year: ' + CAST(Cl.CalendarYear AS VARCHAR(20)) WeekNumber
    FROM
        CRM.dbo.Contact C
        -- use a cartesian join to produce a table list
        CROSS JOIN
            (
                SELECT
                    DISTINCT WeekNumber,
                    CalendarYear,
                    FirstDateOfWeek,
                    LastDateOfWeek
                FROM
                    Calendar
            ) Cl
    ORDER BY
        C.FullName,
        Cl.WeekNumber
    

    This is different from the solution Ben linked to because Marc’s query only returns weeks where there is a matching value, whereas you may or may not want to see even the weeks where there is no activity.

    Once you have your core tables of contacts split out week by week as in the above (or altered for your specific time period), you can simply add a subquery for each StateCode to see the breakdown in columns as in the final query below.

    WITH WeekNumbers AS
    (
        SELECT
            FirstDateOfWeek,
            WeekNumber = row_number() OVER (PARTITION BY CalendarYear ORDER BY FirstDateOfWeek)
        FROM
            master.dbo.Calendar
        GROUP BY
            FirstDateOfWeek,
            CalendarYear
    ), Calendar AS
    (
        SELECT
            WeekNumber =
            (
                SELECT
                    WeekNumber
                FROM
                    WeekNumbers WN
                WHERE
                    C.FirstDateOfWeek = WN.FirstDateOfWeek
            ),
            *
        FROM
            master.dbo.Calendar C
        WHERE
            CalendarDate BETWEEN '1/1/2012' AND getutcdate()
    )
    
    SELECT
        C.FullName,
        --Cl.WeekNumber,
        --Cl.CalendarYear,
        --Cl.FirstDateOfWeek,
        --Cl.LastDateOfWeek,
        'Week: ' + CAST(Cl.WeekNumber AS VARCHAR(20)) +', Year: ' + CAST(Cl.CalendarYear AS VARCHAR(20)) WeekNumber,
        (
            SELECT
                count(*)
            FROM
                CRM.dbo.Incident I
                INNER JOIN CRM.dbo.StringMap SM ON
                    I.StateCode = SM.AttributeValue
                INNER JOIN 
                    (
                        SELECT
                            DISTINCT ME.Name,
                            ME.ObjectTypeCode
                        FROM
                            CRM.MetadataSchema.Entity ME
                    ) E ON
                    SM.ObjectTypeCode = E.ObjectTypeCode
            WHERE
                I.ModifiedOn >= Cl.FirstDateOfWeek 
                AND I.ModifiedOn < dateadd(day, 1, Cl.LastDateOfWeek)
                AND E.Name = 'incident'
                AND SM.AttributeName = 'statecode'
                AND SM.LangId = 1033
                AND I.CustomerId = C.ContactId
                AND SM.Value = 'Active'
        ) ActiveCases,
        (
            SELECT
                count(*)
            FROM
                CRM.dbo.Incident I
                INNER JOIN CRM.dbo.StringMap SM ON
                    I.StateCode = SM.AttributeValue
                INNER JOIN 
                    (
                        SELECT
                            DISTINCT ME.Name,
                            ME.ObjectTypeCode
                        FROM
                            CRM.MetadataSchema.Entity ME
                    ) E ON
                    SM.ObjectTypeCode = E.ObjectTypeCode
            WHERE
                I.ModifiedOn >= Cl.FirstDateOfWeek 
                AND I.ModifiedOn < dateadd(day, 1, Cl.LastDateOfWeek)
                AND E.Name = 'incident'
                AND SM.AttributeName = 'statecode'
                AND SM.LangId = 1033
                AND I.CustomerId = C.ContactId
                AND SM.Value = 'Resolved'
        ) ResolvedCases,
        (
            SELECT
                count(*)
            FROM
                CRM.dbo.Incident I
                INNER JOIN CRM.dbo.StringMap SM ON
                    I.StateCode = SM.AttributeValue
                INNER JOIN 
                    (
                        SELECT
                            DISTINCT ME.Name,
                            ME.ObjectTypeCode
                        FROM
                            CRM.MetadataSchema.Entity ME
                    ) E ON
                    SM.ObjectTypeCode = E.ObjectTypeCode
            WHERE
                I.ModifiedOn >= Cl.FirstDateOfWeek 
                AND I.ModifiedOn < dateadd(day, 1, Cl.LastDateOfWeek)
                AND E.Name = 'incident'
                AND SM.AttributeName = 'statecode'
                AND SM.LangId = 1033
                AND I.CustomerId = C.ContactId
                AND SM.Value = 'Canceled'
        ) CancelledCases
    FROM
        CRM.dbo.Contact C
        CROSS JOIN
            (
                SELECT
                    DISTINCT WeekNumber,
                    CalendarYear,
                    FirstDateOfWeek,
                    LastDateOfWeek
                FROM
                    Calendar
            ) Cl
    ORDER BY
        C.FullName,
        Cl.WeekNumber
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Case i'm trying to write a GWTTestCase for a certain class, used as a
Simple case of trying to prove I can convert NSString to NSDate . Create
I'm working with a page that has multiple <form> tags in it. One of
Case : Again trying to capture packets through my NIC, I have developed 2
I have 2 entities (for this example) in CRM 2011 - Account and Case.
I am trying to get to grips with MS Dynamics CRM 2011. I have
Given that the CRM 2011 linq provider performs paging automatically behind the scenes. Is
I'm trying to implement Microsoft's social networking solution accelerator on my Dynamics CRM deployment.
Hy everyone, how are you?. Well. The case is that I need to develop
Business case: I have a ASMX web service that copies/deletes/downloads files from some remote

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.