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?
Try this: