添加搜索相似字段的行

时间:2013-12-30 10:15:20

标签: sql database

数据以下列格式记录。

 name                count

'EB4_1F_N_MAIN_IN'
'EB4_1F_N_MOD4_IN'
'EB4_1F_N_MOD5_IN'
'EB4_1F_S_MAIN_IN'
'EB4_2F_N_MAIN_IN'
'EB4_2F_S_MAIN_IN'
'EB4_3F_N_MAIN_IN'
'EB4_3F_S_MAIN_IN'
'EB4_3F_SE_TRN_IN'
'EB4_4F_N_MAIN_IN'
'EB4_4F_S_MAIN_IN'
'EB4_4FSE_TRUN_IN'
'EB4_5F_S_MAIN_IN'
'EB4_GF_N_MAIN_IN'
'EB4_GF_S_MAIN_IN'
'EB5_1F_N_MAIN_IN'
'EB5_1F_S_MAIN_IN'
'EB5_2F_N_MAIN_IN'
'EB5_2F_S_MAIN_IN'
'EB5_2F_SW_DMZ_IN'
'EB5_3F_N_MAIN_IN'
'EB5_3F_S_MAIN_IN'
'EB5_4F_N_MAIN_IN'
'EB5_4F_S_MAIN_IN'
'EB5_5F_N_MAIN_IN'
'EB5_5F_S_MAIN_IN'
'EB5_6F_N_MAIN_IN'
'EB5_6F_S_MAIN_IN'
'EB5_DC_EPX_ST_IN'
'EB5_DC_FM_RM1_IN'
'EB5_DC_FM_RM2_IN'
'EB5_DC_NEPAHU_IN'
'EB5_DC_NWPAHU_IN'
'EB5_DC_SO_NWE_IN'
'EB5_GF_N_MAIN_IN'
'EB6_1F_N_MAIN_IN'
'EB6_1F_S_MAIN_IN'
'EB6_1F_SW_ODC_IN'
'EB6_2F_N_MAIN_IN'
'EB6_2F_S_MAIN_IN'
'EB6_3F_N_MAIN_IN'
'EB6_3F_S_MAIN_IN'
'EB6_4F_N_MAIN_IN'
'EB6_4F_S_MAIN_IN'
'EB6_5F_CISCOMAIN'
'EB6_5F_N_MAIN_IN'
'EB6_5F_S_MAIN_IN'
'EB6_GF_N_MAIN_IN'
'EB6_GF_S_MAIN_IN'

以上是表格的快照。 count没有显示。我的要求是添加类似名称字段的数量,如添加所有1F_N和类似。 你可以告诉我我该怎么做。

1 个答案:

答案 0 :(得分:0)

也许这就是你想要的东西?我使用SQL Server进行了测试,因为您没有指定您正在使用的DBMS。你也没有定义你如何确定相似性...我在这个查询中使用了从6开始的四个字符串子串,但它应该很容易适应你的需要。

SELECT a.name, 
       b.cnt AS Count 
FROM   table1 a 
       INNER JOIN (SELECT Substring(name, 6, 4) AS substr, 
                          Count(*)              AS cnt 
                   FROM   table1 
                   GROUP  BY Substring(name, 6, 4)) b 
               ON Substring(a.name, 6, 4) = b.substr 

Sample SQL Fiddle