Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/matlab/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从未订购过某类产品的客户的SQL统计信息_Sql - Fatal编程技术网

从未订购过某类产品的客户的SQL统计信息

从未订购过某类产品的客户的SQL统计信息,sql,Sql,在SQL数据库中,我有一个表,其中包含客户ID列表。我也有一个外国ID的价格,数量和类型1或0订单表 我想知道客户列表中仅订购过类型0的客户的总价格、总数量和订单数量 我在想,我需要先得到一份只使用0类型的客户名单,然后再得到统计数据,但不知道如何轻松做到这一点 订单表: |OrderNo| CustomerCD| Type|Price|Quantity| 所有客户的客户列表表子集: |customer ID| 要寻找一个简单的查询,为从未使用过类型1的客户输出总体指标,我将使用窗口函数:

在SQL数据库中,我有一个表,其中包含客户ID列表。我也有一个外国ID的价格,数量和类型1或0订单表

我想知道客户列表中仅订购过类型0的客户的总价格、总数量和订单数量

我在想,我需要先得到一份只使用0类型的客户名单,然后再得到统计数据,但不知道如何轻松做到这一点

订单表:

|OrderNo| CustomerCD| Type|Price|Quantity|
所有客户的客户列表表子集:

|customer ID|

要寻找一个简单的查询,为从未使用过类型1的客户输出总体指标,我将使用窗口函数:

select customercd, sum(quantity),
       sum(case when num_not_type_0 = 0 then 1 else 0 end)
from (select o.*, sum(case when type <> 0 then 1 else 0 end) as num_not_type_0
      from orders o
     ) o
group by customercd;

请分享您编写的产生错误输出的代码。示例数据、所需结果和数据库标记会有所帮助。到目前为止,您尝试了什么?我使用了两个单独的查询,创建了一个独特客户表,然后将其用作过滤器。第一个查询只是创建了一个客户列表,该列表仅使用group by ID对类型0进行了排序,然后在类型为0时使用Count*=Countcase,然后使用1 end。第二个查询只是对该列表中的ID进行简单的计数/求和。
select sum(quantity), count(*), . . .
from orders o
where not exists (select 1 from orders o2 where o2.customercd = o.customercd and o2.type = 1);