内联视图中的聚合函数

时间:2016-03-30 18:42:10

标签: sql pervasive

我正在尝试为Pervasive数据库编写一些SQL,但我无法使查询正常工作。

让我举一个简单的例子。想象一下,我有下表:

购买:

OrderNumber     CustomerName
 55             Amy
 56             Dan
 57             Bob
 58             Dan
 59             Dan
 60             Bob
 61             Amy
 62             Cindy
 63             Dan

现在我可以使用查询select count(OrderNumber) as "Number of orders palced", CustomerName from PURCHASES group by CustomerName order by count(OrderNumber) desc来获取内联视图的结果:

Number of orders placed                CustomerName
          4                            Dan
          2                            Amy
          2                            Bob
          1                            Cindy

但我不想在这里停下来我想知道每个客户中有多少客户存在"已下订单数量"但我无法正确查询。

我想将其用作子查询,如下所示:

select x."Number of orders placed",count(x.CustomerName) as
       "Number of customers that have purchased this many orders" from 
(
    select count(OrderNumber) as "Number of orders placed", CustomerName from PURCHASES 
    group by CustomerName 
) x
group by x."Number of orders placed" 

我的查询失败了我认为因为列标签不是引用子查询的正确方法。 我想得到的结果应该是这样的:

Number of orders placed           Number of customers...
      4                            1
      2                            2
      1                            1

感谢帮助和解释

1 个答案:

答案 0 :(得分:1)

除了内联视图中的order by之外,您的查询中没有任何错误。

select x."Number of orders placed",count(x.CustomerName) as
       "Number of customers that have purchased this many orders" 
from 
(select count(OrderNumber) as "Number of orders placed", CustomerName 
 from PURCHASES 
 group by CustomerName
) x
group by x."Number of orders placed"