22,294
社区成员
发帖
与我相关
我的任务
分享create table [table_a] (a int,b varchar(4),c int)
insert into [table_a]
select 1,'名称',0 union all
select 2,'名称',0 union all
select 3,'名称',0 union all
select 4,'名称',1 union all
select 5,'名称',1 union all
select 6,'名称',1 union all
select 7,'名称',2 union all
select 8,'名称',3 union all
select 9,'名称',2 union all
select 10,'名称',2 union all
select 11,'名称',1 union all
select 12,'名称',3 union all
select 13,'名称',2
create table [table_b] (aa int,bb varchar(20))
insert into [table_b]
select 1,'4,7,8' union all
select 2,'6,9,12' union all
select 3,'11,10,8' union all
select 4,'5,13,12'
go
SELECT TOP 8000 id = IDENTITY(int, 1, 1) INTO # FROM syscolumns a, syscolumns b
select m.* , count(*) d from table_a m,table_a o,
(
SELECT A.aa, bb = SUBSTRING(A.[bb], B.id, CHARINDEX(',', A.[bb] + ',', B.id) - B.id)
FROM table_b A, # B
WHERE SUBSTRING(',' + A.[bb], B.id, 1) = ','
) n
where m.c = 0 and m.a = o.c and o.a = n.bb
group by m.a, m.b , m.c
union all
select m.* , count(*) d from table_a m,
(
SELECT A.aa, bb = SUBSTRING(A.[bb], B.id, CHARINDEX(',', A.[bb] + ',', B.id) - B.id)
FROM table_b A, # B
WHERE SUBSTRING(',' + A.[bb], B.id, 1) = ','
) n
where m.c <> 0 and m.a = n.bb
group by m.a, m.b , m.c
order by m.a
DROP TABLE #
drop table table_a , table_b
/*
a b c d
----------- ---- ----------- -----------
1 名称 0 4
2 名称 0 4
3 名称 0 4
4 名称 1 1
5 名称 1 1
6 名称 1 1
7 名称 2 1
8 名称 3 2
9 名称 2 1
10 名称 2 1
11 名称 1 1
12 名称 3 2
13 名称 2 1
(所影响的行数为 13 行)
*//*
标题:分拆列值
作者:爱新觉罗.毓华(十八年风雨,守得冰山雪莲花开)
时间:2008-11-20
地点:广东深圳
描述
有表tb, 如下:
id value
----------- -----------
1 aa,bb
2 aaa,bbb,ccc
欲按id,分拆value列, 分拆后结果如下:
id value
----------- --------
1 aa
1 bb
2 aaa
2 bbb
2 ccc
*/
--1. 旧的解决方法(sql server 2000)
SELECT TOP 8000 id = IDENTITY(int, 1, 1) INTO # FROM syscolumns a, syscolumns b
SELECT A.id, SUBSTRING(A.[values], B.id, CHARINDEX(',', A.[values] + ',', B.id) - B.id)
FROM tb A, # B
WHERE SUBSTRING(',' + A.[values], B.id, 1) = ','
DROP TABLE #
--2. 新的解决方法(sql server 2005)
create table tb(id int,value varchar(30))
insert into tb values(1,'aa,bb')
insert into tb values(2,'aaa,bbb,ccc')
go
SELECT A.id, B.value
FROM(
SELECT id, [value] = CONVERT(xml,'<root><v>' + REPLACE([value], ',', '</v><v>') + '</v></root>') FROM tb
)A
OUTER APPLY(
SELECT value = N.v.value('.', 'varchar(100)') FROM A.[value].nodes('/root/v') N(v)
)B
DROP TABLE tb
/*
id value
----------- ------------------------------
1 aa
1 bb
2 aaa
2 bbb
2 ccc
(5 行受影响)
*/if object_id('[ta]') is not null drop table [ta]
go
create table [ta]([a] int,[b] varchar(4),[c] int)
insert [ta]
select 1,'名称',0 union all
select 2,'名称',0 union all
select 3,'名称',0 union all
select 4,'名称',1 union all
select 5,'名称',1 union all
select 6,'名称',1 union all
select 7,'名称',2 union all
select 8,'名称',3 union all
select 9,'名称',2 union all
select 10,'名称',2 union all
select 11,'名称',1 union all
select 12,'名称',3 union all
select 13,'名称',2
if object_id('[tb]') is not null drop table [tb]
go
create table [tb]([aa] int,[bb] varchar(7))
insert [tb]
select 1,'4,7,8' union all
select 2,'6,9,12' union all
select 3,'11,10,8' union all
select 4,'5,13,12'
select * from [ta]
select * from [tb]
select top 100 id=identity(int,1,1) into # from syscolumns
select a,d=count(1) into #1
from
(
select a=substring(bb,b.id,charindex(',',a.bb+',',b.id)-b.id)
from tb a,# b
where charindex(',',','+a.bb,b.id)=b.id
) t
group by a
select a.a,a.b,a.c,d=isnull(b.d,0) into #2
from ta a left join #1 b
on a.a=b.a
select a,b,c,d=case when c=0 then (select sum(d) from #2 where c=t.a) else d end
from #2 t
/*
a b c d
----------- ---- ----------- -----------
1 名称 0 4
2 名称 0 4
3 名称 0 4
4 名称 1 1
5 名称 1 1
6 名称 1 1
7 名称 2 1
8 名称 3 2
9 名称 2 1
10 名称 2 1
11 名称 1 1
12 名称 3 2
13 名称 2 1
(13 行受影响)
*/
drop table #
drop table #1
drop table #2
--> 测试数据: [table_a]
if object_id('[table_a]') is not null drop table [table_a]
create table [table_a] (a int,b varchar(4),c int)
insert into [table_a]
select 1,'名称',0 union all
select 2,'名称',0 union all
select 3,'名称',0 union all
select 4,'名称',1 union all
select 5,'名称',1 union all
select 6,'名称',1 union all
select 7,'名称',2 union all
select 8,'名称',3 union all
select 9,'名称',2 union all
select 10,'名称',2 union all
select 11,'名称',1 union all
select 12,'名称',3 union all
select 13,'名称',2
--> 测试数据: [table_b]
if object_id('[table_b]') is not null drop table [table_b]
create table [table_b] (aa int,bb varchar(20))
insert into [table_b]
select 1,'4,7,8' union all
select 2,'6,9,12' union all
select 3,'11,10,8' union all
select 4,'5,13,12'
select *,d=case c when 0 then (select count(1) from table_b,table_a d where d.c=a.a and charindex(','+ltrim(d.a)+',',','+ltrim(bb)+',')>0)
else (select count(1) from table_b where charindex(','+ltrim(a.a)+',',','+ltrim(bb)+',')>0) end
from [table_a] a
--结果:
a b c d
----------- ---- ----------- -----------
1 名称 0 4
2 名称 0 4
3 名称 0 4
4 名称 1 1
5 名称 1 1
6 名称 1 1
7 名称 2 1
8 名称 3 2
9 名称 2 1
10 名称 2 1
11 名称 1 1
12 名称 3 2
13 名称 2 1
(13 行受影响)