多个 Sql 计数

发布于 2024-11-16 19:08:13 字数 1354 浏览 4 评论 0原文

我的数据库中有一个表(tbl_MatterItem),用于跟踪项目发送和接收的日期。如果尚未发送或接收,则值为 NULL。我有另一个要链接的表 (tbl_Matter),以获取该特定项目的 BillingLawyer。

BillingLawyer     ItemSent     ItemReceived
-------------     --------     ------------
   Alison         09/09/09        NULL
   Alison         10/10/10        NULL
   Alison         11/11/11       13/11/11
   Alison         12/12/12        NULL

我想为每位律师检索一行,其中包含他们的姓名、发送的项目数量和收到的项目数量。

BillingLawyer    Sent    Received
-------------    -----   --------
   Alison         4         1

以下是我到目前为止所得到的:

SELECT BillingLawyer, 
(SELECT COUNT(DISTINCT itemSent)FROM tbl_matteritem mit WHERE itemid=2 AND itemSent IS NOT NULL AND mit.ItemSent= mi.itemSent) AS [Sent], 
(SELECT COUNT(DISTINCT itemReceived)FROM tbl_matteritem mitm WHERE itemid=2 AND itemReceived IS NOT NULL AND mitm.itemreceived = mi.itemreceived)AS Received
FROM tbl_matteritem mi JOIN tbl_matter m ON mi.matterid = m.matterid
GROUP BY BillingLawyer, ItemSent, itemreceived
ORDER BY 1

但是我的结果显示了额外的记录以及似乎是位值的内容?:

BillingLawyer    Sent    Received
-------------    ----    --------
   Alison         0         0
   Alison         1         1
   Alison         1         0
   Alison         1         0
   Alison         1         0

我有什么想法出错了吗?

I have a table (tbl_MatterItem) in my DB that tracks the dates that items are sent and received. If it hasn't been sent or received, the value is NULL. I have another table (tbl_Matter) that I am linking to, to get the BillingLawyer for that particular item.

BillingLawyer     ItemSent     ItemReceived
-------------     --------     ------------
   Alison         09/09/09        NULL
   Alison         10/10/10        NULL
   Alison         11/11/11       13/11/11
   Alison         12/12/12        NULL

I would like to retrieve one row for each lawyer with their name, the number of items sent, and the number of items received.

BillingLawyer    Sent    Received
-------------    -----   --------
   Alison         4         1

Below is what I have so far:

SELECT BillingLawyer, 
(SELECT COUNT(DISTINCT itemSent)FROM tbl_matteritem mit WHERE itemid=2 AND itemSent IS NOT NULL AND mit.ItemSent= mi.itemSent) AS [Sent], 
(SELECT COUNT(DISTINCT itemReceived)FROM tbl_matteritem mitm WHERE itemid=2 AND itemReceived IS NOT NULL AND mitm.itemreceived = mi.itemreceived)AS Received
FROM tbl_matteritem mi JOIN tbl_matter m ON mi.matterid = m.matterid
GROUP BY BillingLawyer, ItemSent, itemreceived
ORDER BY 1

However my results show with an extra record and what appears to be bit values?:

BillingLawyer    Sent    Received
-------------    ----    --------
   Alison         0         0
   Alison         1         1
   Alison         1         0
   Alison         1         0
   Alison         1         0

Any ideas where I am going wrong?

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

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

发布评论

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

评论(4

梦太阳 2024-11-23 19:08:13

试试这个:

SELECT
    BillingLawyer,
    SUM(CASE WHEN ItemSent IS NOT NULL THEN 1 ELSE 0 END) AS Sent,
    SUM(CASE WHEN ItemReceived IS NOT NULL THEN 1 ELSE 0 END) AS Received
FROM
    tbl_matteritem mi
JOIN tbl_matter m ON
    mi.matterid = m.matterid
GROUP BY BillingLawyer
ORDER BY BillingLawyer

Try this:

SELECT
    BillingLawyer,
    SUM(CASE WHEN ItemSent IS NOT NULL THEN 1 ELSE 0 END) AS Sent,
    SUM(CASE WHEN ItemReceived IS NOT NULL THEN 1 ELSE 0 END) AS Received
FROM
    tbl_matteritem mi
JOIN tbl_matter m ON
    mi.matterid = m.matterid
GROUP BY BillingLawyer
ORDER BY BillingLawyer
情泪▽动烟 2024-11-23 19:08:13

您正在对 ItemSent 进行分组,因此 count(distinct ItemSent) 将返回 1 或 0。毕竟,将为每个不同的 ItemSent,然后 count() 将应用于该组。

如果您想对每个 BillingLawyer 的项目进行计数,请对 BillingLawyer 进行分组,而不是对 ItemSent 进行分组。

You're grouping on ItemSent, so count(distinct ItemSent) will return either 1 or 0. After all, a new group will be created for each different value of ItemSent, and the count() will then be applied to that group.

If you want to count items per BillingLawyer, group on BillingLawyer but not ItemSent.

昇り龍 2024-11-23 19:08:13

我认为首先删除 DISTINCT 因为它会计算每个日期。

SELECT BillingLawyer, 
(SELECT COUNT( itemSent)FROM tbl_matteritem mit WHERE itemid=2 AND itemSent IS      NOT NULL AND mit.ItemSent= mi.itemSent) AS [Sent], 
(SELECT COUNT( itemReceived)FROM tbl_matteritem mitm WHERE itemid=2 AND itemReceived IS NOT NULL AND mitm.itemreceived = mi.itemreceived)AS Received
FROM tbl_matteritem mi JOIN tbl_matter m ON mi.matterid = m.matterid
GROUP BY BillingLawyer, ItemSent, itemreceived
ORDER BY 1

Remove the DISTINCT first I think because it will count for each date.

SELECT BillingLawyer, 
(SELECT COUNT( itemSent)FROM tbl_matteritem mit WHERE itemid=2 AND itemSent IS      NOT NULL AND mit.ItemSent= mi.itemSent) AS [Sent], 
(SELECT COUNT( itemReceived)FROM tbl_matteritem mitm WHERE itemid=2 AND itemReceived IS NOT NULL AND mitm.itemreceived = mi.itemreceived)AS Received
FROM tbl_matteritem mi JOIN tbl_matter m ON mi.matterid = m.matterid
GROUP BY BillingLawyer, ItemSent, itemreceived
ORDER BY 1
旧情别恋 2024-11-23 19:08:13

请勿在 GROUP BY 中包含 ItemSent, itemreceived
仅使用GROUP BY BillingLawyer

Do not include ItemSent, itemreceived in GROUP BY.
Use only GROUP BY BillingLawyer

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文