每个唯一客户ID /会员ID一行

时间:2016-02-01 00:42:15

标签: sql plsql oracle11g

我正在使用oracle 11g。有人可以告诉我,对于输出中的每个唯一客户ID /会员ID,我只能获得一行。

从oracle表输入数据:

  • customerID会员ID地址州电话类型

    42084 100 123 walton il xx business

    42085 101 124 walton ak xx personal

    42084 100 83 nw st ny xx business

    42086 102 84 sw ny xx ext

    42084 100 123 walton il xx business

    42086 100 123 walton il xx business

    82084 100 123 walton il xx business

    42084 101 124 walton ak xx personal

    42085 100 123 walton il xx business

    42084 103 83 nwst ny xx inc

    42087 103 83 nw st ny xx inc

预期产出:

客户ID会员ID地址州电话类型

42084 100 123 walton il xx business

42084 101 124 walton ak xx personal

42084 103 83 nwst ny xx inc

42085 101 124 walton ak xx personal

42085 100 123 walton il xx business

42086 102 84 sw ny xx ext

42086 100 123 walton il xx business

42087 103 83 nwst ny xx inc

2 个答案:

答案 0 :(得分:2)

您想在CustomerID和AffiliateID列上使用分区 像这样...

select * from (select customerID,affiliateid,address,state,phone,type,
                 row_number() over(partition by CustomerID,AffiliateID
                            order by CustomerID,AffiliateID) r from xyzz)
 where r=1

这里

select * from (select customerID,affiliateid,address,state,phone,type,
                     row_number() over(partition by CustomerID,AffiliateID
                                order by CustomerID,AffiliateID) r from xyzz)

此查询返回所有带行号的行,按CustomerID分区 和AffiliateID

喜欢这个..r-是行号

CustomerID      AffiliateID        add       state      phone    r

   10              456             xyz         xx         00     1
   10              456             xyz         xx         00     2
   10              456             xyz         xx         00     3
   10              123             xyz         xx         00     1
   20              456             xyz         xx         00     1
   20              789             xyz         xx         00     1
   20              789             xyz         xx         00     2
   30              789             xyz         xx         00     1`

设置所有列的行号

where r=1

where子句返回设置为1的行 并消除重复的行

答案 1 :(得分:0)

只需使用
    SELECT DISTINCT CustomerID, AffiliateID FROM [TableName]

这将返回其中(CustomerID,AffiliateID)组合是唯一的值。 如果您需要在结果中包含其他字段,可以使用:

SELECT *
FROM [TableName]
GROUP BY CONCAT(CustomerID, AffiliateID);