繁体   English   中英

SAS中的SQL:尝试通过左联接计算零值

[英]SQL in SAS: Trying to count zero values by left-join

尽管对我的问题进行了深入研究,但我仍无法解决我在SQL查询中遇到的问题。

我在客户数据库上有一个二维查询。 我想按客户和产品对所有购买金额进行排序。 由于并非所有客户都购买了每种产品,因此也必须显示每个客户零购买的产品。

我有两个表:具有所有单笔交易的大表purchases和第二个表products ,这是通过从purchases数据库中进行的简单查询生成的。

该表用于列出所有可能的产品。

我的查询如下所示:

proc sql; 
create table customers_products 
as select  a.customers as customers, b.products as product,sum(a.amount) as amount format=comma16.0, count(a.amount) as transactions format=comma16.0
from products as b 
left join work.transactions as a on b.product = a.product
group by a.customers , b.products 
order by a.products, amount desc; 
quit;  

但是,没有零计数。 任何帮助表示赞赏!

这能解决您的难题吗? 缺少样本数据和所需的输出,我无法对其进行测试。

proc sql; 
create table customers_products as
select a.customers as customers
      ,b.products as product
      ,sum(a.amount) as amount format=comma16.0
      ,case
        when sum(a.amount) is missing then 0
        else count(a.amount) 
       end as transactions format=comma16.0
from products as b 
left join work.transactions as a on b.product = a.product
group by a.customers , b.products 
order by a.products, amount desc; 
quit; 

编辑:我现在看到,在您的查询中您既有product又有products变量。 那是错字吗?

编辑2:我做了一些示例数据,并在上面运行您的查询

data products;
infile datalines;
input product $;
datalines;
a
b
c
d
;
run;
data transactions;
infile datalines;
input customers $ product $ amount;
datalines;
X a 3
X b 1
Y c 5
;
run;

proc sql; 
create table customers_products as
select a.customers as customers
            ,b.product as product
            ,sum(a.amount) as amount format=comma16.0
            ,count(a.amount) as transactions format=comma16.0
from products as b 
left join transactions as a on b.product = a.product
group by a.customers , b.product 
order by a.product, amount desc; 
quit;

这是您的确切查询,只是我以为您对prodcutsproduct各种引用是错误的,并且将它们全部更改为product 这将产生以下结果:

customers product amount transactions
-------------------------------------
          d       .      0
X         a       3      1
X         b       1      1
Y         c       5      1

如您所见,我得到了一条记录,其中记录了产品b零价值交易。 因此,我不确定我是否理解您的问题(再次强调,如果问题包括样本数据和所需的输出,为什么它真的很有帮助)。

可能有一种更简单的方法来执行此操作,但是这是我将如何解决您的问题

首先:这是一些购买示例。 每个客户有60%的机会购买产品1次或多次:

data purchases;
do customer=1 to 10;
    do product=1 to 10;
        if ranuni(1) > .4 then do;
            x = ceil(4*ranuni(1)); /*Buy 1 to 4 times*/
            do i=1 to x;
                amount = ranuni(1)*10;
                output;
            end;
        end;
    end;
end;
drop i x;
run;

接下来,我得到了不同的产品和客户,并且得到了每个客户/产品组的总数。

proc sql noprint;
create table products as
select distinct product from purchases;

create table customers as
select distinct customer from purchases;

create table totals as
select customer, product, sum(amount) as amt
from purchases
group by customer, product;
quit;

您的问题涉及到客户和产品上的笛卡尔积-即,您想要所有客户和产品。 所以我继续在这里做。

proc sql noprint;
create table customers_x_products as 
select a.customer, b.product
from customers as a, 
     products as b;
quit;

最后,我将customers_x_products表与totals表一起生成我认为您正在寻找的表:

proc sql noprint;
create table customers_products as
select b.customer,
       b.product,
       coalesce(a.amt,0) as amt
    from customers_x_products as b
      left join
         totals as a
      on a.product=b.product 
      and a.customer=b.customer
    order by b.customer, b.product;
quit;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM