帮助解决困难的“分组依据”条款

发布于 2024-11-09 14:49:20 字数 834 浏览 0 评论 0原文

需要您的帮助来进行查询。

我有一个表 Managers (ManagerId, ManagerName)

我有一个表 Statuses (StatusId, StatusName)
(该表中有大约 10 个状态)

我有一个表 Clients (ClientId, ClientName, ManagerId, StatusId, WhenAdded)
WhenAdded 是日期时间类型)

很明显,字段 'ManagerId' 引用了表 'Managers' 并且字段'StatusId'引用表'Statuses'

用户希望获取下表中一段时间​​内(从 startDate 到 endDate,使用字段“WhenAdded”)有关经理的一些统计信息。

列:

ManagerName、NumberOfClients、NumberOfClientsWithStatus1、NumberOfClientsWithStatus2、NumberOfClientsWithStatus3 等。

名称为 NumberOfClientsWithStatusI 的列数,其中 i 是等于表 'Statuses' 中的行数的状态数。

我怎样才能做到这一点?

t-sql,sql server 2008 r2 快捷版。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

雄赳赳气昂昂 2024-11-16 14:49:20
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

注意:没有干净的方法可以在 SQL(不仅仅是 SQL Server)中添加任意数量的状态列,因为它是固定列输出。您必须更改状态查询,除非您

在评论后在客户端编辑中处理此问题

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
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
甜妞爱困 2024-11-16 14:49:20

如果您知道 statuses 表将始终包含有限数量的状态,您可以执行以下操作:

SELECT M.ManagerName,
       COUNT(C.ClientId) NumberOfClients,
       SUM(CASE WHEN S.StatusId= 1 THEN 1 ELSE 0 END) NumberOfClientsWithStatus1,
       SUM(CASE WHEN S.StatusId= 2 THEN 1 ELSE 0 END) NumberOfClientsWithStatus2,
       ...
  FROM Clients C
  JOIN Managers M on M.ManagerId = C.ManagerId
  JOIN Statuses S on S.StatusId = C.StatusId
 WHERE C.WhenAdded BETWEEN startDate AND endDate 
 GROUP BY ManagerName

If you know that statuses table will always contain a limited number of statuses, you can do this:

SELECT M.ManagerName,
       COUNT(C.ClientId) NumberOfClients,
       SUM(CASE WHEN S.StatusId= 1 THEN 1 ELSE 0 END) NumberOfClientsWithStatus1,
       SUM(CASE WHEN S.StatusId= 2 THEN 1 ELSE 0 END) NumberOfClientsWithStatus2,
       ...
  FROM Clients C
  JOIN Managers M on M.ManagerId = C.ManagerId
  JOIN Statuses S on S.StatusId = C.StatusId
 WHERE C.WhenAdded BETWEEN startDate AND endDate 
 GROUP BY ManagerName
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文