从另一个查询SQL中选择信息

时间:2014-12-04 14:27:41

标签: sql subquery

我正在尝试使用SQL从已运行的查询中选择信息。我没有权利创建一个我知道会解决我的问题的视图。我运行了以下(混淆)查询,这会导致一些错误:

SELECT
  distinct(countValue),
  count(countValue)
FROM
(
  SELECT customer_identifier, count(distinct(2nd_customer_identifier)) AS countValue FROM table GROUP BY customer_identifier;
)
GROUP BY
  distinct(countValue)

子查询(下方)试图计算每位客户的唯一收款人数:

SELECT 
    customer_identifier, 
    count(distinct(2nd_customer_identifier)) AS countValue 
FROM table
GROUP BY aid

并且使用它的主要查询是尝试从上表中获取计数值并计算每个值出现的次数。

非常感谢任何帮助。

1 个答案:

答案 0 :(得分:1)

您可以用以下方式从逻辑上替换它吗?这将告诉您有多少付款人的情况。

SELECT 
    CountValue, 
    count(countValue) TotalRecords
FROM 
  ( 
    SELECT 
        customer_identifier, 
        count(distinct([2nd_customer_identifier])) AS countValue 
    FROM table 
    GROUP BY customer_identifier
  ) a
GROUP BY countValue ;

输出会告诉你一些事情:

  

共有25个付款人帐户

     

共有17个双付款账户

     

有9个三付款帐户

     

如果这不是您想要的,请编辑您的问题以描述您所追求的输出。