Results 1 to 5 of 5
  1. #1
    Join Date
    May 2006
    Posts
    16

    Unanswered: Percentaje of Clients Number by Region

    To calculate the percentage of customers by region, from the following tables:
    Client (Client_id, Region_Cd, ...)
    Region (Region_Cd, Region_Nm, ...) where Region_Cd is the region code and Region_Nm is the region name ,

    the next result is required:
    Region_Nm, Region_Percent (1 row for each region where Region_Percent = Region Clients Count * 100 / Clients Count)

    Thanks in advance,
    ALF.
    Last edited by arlf; 10-13-10 at 08:31.

  2. #2
    Join Date
    Feb 2004
    Location
    In front of the computer
    Posts
    15,579
    Provided Answers: 54
    Use a tool like SQL Server Reporting Services to produce the pie chart. Using SQL Server to do this task is very poor design.

    -PatP
    In theory, theory and practice are identical. In practice, theory and practice are unrelated.

  3. #3
    Join Date
    May 2006
    Posts
    16
    My concern is the query. The chart will be created with Reporting Services(rendering). ALF

  4. #4
    Join Date
    Feb 2004
    Location
    In front of the computer
    Posts
    15,579
    Provided Answers: 54
    I'm confused. Reporting Services does that calculation for you. Just feed it a result set with regions and client counts, SQL Server Reporting Services will then build the pie chart for you.

    -PatP
    In theory, theory and practice are identical. In practice, theory and practice are unrelated.

  5. #5
    Join Date
    Nov 2004
    Posts
    1,427
    Provided Answers: 4
    Try this:
    Code:
    CREATE TABLE #Client (
        Client_id    INT    NOT NULL, 
        Region_Cd    CHAR(4) NOT NULL
    )
    GO
    
    CREATE TABLE #Region (
        Region_Cd    CHAR(4)        NOT NULL, 
        Region_Nm    VARCHAR(20)    NOT NULL
    )
    GO
    
    INSERT INTO #Region (Region_Cd, Region_Nm) VALUES
    ('FRA', 'France'), 
    ('GER', 'Germany'), 
    ('MBY', 'My BackYard')
    
    INSERT INTO #Client (Client_id, Region_Cd) VALUES
    (1, 'FRA'), (2, 'FRA'), (3, 'FRA'), (4, 'FRA'), (5, 'MBY')
    
    SELECT Region_Nm,
        (SELECT (COUNT(*) * 100.0) / TCC.TotalClientCount
        FROM #Client
        WHERE #Region.Region_Cd = #Client.Region_Cd
        ) AS Region_Percent
    FROM #Region,
        (SELECT COUNT(*) as TotalClientCount
        FROM #Client) AS TCC
    With kind regards . . . . . SQL Server 2000/2005/2012
    Wim

    Grabel's Law: 2 is not equal to 3 -- not even for very large values of 2.
    Pat Phelan's Law: 2 very definitely CAN equal 3 -- in at least two programming languages

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •