sql连接逗号分隔列

时间:2018-08-14 00:41:10

标签: sql left-join

应该很简单

我的数据是

TABLE1
AccreditationStatus         FacilityId      FacilityName                GroupAlphabet   NodeName    c_NodeId
        -------------------------------------------------------------------------------------------------------------
        Current                     12359116         Addiction Medicine         A               Nepean      4


TABLE2
        NodeId                      GroupNames      RegPerSup   FacCapacility
        ------------------------------------------------------------------------
        4                           A,B,X           1           2

我的查询

select *
from table1 left join
     table2 ast
     on ast.nodeid = c_NodeId and
     GroupAlphabet in (select items from SplitString(ast.GroupNames, ','))

返回第二个表中的所有NULL

Current                     12359116         Addiction Medicine         A               Nepean      4     NULL    NULL    NULL NULL

如果我尝试

select *
from table1 left join
     table2 ast
     on ast.nodeid = c_NodeId and
        GroupAlphabet like '%' + ast.GroupNames + '%'

只有完全匹配的结果才是正确的,而逗号分隔的组则不正确

SplitString是一个UDF,它返回一个表,其表的项数据类型为nvarchar(如果重要,GroupAlphabet的类型为varchar)

我的SplitString函数是

ALTER FUNCTION [dbo].[SplitString](@String NVARCHAR(max), @Delimiter char(1))    
returns @temptable TABLE (items NVARCHAR(max))       
as      
begin  
      declare @idx int   
     declare @slice NVARCHAR(max)         
select @idx = 1     
       if len(@String)<1 or @String is null  return   
      while @idx!= 0       
 begin            
set @idx = charindex(@Delimiter,@String)     
       if @idx!=0             
   set @slice = left(@String,@idx - 1)     
       else             
   set @slice = @String   
          if(len(@slice)>0)           
insert into @temptable(Items) values(@slice)      
       set @String = right(@String,len(@String) - @idx)      
      if len(@String) = 0 break      
  end   
 return       
 end

2 个答案:

答案 0 :(得分:3)

使用if __name__ == '__main__': import itertools MOD03_path = r"C:\Users\saviosebastian\MYD03.A2008001.0000.006.2012066122450.hdf" MOD06_path = r"C:\Users\saviosebastian\MYD06_L2.A2008001.0000.006.2013341193524.hdf" satellite = 'Aqua' yr = [2008] mn = [1] #np.arange(1,13) dy = [1] # latitude and longtitude boundaries of level-3 grid lat_bnd = np.arange(-90,91,1) lon_bnd = np.arange(-180,180,1) nlat = 180 nlon = 360 TOT_pix = np.zeros(nlat*nlon) CLD_pix = np.zeros(nlat*nlon) ### To use Spark in Python spark = SparkSession\ .builder\ .appName("Aggregation")\ .getOrCreate() filenames0=['']*500 i=0 for y,m,d in itertools.product(yr,mn,dy): #-------------find the MODIS prodcts--------------# date = datetime.datetime(y,m,d) JD01, JD02 = gcal2jd(y,1,1) JD1, JD2 = gcal2jd(y,m,d) JD = np.int((JD2+JD1)-(JD01+JD02) + 1) granule_time = datetime.datetime(y,m,d,0,0) while granule_time <= datetime.datetime(y,m,d,23,55): # 23,55 print('granule time:',granule_time) **[MOD03_fp = 'MYD03.A{:04d}{:03d}.{:02d}{:02d}.006.?????????????.hdf'.format(y,JD,granule_time.hour,granule_time.minute)][1]** MOD06_fp = 'MYD06_L2.A{:04d}{:03d}.{:02d}{:02d}.006.?????????????.hdf'.format(y,JD,granule_time.hour,granule_time.minute) MOD03_fn, MOD06_fn =[],[] for MOD06_flist in os.listdir(MOD06_path): if fnmatch.fnmatch(MOD06_flist, MOD06_fp): MOD06_fn = MOD06_flist for MOD03_flist in os.listdir(MOD03_path): if fnmatch.fnmatch(MOD03_flist, MOD03_fp): MOD03_fn = MOD03_flist if MOD03_fn and MOD06_fn: # if both MOD06 and MOD03 products are in the directory 的正确格式是:

like

我不确定您的第一个查询出了什么问题。但是,您应该使用联结表,而不是以分隔的字符串存储事物列表。 SQL具有用于存储列表的良好数据结构。它称为,而不是 string

答案 1 :(得分:2)

Gordon Linoff是行级数据存储的正确选择,而不是用逗号分隔。 如果您使用逗号分隔的数据并且表的大小很高,那么我将为MS SQL Server提供另一种具有良好查询性能的解决方案。

将数据用逗号分隔以列出,然后对所需的表使用JOIN。

try {
console.log(window.device.version) 
} catch (e) {
console.log('Error')
}