具有group by + count的列使用left join在SQL或Access中进行咨询

时间:2017-05-25 17:56:52

标签: sql ms-access

首先,我展示我的表格和查询:

Tbl_type: id, description (1,Type A)-(2,Type B)-(3,Type C)

Tbl_operation: id, client, type, import (1,1,1,100.00)-(2,1,1,75.00)-(3,1,1,44.00)-(4,1,2,90.00)-(5,1,2,11.00)

当我执行此查询时:

SELECT Operation.Client, Type.description AS Type, Count(Operation.Id) AS Nr
FROM Tipo LEFT JOIN
     Operation
     ON Type.Id = Operation.type
GROUP BY Type.description, Operation.Client;

我得到了输出:

Client,Type,Nr (1,Type A,3)-(1,Type B,2)

但是,我想得到这个:

Client,Type,Nr (1,Type A ,3)-(1,Type B,2)-(1,Type C,0)

此外,我还有第二个客户(id = 2)

Tbl_operation:id, client, type, import (1,1,1,100.00)-(2,1,1,75.00)-(3,1,1,44.00)-(4,1,2,90.00)-(5,1,2,11.00)-(6,2,3,66.00)

然后,咨询也应该这样做:

Client,Type,Nr (1,Type A,3)-(1,Type B,2)-(1,Type C,0)-(2,Type A,0)-(2,Type B,0)-
(2,Type C,1)

这可能吗?或者我必须做一个子选择?我试图使用完全外连接,但它不起作用,因为它只显示已注册的行。

0 个答案:

没有答案