列包含来自另一列的字符

时间:2018-09-10 21:05:04

标签: sql-server tsql

是否有一种简单的方法可以使条件“列A中的字符来自列B中的字符”?基本上,我有一个专栏,每个工作日的每一天都有一封信。 MTWHF。我需要在日期匹配的地方加入两条记录,这基本上就是字母在字符串中是否匹配。

public class Singleton<T> implements Supplier<T> {

    private boolean initialized;
    private Supplier<T> singletonSupplier;

    public Singleton(T singletonValue) {
        this.singletonSupplier = () -> singletonValue;
    }

    public Singleton(Supplier<T> supplier) {
        this.singletonSupplier = () -> {
            // The initial supplier is temporary; it will be replaced after initialization
            synchronized (supplier) {
                if (!initialized) {
                    T singletonValue = supplier.get();
                    // Now that the singleton value has been initialized,
                    // replace the blocking supplier with a non-blocking supplier
                    singletonSupplier = () -> singletonValue;
                    initialized = true;
                }
                return singletonSupplier.get();
            }
        };
    }

    @Override
    public T get() {
        return singletonSupplier.get();
    }
}

SQL查询类似于;

----------------
| ID | MetDays |
----------------
| 1  | 'MWF'   |
| 2  | 'TH'    |
| 3  | 'M'     |
| 4  | 'T'     |
| 5  | 'WHF'   |
----------------

在这种情况下,我会在SELECT MyTableA.ID AS IDa, MyTableB.ID AS IDb FROM MyTable AS MyTableA JOIN MyTable AS MyTableB ON MyTableA.MetDays ???? MyTableB.MetDays 之间成功;

JOIN

2 个答案:

答案 0 :(得分:2)

create table MyTable ( ID int, MetDays varchar(5) )

insert into MyTable ( ID, MetDays ) values
( 1, 'MWF' ),
( 2, 'TH'  ),
( 3, 'M'   ),
( 4, 'T'   ),
( 5, 'WHF' )


;with 

  -- Create a table of the 5 characters.
  -- You might want to make this a permanent table.

  DayList as
  (       select 'M' as aDay 
    union select 'T' 
    union select 'W' 
    union select 'H' 
    union select 'F' ),

  -- Join MyTable with this list.
  -- The result will be one record for each letter in each row
  -- ID aDay
  --  1  M
  --  1  W
  --  1  F
  -- and so on

  MetDayList as
  ( select ID, aDay 
    from MyTable
    join DayList 
    on MyTable.MetDays like '%' + aDay + '%' )

  -- Self join this table

  select distinct A.ID as IDa, B.ID as IDb 
  from MetDayList A
  join MetDayList B 
  on A.ID <> B.ID
  and A.aDay=B.aDay 
  order by IDa, IDb

答案 1 :(得分:1)

这是一种使用substring将日期分为5个单独的列的方法:

select t1.id as IDa, t2.id as IDb
from mytable t1, mytable t2
where t1.id != t2.id and 
    (   (t1.metdays like '%' + substring(t2.metdays,1,1) + '%' and substring(t2.metdays,1,1) != '')
     or (t1.metdays like '%' + substring(t2.metdays,2,1) + '%' and substring(t2.metdays,2,1) != '')
     or (t1.metdays like '%' + substring(t2.metdays,3,1) + '%' and substring(t2.metdays,3,1) != '')
     or (t1.metdays like '%' + substring(t2.metdays,4,1) + '%' and substring(t2.metdays,4,1) != '')
     or (t1.metdays like '%' + substring(t2.metdays,5,1) + '%' and substring(t2.metdays,5,1) != '')
    )
order by t1.id, t2.id