select N'广河县畜牧局' ad, null as ad1 into #A union all select N'北京畜牧局' ad, null as ad1select 1 p_id,N'北京' pro,1 c_id,null city,1 t_id,N'昌平' town into #B union all select 3 ,N'河北',3,N'石家庄市',3,N'长安区'select ad,ad1=max(isnull(ad1,'')) from ( select ad ,ad1=case when CHARINDEX(pro,ad)>0 then pro when CHARINDEX(city,ad)>0 then city when CHARINDEX(town,ad)>0 then town end from #A,#B )t group by ad 这样?
select distinct ad,pro from (select ad, ad1=case when CHARINDEX(pro,ad)>0 then pro when CHARINDEX(city,ad)>0 then city when CHARINDEX(town,ad)>0 then town end from #A,#B),#B where ad1=pro or ad1=city or ad1=town 这样?
辛苦了辛苦了,表A中ad有多个不同的值,表B中pro、city、town都有很多不同的值,上万条,怎么先写入临时表???还有输出结果,ad先与B表中pro匹配,成功ad1显示pro的值(例如北京),不成功与city匹配,成功则显示的是city对应的pro的值(例如石家庄对应的是河北,则此ad1显示为河北,而不是石家庄),如不成功则与town匹配,成功显示town对应的city对应的pro(例如长安区对应的city是石家庄,石家庄对应的pro是河北,则ad1显示为河北),求大神继续帮忙啊,在线等,急用万分感谢啊。 select N'广河县畜牧局' ad, null as ad1 into #A union all select N'北京畜牧局' ad, null as ad1select 1 p_id,N'北京' pro,1 c_id,null city,1 t_id,N'昌平' town into #B union all select 3 ,N'河北',3,N'石家庄市',3,N'长安区'select ad,ad1=max(isnull(ad1,'')) from ( select ad ,ad1=case when CHARINDEX(pro,ad)>0 or CHARINDEX(city,ad)>0 or CHARINDEX(town,ad)>0 then pro end from #A,#B )t group by ad把#A ,#B换成你自己的表就行了。不用插入临时表。这临时表只是我用来放测试数据的
辛苦了辛苦了,表A中ad有多个不同的值,表B中pro、city、town都有很多不同的值,上万条,怎么先写入临时表???还有输出结果,ad先与B表中pro匹配,成功ad1显示pro的值(例如北京),不成功与city匹配,成功则显示的是city对应的pro的值(例如石家庄对应的是河北,则此ad1显示为河北,而不是石家庄),如不成功则与town匹配,成功显示town对应的city对应的pro(例如长安区对应的city是石家庄,石家庄对应的pro是河北,则ad1显示为河北),求大神继续帮忙啊,在线等,急用万分感谢啊。 select N'广河县畜牧局' ad, null as ad1 into #A union all select N'北京畜牧局' ad, null as ad1select 1 p_id,N'北京' pro,1 c_id,null city,1 t_id,N'昌平' town into #B union all select 3 ,N'河北',3,N'石家庄市',3,N'长安区'select ad,ad1=max(isnull(ad1,'')) from ( select ad ,ad1=case when CHARINDEX(pro,ad)>0 or CHARINDEX(city,ad)>0 or CHARINDEX(town,ad)>0 then pro end from #A,#B )t group by ad把#A ,#B换成你自己的表就行了。不用插入临时表。这临时表只是我用来放测试数据的 结果如果是广河县,没有进行匹配啊,ad1什么都没显示....请继续帮忙啊,大神。
辛苦了辛苦了,表A中ad有多个不同的值,表B中pro、city、town都有很多不同的值,上万条,怎么先写入临时表???还有输出结果,ad先与B表中pro匹配,成功ad1显示pro的值(例如北京),不成功与city匹配,成功则显示的是city对应的pro的值(例如石家庄对应的是河北,则此ad1显示为河北,而不是石家庄),如不成功则与town匹配,成功显示town对应的city对应的pro(例如长安区对应的city是石家庄,石家庄对应的pro是河北,则ad1显示为河北),求大神继续帮忙啊,在线等,急用万分感谢啊。 select N'广河县畜牧局' ad, null as ad1 into #A union all select N'北京畜牧局' ad, null as ad1select 1 p_id,N'北京' pro,1 c_id,null city,1 t_id,N'昌平' town into #B union all select 3 ,N'河北',3,N'石家庄市',3,N'长安区'select ad,ad1=max(isnull(ad1,'')) from ( select ad ,ad1=case when CHARINDEX(pro,ad)>0 or CHARINDEX(city,ad)>0 or CHARINDEX(town,ad)>0 then pro end from #A,#B )t group by ad把#A ,#B换成你自己的表就行了。不用插入临时表。这临时表只是我用来放测试数据的 结果如果是广河县,没有进行匹配啊,ad1什么都没显示....请继续帮忙啊,大神。 我的#B表只有两条,没有匹配到是因为#B表中没有广河县的数据。
辛苦了辛苦了,表A中ad有多个不同的值,表B中pro、city、town都有很多不同的值,上万条,怎么先写入临时表???还有输出结果,ad先与B表中pro匹配,成功ad1显示pro的值(例如北京),不成功与city匹配,成功则显示的是city对应的pro的值(例如石家庄对应的是河北,则此ad1显示为河北,而不是石家庄),如不成功则与town匹配,成功显示town对应的city对应的pro(例如长安区对应的city是石家庄,石家庄对应的pro是河北,则ad1显示为河北),求大神继续帮忙啊,在线等,急用万分感谢啊。 select N'广河县畜牧局' ad, null as ad1 into #A union all select N'北京畜牧局' ad, null as ad1select 1 p_id,N'北京' pro,1 c_id,null city,1 t_id,N'昌平' town into #B union all select 3 ,N'河北',3,N'石家庄市',3,N'长安区'select ad,ad1=max(isnull(ad1,'')) from ( select ad ,ad1=case when CHARINDEX(pro,ad)>0 or CHARINDEX(city,ad)>0 or CHARINDEX(town,ad)>0 then pro end from #A,#B )t group by ad把#A ,#B换成你自己的表就行了。不用插入临时表。这临时表只是我用来放测试数据的 结果如果是广河县,没有进行匹配啊,ad1什么都没显示....请继续帮忙啊,大神。 我的#B表只有两条,没有匹配到是因为#B表中没有广河县的数据。少一个市区的市字的,或者县字的都不匹配,空值,这该怎么办?我需要那么也进行匹配。还有就是加入华北油田,表B中没有,我想让显示ad自身字段的华北,这该怎么做呢?大神继续辛苦您了啊。。
辛苦了辛苦了,表A中ad有多个不同的值,表B中pro、city、town都有很多不同的值,上万条,怎么先写入临时表???还有输出结果,ad先与B表中pro匹配,成功ad1显示pro的值(例如北京),不成功与city匹配,成功则显示的是city对应的pro的值(例如石家庄对应的是河北,则此ad1显示为河北,而不是石家庄),如不成功则与town匹配,成功显示town对应的city对应的pro(例如长安区对应的city是石家庄,石家庄对应的pro是河北,则ad1显示为河北),求大神继续帮忙啊,在线等,急用万分感谢啊。 select N'广河县畜牧局' ad, null as ad1 into #A union all select N'北京畜牧局' ad, null as ad1select 1 p_id,N'北京' pro,1 c_id,null city,1 t_id,N'昌平' town into #B union all select 3 ,N'河北',3,N'石家庄市',3,N'长安区'select ad,ad1=max(isnull(ad1,'')) from ( select ad ,ad1=case when CHARINDEX(pro,ad)>0 or CHARINDEX(city,ad)>0 or CHARINDEX(town,ad)>0 then pro end from #A,#B )t group by ad把#A ,#B换成你自己的表就行了。不用插入临时表。这临时表只是我用来放测试数据的 结果如果是广河县,没有进行匹配啊,ad1什么都没显示....请继续帮忙啊,大神。 我的#B表只有两条,没有匹配到是因为#B表中没有广河县的数据。少一个市区的市字的,或者县字的都不匹配,空值,这该怎么办?我需要那么也进行匹配。还有就是加入华北油田,表B中没有,我想让显示ad自身字段的华北,这该怎么做呢?大神继续辛苦您了啊。。大神,快来继续帮忙啊,ad不能用group by后少了很多条记录,不能去掉ad中重复的,问题还没解决,求帮忙啊
select N'广河县畜牧局' ad, null as ad1
into #A
union all select N'北京畜牧局' ad, null as ad1select 1 p_id,N'北京' pro,1 c_id,null city,1 t_id,N'昌平' town
into #B
union all select 3 ,N'河北',3,N'石家庄市',3,N'长安区'select ad,ad1=max(isnull(ad1,''))
from
(
select ad
,ad1=case
when CHARINDEX(pro,ad)>0 then pro
when CHARINDEX(city,ad)>0 then city
when CHARINDEX(town,ad)>0 then town
end
from #A,#B
)t
group by ad
这样?
(select ad,
ad1=case
when CHARINDEX(pro,ad)>0 then pro
when CHARINDEX(city,ad)>0 then city
when CHARINDEX(town,ad)>0 then town
end
from #A,#B),#B where ad1=pro or ad1=city or ad1=town
这样?
select N'广河县畜牧局' ad, null as ad1
into #A
union all select N'北京畜牧局' ad, null as ad1select 1 p_id,N'北京' pro,1 c_id,null city,1 t_id,N'昌平' town
into #B
union all select 3 ,N'河北',3,N'石家庄市',3,N'长安区'select ad,ad1=max(isnull(ad1,''))
from
(
select ad
,ad1=case
when CHARINDEX(pro,ad)>0 or CHARINDEX(city,ad)>0 or CHARINDEX(town,ad)>0 then pro
end
from #A,#B
)t
group by ad把#A ,#B换成你自己的表就行了。不用插入临时表。这临时表只是我用来放测试数据的
select N'广河县畜牧局' ad, null as ad1
into #A
union all select N'北京畜牧局' ad, null as ad1select 1 p_id,N'北京' pro,1 c_id,null city,1 t_id,N'昌平' town
into #B
union all select 3 ,N'河北',3,N'石家庄市',3,N'长安区'select ad,ad1=max(isnull(ad1,''))
from
(
select ad
,ad1=case
when CHARINDEX(pro,ad)>0 or CHARINDEX(city,ad)>0 or CHARINDEX(town,ad)>0 then pro
end
from #A,#B
)t
group by ad把#A ,#B换成你自己的表就行了。不用插入临时表。这临时表只是我用来放测试数据的
结果如果是广河县,没有进行匹配啊,ad1什么都没显示....请继续帮忙啊,大神。
select N'广河县畜牧局' ad, null as ad1
into #A
union all select N'北京畜牧局' ad, null as ad1select 1 p_id,N'北京' pro,1 c_id,null city,1 t_id,N'昌平' town
into #B
union all select 3 ,N'河北',3,N'石家庄市',3,N'长安区'select ad,ad1=max(isnull(ad1,''))
from
(
select ad
,ad1=case
when CHARINDEX(pro,ad)>0 or CHARINDEX(city,ad)>0 or CHARINDEX(town,ad)>0 then pro
end
from #A,#B
)t
group by ad把#A ,#B换成你自己的表就行了。不用插入临时表。这临时表只是我用来放测试数据的
结果如果是广河县,没有进行匹配啊,ad1什么都没显示....请继续帮忙啊,大神。
我的#B表只有两条,没有匹配到是因为#B表中没有广河县的数据。
select N'广河县畜牧局' ad, null as ad1
into #A
union all select N'北京畜牧局' ad, null as ad1select 1 p_id,N'北京' pro,1 c_id,null city,1 t_id,N'昌平' town
into #B
union all select 3 ,N'河北',3,N'石家庄市',3,N'长安区'select ad,ad1=max(isnull(ad1,''))
from
(
select ad
,ad1=case
when CHARINDEX(pro,ad)>0 or CHARINDEX(city,ad)>0 or CHARINDEX(town,ad)>0 then pro
end
from #A,#B
)t
group by ad把#A ,#B换成你自己的表就行了。不用插入临时表。这临时表只是我用来放测试数据的
结果如果是广河县,没有进行匹配啊,ad1什么都没显示....请继续帮忙啊,大神。
我的#B表只有两条,没有匹配到是因为#B表中没有广河县的数据。少一个市区的市字的,或者县字的都不匹配,空值,这该怎么办?我需要那么也进行匹配。还有就是加入华北油田,表B中没有,我想让显示ad自身字段的华北,这该怎么做呢?大神继续辛苦您了啊。。
select N'广河县畜牧局' ad, null as ad1
into #A
union all select N'北京畜牧局' ad, null as ad1select 1 p_id,N'北京' pro,1 c_id,null city,1 t_id,N'昌平' town
into #B
union all select 3 ,N'河北',3,N'石家庄市',3,N'长安区'select ad,ad1=max(isnull(ad1,''))
from
(
select ad
,ad1=case
when CHARINDEX(pro,ad)>0 or CHARINDEX(city,ad)>0 or CHARINDEX(town,ad)>0 then pro
end
from #A,#B
)t
group by ad把#A ,#B换成你自己的表就行了。不用插入临时表。这临时表只是我用来放测试数据的
结果如果是广河县,没有进行匹配啊,ad1什么都没显示....请继续帮忙啊,大神。
我的#B表只有两条,没有匹配到是因为#B表中没有广河县的数据。少一个市区的市字的,或者县字的都不匹配,空值,这该怎么办?我需要那么也进行匹配。还有就是加入华北油田,表B中没有,我想让显示ad自身字段的华北,这该怎么做呢?大神继续辛苦您了啊。。大神,快来继续帮忙啊,ad不能用group by后少了很多条记录,不能去掉ad中重复的,问题还没解决,求帮忙啊