Counting Field Values based on another field value

  • How do I achieve the following summary

    =========================================================================

    Project_Type Project Number Customer Expected Date Actual Date

    A F-11-02348 George 8/1/2012 8/21/2012

    A F-11-01718 George 8/31/2012 10/4/2012

    A F-11-03542 George 8/8/2012 6/26/2012

    B M-11-07056 Tom 9/2/2012 8/15/2012

    B M-11-09207 Tom 9/29/2012 6/28/2012

    C D-12-22469 Dora 8/31/2012 8/8/2012

    C D-11-22049 Dora 8/18/2012 9/7/2012

    C D-11-22049 Dora 9/30/2012 9/27/2012

    C D-12-22469 Dora 10/1/2012 9/21/2012

    Project Types Summary

    Total Number of Type A Projects 3

    Total Number of Type B Projects 2

    Total Number of Type C Projects 2

    Total Number of Type D Projects 0

    Total Number of Type E Projects 0

    Total Number of Type F Projects 0

    Total Number of Type G Projects 0

    Total Number of Projects 7

    =====================================================

    My code is working well except when I only have one entry for a certain Project_Type, then it returns a 0 for the total number of that particular project_Type in the summary

  • Thats great, you posted sample data.

    Can you please take a few minutes create a script that put the data in a Table or create a crude cte just so we have something to work with.

    Please Read.

    http://qa.sqlservercentral.com/articles/Best+Practices/61537/[/url]

    Using your data as the example.

    ;WITH MyTable ([Project_Type], [Project Number], [Customer], [Expected Date], [Actual Date]) AS (

    SELECT 'A', 'F-11-02348', 'George', '8/1/2012', '8/21/2012' UNION ALL

    SELECT 'A', ' F-11-01718', 'George', '8/31/2012', '10/4/2012' UNION ALL

    SELECT 'A', 'F-11-03542', 'George', '8/8/2012', '6/26/2012' UNION ALL

    SELECT 'B', 'M-11-07056', 'Tom', '9/2/2012', '8/15/2012' UNION ALL

    SELECT 'B', 'M-11-09207', 'Tom', '9/29/2012', '6/28/2012' UNION ALL

    SELECT 'C', 'D-12-22469', 'Dora', '8/31/2012', '8/8/2012' UNION ALL

    SELECT 'C', 'D-11-22049', 'Dora', '8/18/2012', '9/7/2012' UNION ALL

    SELECT 'C', 'D-11-22049', 'Dora', '9/30/2012', '9/27/2012' UNION ALL

    SELECT 'C', 'D-12-22469', 'Dora', '10/1/2012', '9/21/2012')

    SELECT COUNT(1) AS ProjectQuantity, Project_Type

    FROM MyTable

    GROUP BY Project_Type

    What query are you running to get the results you requested?

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

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