避免列中的重复值

时间:2014-02-04 14:12:38

标签: sql postgresql postgresql-9.3

我的表table1看起来像这样:

 no    name

 1      C_GT_2013-2014,C_GT_AcMaster,C_GT_Master
 1      C_GT_2014-2015,C_GT_AcMaster,C_GT_Master
 2      C_TGYY_2013-2014,G_New_AcMaster,G_New_Master
 2      C_TGYY_2014-2015,G_New_AcMaster,G_New_Master
 3      C_TGYN_2013-2014,G_ACYMAN_AcMaster,C_TGYN_Master
 3      C_TGYN_2014-2015,G_ACYMAN_AcMaster,C_TGYN_Master
 4      C_TGNY_2013-2014,C_TGNY_AcMaster,G_ACNMAY_Master
 4      C_TGNY_2014-2015,C_TGNY_AcMaster,G_ACNMAY_Master
 5      C_GYY_2013-2014,G_New_AcMaster,G_New_Master
 6      C_DD_2013-2014,C_DD_AcMaster,G_ACNMAY_Master
 7      C_YN_2013-2014,G_ACYMAN_AcMaster,C_YN_Master

我想要检索没有重复no的值并使用“,”分割name,所以我尝试这样:

select (string_to_array(name,','))[3] as master
      ,(string_to_array(name,','))[2] as acmaster
      ,string_agg((string_to_array(name,','))[1],',') as trans
      ,no
 from table t
 where no in (2,3,4,6,7)
 group by (string_to_array(schemaname,','))[3]
        , (string_to_array(schemaname,','))[2]
        , no
 order by no

但它没有重复。我想得到价值

  1. no
  2. 中没有重复
  3. 名称具有相同的no。它使用','像这样(no 1 name with split(',') C_GT_2013-2014,C_GT_2014-2015
  4. 我正在使用 Postgresql 9.3

    我的结果将是:

    我想把Name分成三列,哪个词有master,它来到Master列,哪个词有它来到AcMaster,其他来到Trans没有重复

     Master            AcMaster                  Trans                                   No
    
     G_New_Master     G_New_AcMaster             C_TGYY_2013-2014,C_TGYY_2014-2015        1
     C_TGYN_Master,   G_ACYMAN_AcMaster          C_TGYN_2014-2015,C_TGYN_2013-2014,       2
     C_YN_Master                                             C_YN_2013-2014    
    
     G_ACNMAY_Master  C_DD_AcMaster,              C_DD_2013-2014                          3
                      C_TGNY_AcMaster             C_TGNY_2013-2014,C_TGNY_2014-2015
    

1 个答案:

答案 0 :(得分:1)

以下是您的查询:

select no,
string_agg(case when name like '%\_Master' escape '\' then name end, ','),
string_agg(case when name like '%\_AcMaster' escape '\' then name end, ','),
string_agg(case when (name not like '%\_Master' and name not like '%\_AcMaster' escape '\') then name end, ',')
from
(select no, (string_to_array(name,','))[1] as name
from   table1
union
select no, (string_to_array(name,','))[2] as name
from   table1
union
select no, (string_to_array(name,','))[3] as name
from   table1) t
where no in (2,3,4,6,7)
group by no
order by no

此查询结果为:

no  Master              AcMaster        Trans

2   G_New_Master        G_New_AcMaster      C_TGYY_2014-2015,C_TGYY_2013-2014
3   C_TGYN_Master       G_ACYMAN_AcMaster   C_TGYN_2014-2015,C_TGYN_2013-2014
4   G_ACNMAY_Master     C_TGNY_AcMaster     C_TGNY_2013-2014,C_TGNY_2014-2015
6   G_ACNMAY_Master     C_DD_AcMaster       C_DD_2013-2014
7   C_YN_Master         G_ACYMAN_AcMaster   C_YN_2013-2014

现在有一些解释:

1-我把你的桌子弄平:

(select no, (string_to_array(name,','))[1] as name
from   table1
union
select no, (string_to_array(name,','))[2] as name
from   table1
union
select no, (string_to_array(name,','))[3] as name
from   table1)

所以我可以得到这样的结果:

4   G_ACNMAY_Master
1   C_GT_Master
3   G_ACYMAN_AcMaster
1   C_GT_2013-2014
2   G_New_Master
2   G_New_AcMaster
1   C_GT_2014-2015
...

2-我选择了3个必填栏:

case when name like '%\_Master' escape '\' then name end -- Master
case when name like '%\_AcMaster' escape '\' then name end -- AcMaster
case when (name not like '%\_Master' and name not like '%\_AcMaster' escape '\') then name end -- Trans

获得以下结果:

2   G_New_Master
2                                       C_TGYY_2014-2015
2                   G_New_AcMaster
2                                       C_TGYY_2013-2014
3                                       C_TGYN_2014-2015
...

3-之后我使用no分组的string_agg函数聚合结果:

string_agg(case when name like '%\_Master' escape '\' then name end, ',')