比较表中的列

时间:2014-12-18 20:36:28

标签: sql oracle

在下面的场景中,DEAL_ID和CUST_NUM的唯一组合可以有不同的CUST_ID ..

通过以下查询,我可以手动查看..

SELECT DEAL_ID,CUST_NUM,CUST_ID,
ROW_NUMBER() OVER(PARTITION BY DEAL_ID ORDER BY DEAL_ID) RID FROM TABLE_A;

场景1:使用相同的CUST_ID

DEAL_ID   CUST_NUM  CUST_ID    RID 
10002804    609793    0229      1   
10002804    609793    0229      2   
10002804    609793    0229      3   
10002804    609793    0229      4   
10002804    609793    0229      5   

场景2:使用不同的CUST_ID

DEAL_ID   CUST_NUM  CUST_ID    RID
10002804    609793     6221     1
10002804    609793     0229     2
10002804    609793     0229     3
10002804    609793     6221     4
10002804    609793     0229     5

我想在RID的帮助下比较DEAL_ID和CUST_NUM组合的CUST_ID列值。 我怎么能这样做?

我希望看到结果集看起来像..

DEAL_ID   CUST_NUM  CUST_ID  RID   COMPARE
10002804    609793    0229      1   SAME
10002804    609793    0229      2   SAME
10002804    609793    0229      3   SAME
10002804    609793    0229      4   SAME
10002804    609793    0229      5   SAME


DEAL_ID   CUST_NUM  CUST_ID  RID    COMPARE 
10002804    609793     6221     1   NOT SAME
10002804    609793     0229     2   NOT SAME
10002804    609793     0229     3   NOT SAME
10002804    609793     6221     4   NOT SAME
10002804    609793     0229     5   NOT SAME 

1 个答案:

答案 0 :(得分:2)

SQL Fiddle

Oracle 11g R2架构设置

CREATE TABLE TAB
    ("DEAL_ID" int, "CUST_NUM" int, "CUST_ID" int, "RID" int)
;

INSERT ALL 
    INTO TAB ("DEAL_ID", "CUST_NUM", "CUST_ID", "RID")
         VALUES (10002804, 609793, 6221, 1)
    INTO TAB ("DEAL_ID", "CUST_NUM", "CUST_ID", "RID")
         VALUES (10002804, 609793, 0229, 2)
    INTO TAB ("DEAL_ID", "CUST_NUM", "CUST_ID", "RID")
         VALUES (10002804, 609793, 0229, 3)
    INTO TAB ("DEAL_ID", "CUST_NUM", "CUST_ID", "RID")
         VALUES (10002804, 609793, 6221, 4)
    INTO TAB ("DEAL_ID", "CUST_NUM", "CUST_ID", "RID")
         VALUES (10002804, 609793, 0229, 5)
    INTO TAB ("DEAL_ID", "CUST_NUM", "CUST_ID", "RID")
         VALUES (100028041, 609793, 0229, 1)
    INTO TAB ("DEAL_ID", "CUST_NUM", "CUST_ID", "RID")
         VALUES (100028041, 609793, 0229, 2)
    INTO TAB ("DEAL_ID", "CUST_NUM", "CUST_ID", "RID")
         VALUES (100028041, 609793, 0229, 3)
    INTO TAB ("DEAL_ID", "CUST_NUM", "CUST_ID", "RID")
         VALUES (100028041, 609793, 0229, 4)
    INTO TAB ("DEAL_ID", "CUST_NUM", "CUST_ID", "RID")
         VALUES (100028041, 609793, 0229, 5)
SELECT * FROM dual
;

查询1

select deal_id, cust_num, cust_id,
decode(
  count(distinct cust_id) 
  over (partition by deal_id, cust_num), 
  1, 'SAME', 
  'NOT SAME') same
from tab

<强> Results

|   DEAL_ID | CUST_NUM | CUST_ID |     SAME |
|-----------|----------|---------|----------|
|  10002804 |   609793 |     229 | NOT SAME |
|  10002804 |   609793 |     229 | NOT SAME |
|  10002804 |   609793 |     229 | NOT SAME |
|  10002804 |   609793 |    6221 | NOT SAME |
|  10002804 |   609793 |    6221 | NOT SAME |
| 100028041 |   609793 |     229 |     SAME |
| 100028041 |   609793 |     229 |     SAME |
| 100028041 |   609793 |     229 |     SAME |
| 100028041 |   609793 |     229 |     SAME |
| 100028041 |   609793 |     229 |     SAME |