【问题标题】:Getting Number of Common Values from 2 comma-seperated strings从 2 个逗号分隔的字符串中获取公共值的数量
【发布时间】:2018-07-13 23:27:57
【问题描述】:

我在 Postgres 的列中有一个包含逗号分隔值的表。

ID                          PRODS
--------------------------------------
1                           ,142,10,75,
2                           ,142,87,63,
3                           ,75,73,2,58,
4                           ,142,2,

现在我想要一个查询,我可以在其中给出一个逗号分隔的字符串,它会告诉我输入字符串和行中存在的字符串之间的匹配数。

例如,对于输入值',142,87,',我想要输出类似

ID                          PRODS                        No. of Match
------------------------------------------------------------------------
1                           ,142,10,75,                  1
2                           ,142,87,63,                  2
3                           ,75,73,2,58,                 0
4                           ,142,2,                      1

【问题讨论】:

  • what are match with what ....your question is not all clear
  • 这是糟糕的数据库设计。我们可以处理在 CSV 字符串中搜索单个值,但两端的 CSV 是有缺陷的。你应该标准化你的数据。
  • 你是对的 Tim,这是相当古老的数据结构。但到目前为止,我必须忍受这个。

标签: sql postgresql


【解决方案1】:

试试这个:

SELECT
    *,
    ARRAY(
        SELECT 
            * 
        FROM 
            unnest(string_to_array(trim(both ',' from prods), ',')) 
        WHERE 
            unnest = ANY(string_to_array(',142,87,', ','))
    )
FROM
    prods_table;

输出是:

1   ,142,10,75,     {142}
2   ,142,87,63,     {142,87}
3   ,75,73,2,58,    {}
4   ,142,2,         {142}

将cardinality(anyarray) 函数添加到最后一列以获取匹配的数量。

并考虑更改您的数据库设计。

【讨论】:

    【解决方案2】:

    检查一下。

        select T.*,
        COALESCE(No_of_Match,'0')  
        from TT T Left join
        (
            select ID,count(ID) No_of_Match 
            from (
                    select ID,unnest(string_to_array(trim(t.prods, ','), ',')) A
                    from TT t)a 
            Where A in ('142','87')
            group by ID
        )B
        On T.Id=b.id
    

    Demo Here

    输出

    【讨论】:

      【解决方案3】:

      如果您安装 intarray 扩展,这将变得非常简单:

      select id, prods, cardinality(string_to_array(trim(prods, ','), ',')::int[] & array[142,87])
      from bad_design;
      

      不然会复杂一点:

      select bd.id, bd.prods, m.matches
      from bad_design bd
        join lateral (
          select bd.id, count(v.p) as matches
          from unnest(string_to_array(trim(bd.prods, ','), ',')) as l(p)
            left join ( 
               values ('142'),('87')  --<< these are your input values
            ) v(p) on l.p = v.p
          group by bd.id
        ) m on m.id = bd.id
      order by bd.id;
      

      在线示例:http://rextester.com/ZIYS97736

      但你应该真的修复你的数据模型。

      【讨论】:

        【解决方案4】:
        with data as
        ( 
        select *, 
        unnest(string_to_array(trim(both ',' from prods), ',') ) as v
        from myTable
        ),
        counts as 
        (
        select id, count(t) as c from data 
        left join 
        ( select unnest(string_to_array(',142,87,', ',') ) as t) tmp on tmp.t = data.v 
        group by id
        order by id
        )
        select t1.id, t1.prods, t2.c as "No. of Match"
        from myTable t1
        inner join counts t2 on t1.id = t2.id;
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2019-05-08
          • 2012-05-19
          • 2011-09-07
          • 1970-01-01
          • 2017-07-22
          • 2013-07-14
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多