如何验证B列中的值组与A列中的值进行比较

时间:2013-04-26 08:13:44

标签: sql sas proc-sql

我需要验证是否为表应用了特定规则。

规则=链接到1个客户(CUSTOMER_NR)的所有选项(列OPTION_REF)应该相同

(注:客户可以链接1到800个选项,因此只有链接表自身不适用)

我需要检测并非所有链接选项都相同的客户。

CUSTOMER_NR  CONTRACT_NR  OPTION_REF
-------------------------------------
CUSTOMER1    CONTRACT1    OPTION A
CUSTOMER1    CONTRACT2    OPTION A
CUSTOMER1    CONTRACT3    OPTION A
CUSTOMER2    CONTRACT1    OPTION F
CUSTOMER2    CONTRACT2    OPTION F
CUSTOMER3    CONTRACT1    OPTION B
CUSTOMER3    CONTRACT2    OPTION T
CUSTOMER3    CONTRACT3    OPTION B

在上面的示例中,我需要检索“CUSTOMER3”作为查询的结果,因为2个不同的选项(OPTION B& OPTION T)链接到CUSTOMER3

有人可以帮忙吗?

提前多多感谢!

3 个答案:

答案 0 :(得分:2)

试试这个:

declare @data as table (customer varchar(50), cont varchar(50), opt varchar(50))
 insert into @data values
 ('CUSTOMER1', 'CONTRACT1', 'OPTION A'),
('CUSTOMER1', 'CONTRACT2', 'OPTION A'),
('CUSTOMER1', 'CONTRACT3', 'OPTION A'),
('CUSTOMER2', 'CONTRACT1', 'OPTION F'),
('CUSTOMER2', 'CONTRACT2', 'OPTION F'),
('CUSTOMER3', 'CONTRACT1', 'OPTION B'),
('CUSTOMER3', 'CONTRACT2', 'OPTION T'),
('CUSTOMER3', 'CONTRACT3', 'OPTION B')


select agregatedData.customer
from(
    select customer,opt
    from @data
    group by customer,opt
) as agregatedData
group by agregatedData.customer
having COUNT(0) > 1

答案 1 :(得分:2)

SAS解决方案:

proc sort data=have out=sorted nodupkey;
by customer_nr option_ref;
run;

data want;
set sorted;
by customer_nr option_ref;
if not (first.customer_nr and last.customer_nr);
run;

也可以使用PROC FREQ或其他任何东西进行聚合,但排序也很简单,因为你不关心每个选项的计数,除非它是一个非常庞大的数据集(在这种情况下,PROC FREQ / MEANS / whatnot可能更快)。

如果你实际上只想要返回CUSTOMER3并且甚至不关心这两个不同的选项,那么它就更容易 - 没有排序,假设它已经按照上面的CUSTOMER_NR排序。

data want;
set have;
by customer_nr option_ref notsorted;
if first.option_ref and not first.customer_nr;
run;

这不会返回每条记录(特别是它不会返回第一个选项)但是每个差异至少会返回一条记录(可能会返回很多记录)。

答案 2 :(得分:0)

SAS SQL版:

data mydata;
  input customer $
        contract $
        option   $20.
        ;
datalines;
CUSTOMER1 CONTRACT1 OPTION A
CUSTOMER1 CONTRACT2 OPTION A
CUSTOMER1 CONTRACT3 OPTION A
CUSTOMER2 CONTRACT1 OPTION F
CUSTOMER2 CONTRACT2 OPTION F
CUSTOMER3 CONTRACT1 OPTION B
CUSTOMER3 CONTRACT2 OPTION T
CUSTOMER3 CONTRACT3 OPTION B
;
run;


**
** SUMMARY OF INVALID CUSTOMERS
*;
proc sql noprint;
  create table validation_summary as 
  select customer,
         count(distinct option) as number_unique_options
  from mydata
  group by 1
  having count(distinct option) > 1
  ;
quit;

**
** DETAILS OF INVALID CUSTOMERS
*;
proc sql noprint;
  create table validation_detail as 
  select distinct a.customer, a.option
  from mydata a
  where a.customer in (select b.customer 
                       from mydata b 
                       group by 1 
                       having count(distinct option) > 1
                      )
  ;
quit;

请注意,这将返回与无效配置相关的所有选项,包括第一个。在您的要求中,您似乎想要忽略第一个。如果需要,可以通过后续步骤轻松完成。