多个 Sql 计数
我的数据库中有一个表(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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
试试这个:
Try this:
您正在对
ItemSent
进行分组,因此count(distinct ItemSent
) 将返回 1 或 0。毕竟,将为每个不同的ItemSent
,然后count()
将应用于该组。如果您想对每个
BillingLawyer
的项目进行计数,请对BillingLawyer
进行分组,而不是对ItemSent
进行分组。You're grouping on
ItemSent
, socount(distinct ItemSent
) will return either 1 or 0. After all, a new group will be created for each different value ofItemSent
, and thecount()
will then be applied to that group.If you want to count items per
BillingLawyer
, group onBillingLawyer
but notItemSent
.我认为首先删除
DISTINCT
因为它会计算每个日期。Remove the
DISTINCT
first I think because it will count for each date.请勿在
GROUP BY
中包含ItemSent, itemreceived
。仅使用
GROUP BY BillingLawyer
Do not include
ItemSent, itemreceived
inGROUP BY
.Use only
GROUP BY BillingLawyer