我有来自两个电子表格的数据,我需要比较安装和购买的软件数量。公共字段是两个电子表格中的前缀和客户端,但没有唯一标识符。
我创建了3个表。客户端(Client_ID)中的主键和已安装和已购买的外键。 #Prinmary * Foreign
表: -
Client - Client.Client_ID#, Client.Client
Installed - Installed.Client_ID*, Installed.Prefix, Installed.Publisher, Installed.Version,Installed.Amount
Purchased -Purchase.Client_ID*, Purchase.Prefix, Purchase.Status, Purchase.Qty
我遇到的部分问题是大多数记录/行都是列表多次。例如,产品Y记录为20行/行,而不是Y数量20.
我想根据前缀AND client_ID计算计数(Installed.Amount)。
到目前为止,我只能计算前缀的计数而不是client_ID。是否可以组合列,以便查询将产生前缀和其他列的数量。
查询: -
SELECT Installed.Prefix, Count(Installed.Amount) AS PrefixQuantity
FROM Installed
GROUP BY Installed.Prefix;
答案 0 :(得分:0)
SELECT *
FROM
client c
INNER JOIN purchase p
ON c.client_id = p.client_id
INNER JOIN (SELECT installed.prefix,
installed.client_id,
COUNT(installed.amount) prefixquantity
FROM installed
GROUP BY installed.prefix,
installed.client_id) counts
ON c.client_id = counts.client_id
答案 1 :(得分:0)
SELECT Installed.Client_ID, Installed.Prefix, COUNT(Installed.Amount)
AS PrefixQuantity FROM Installed GROUP BY Installed.Client_ID, Installed.Prefix
SELECT Installed.Client_ID, Installed.Prefix, SUM(Installed.Amount)
AS PrefixQuantity FROM Installed GROUP BY Installed.Client_ID, Installed.Prefix
即使Amount列的值大于1,也会得到正确的结果。