访问 Distinct Count 和 Count on same table 的子查询

Access subquery for Distinct Count and Count on same table

您好,我浏览了论坛一段时间,在这里提出我的第一个问题。我有点手足无措,想知道我是否能得到一些帮助。我正在使用 Access,但尚未在网上找到该问题的良好答案。

我有一个名为 tblTransactions 的 table 用于 Access 2013 上的交易。它看起来像这样:

Transaction_ID Customer_No Prod_ID Lıcence_ID
1 111 1 1
2 111 1 2
3 222 1 2
4 111 2 1
5 222 2 1
6 222 2 2
7 333 1 1

tblProd 看起来像:

Prod_ID Prod_Name Prod_Price
1 Prod 1 30
2 Prod 2 50

tblLicence 看起来像:

Lıcence_ID Lıcence_Name Lıcence_Price
1 Lıcence 1 80
2 Lıcence 2 100

客户购买产品一次,可以获得该产品的多个许可。该产品只需支付一次,但适用于拥有的所有许可证。

我想为交易创建一个摘要列表。我无法在客户编号旁边打印出它有多少个不同的产品以及总共有多少个许可证。

我想要的输出应该是这样的:

Customer_No Count_Uniq_Prods Count_Licences Sum_Prods_Price Sum_Licences_Price
111 2 3 80 260
222 2 3 80 280
333 1 1 30 80

前 3 列我尝试了不同的方法。

  1. 当我尝试使用子查询时,客户编号和产品数量是正确的,但它也从许可证中删除了重复项。
SELECT C.Customer_No, T2.Count_Uniq_Prods, T2.Count_Licences" & _
        FROM" & _
            (SELECT T1.Customer_No, T1.Count_Uniq_Prods, Count(Lıcence_ID) As Count_Licences"
            FROM" & _
                (SELECT DISTINCT Customer_No, Lıcence_ID, Count(Prod_ID) As Count_Uniq_Prods
                FROM tblTransactions  GROUP BY Customer_No, Lıcence_ID ) AS T1
            GROUP BY T1.Customer_No, T1.Count_Uniq_Prods) AS T2
        INNER JOIN tblTransactions  AS C
        ON T2.Customer_No = C.Customer_No" & _
        GROUP BY C.Customer_No, T2.Count_Uniq_Prods, T2.Count_Licences;
  1. 当我尝试左连接操作时,我可以成功地分别获取产品和许可证的结果,但是当我想单独获取时table,结果不是我想要的。

适用于 Customer_No、Count_Uniq_Prods、Sum_Prods_Price:

SELECT T.Customer_No,
Count(T.Prod_ID), SUM(tblProd.Prod_Price) AS Sum_Prods_Price
FROM ((SELECT DISTINCT Customer_No, Prod_ID FROM tblTransactions ) AS T
LEFT JOIN tblProd ON tblProd.Prod_ID= T.Prod_ID)
GROUP BY T.Customer_No;

适用于 Customer_No、Count_Licences、Sum_Licences_Price:

SELECT T.Customer_No,
Count(T.Lıcence_ID), SUM(tblLicence.[Lıcence_Price]) AS Sum_Licences_Price
FROM ((SELECT Customer_No, Lıcence_ID FROM tblTransactions ) AS T
LEFT JOIN tblLicence ON tblLicence.Lıcence_ID = T.Lıcence_ID)
GROUP BY T.Customer_No

但是当我将一个作为另一个的子查询时,我无法在两个结果中都达到预期的结果。

希望我能解释清楚。在此先感谢您的帮助。

这应该适合你。

我的方法是解决您的问题并将其分解为构成要素。

This query retrieves the products in the format that you requested.

select customer_no, count(t.prod_id) as Count_Uniq_Prods, 
       sum(p.prod_price) as Sum_Prods_Price
      from  ( 
         select customer_no, prod_id
         from tblTransactions t
         group by customer_no, prod_id
      ) t
     inner join tblProd p on
       t.prod_id = p.prod_id
     group by customer_no

This query retrieves the licenses in the format that you requested.

select customer_no, count(t. license_id) as Count_Licenses, 
  sum(l.license_price) as Sum_Licenses_Price
from tblTransactions t
inner join tblLicense l on 
  t.license_id = l.license_id
  group by customer_no

Finally, we put them together and get the following:

select distinct  p.customer_no, Count_Uniq_Prods, 
         Count_Licenses,
         Sum_Prods_Price,
         Sum_Licenses_Price         
from (
     select customer_no, count(t.prod_id) as Count_Uniq_Prods, 
       sum(p.prod_price) as Sum_Prods_Price
      from  ( 
         select customer_no, prod_id
         from tblTransactions t
         group by customer_no, prod_id
      ) t
     inner join tblProd p on
       t.prod_id = p.prod_id
     group by customer_no
     
 ) p
inner join (
    select customer_no, count(t. license_id) as Count_Licenses, 
      sum(l.license_price) as Sum_Licenses_Price
    from tblTransactions t
    inner join tblLicense l on 
      t.license_id = l.license_id
      group by customer_no
    ) l
 on p.customer_no = l.customer_no