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

  • Home
  • SEARCH
  • 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 5938985
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T15:45:45+00:00 2026-05-22T15:45:45+00:00

need some your help with a query. I have a table Managers (ManagerId, ManagerName)

  • 0

need some your help with a query.

I have a table Managers (ManagerId, ManagerName)

I have a table Statuses (StatusId, StatusName)
(There’s about 10 statuses in that table)

I have a table Clients (ClientId, ClientName, ManagerId, StatusId, WhenAdded)
(WhenAdded is a datetime type)

It’s obvious that field 'ManagerId' refers to a table 'Managers' and field 'StatusId' refers to a table 'Statuses'.

User wants to get some statistics about Managers over a period of time (from startDate to endDate using field 'WhenAdded') in the following table.

Columns:

ManagerName, NumberOfClients, NumberOfClientsWithStatus1, NumberOfClientsWithStatus2, NumberOfClientsWithStatus3 and so on.

Number of columns with name NumberOfClientsWithStatusI where i is a number of statuses equal to number of rows in table 'Statuses'.

How can I do that?

t-sql, sql server 2008 r2 express edition.

  • 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-05-22T15:45:45+00:00Added an answer on May 22, 2026 at 3:45 pm
    SELECT
        ManagerName,
        COUNT(*) AS NumberOfClients,
        COUNT(CASE WHEN S.StatusId = 1 THEN 1 ELSE NULL END) AS NumberOfClientsWithStatus1,
        COUNT(CASE WHEN S.StatusId = 2 THEN 1 ELSE NULL END) AS NumberOfClientsWithStatus2,
        COUNT(CASE WHEN S.StatusId = 3 THEN 1 ELSE NULL END) AS NumberOfClientsWithStatus3,
        ...
    FROM
       Clients C
       JOIN
       Managers M ON C.ManagerId = M.ManagerId
       JOIN
       Statuses S ON C.StatusId = S.StatusId
    WHERE
        M.WhenAdded BETWEEN @startDate AND @endDate
    GROUP BY
        M.ManagerName
    

    Note: there is no clean way to add arbritrary numbers of status columns in SQL (not just SQL Server) because its a fixed column output. You’d have to change the query for status, unless you deal with this in the client

    Edit, after comment

    SELECT
        ManagerName,
        COUNT(*) AS NumberOfClients,
        COUNT(CASE WHEN S.StatusId = 1 THEN 1 ELSE NULL END) AS NumberOfClientsWithStatus1,
        COUNT(CASE WHEN S.StatusId = 2 THEN 1 ELSE NULL END) AS NumberOfClientsWithStatus2,
        COUNT(CASE WHEN S.StatusId = 3 THEN 1 ELSE NULL END) AS NumberOfClientsWithStatus3,
        ...
    FROM
       Managers M ON C.ManagerId = M.ManagerId
       LEFT JOIN
       Clients C
       LEFT JOIN
       Statuses S ON C.StatusId = S.StatusId
    WHERE
        M.WhenAdded BETWEEN @startDate AND @endDate
    GROUP BY
        M.ManagerName
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've some questions .. and I really need your help. I have an application.
I need some help from the shell-script gurus out there. I have a .txt
I need some resources that talk about how to design your software to be
Do you need to use some kind of provider? Can you setup your own
I need some basic CMS functionality with rich text editing. On stack overflow there
I need some advice as to how I easily can separate test runs for
I need some info on how to use margins and how exactly padding works.
I need some software to explore and modify some SQLite databases. Does anything similar
We need some input on what is a good design pattern on using AJAX
I need some sort of interactive chart control for my .NET-based web app. I

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.