22,209
社区成员
发帖
与我相关
我的任务
分享
with tb as (
select 1 a ,1 b union all
select 1,2 union all
select 1,3 union all
select 1,4 union all
select 2,1 union all
select 3,2 union all
select 3,3
),
--上面是建标语句,为了测试用的,你要用从下面开始用
tc as(
select distinct * from (select a from tb)a,(select b from tb)b)
,td as(
select tc.a,(case when tb.b is null then '0' else '1' end)b
from tc left join tb on tc.a=tb.a and tc.b=tb.b)
,te as(
select distinct a,b=(select ''+b from td where a.a=a for xml path('')) from td a)
--下面是更新语句
update b
set col2=te.b from te where te.a=b.col1
WITH CTE
AS ( SELECT 1 AS ctecol1 ,1 AS ctecol2 ,'0' AS ctecol3
UNION ALL
SELECT 1 AS ctecol1 ,2 AS ctecol2 ,'0' AS ctecol3
UNION ALL
SELECT 1 AS ctecol1 ,3 AS ctecol2 ,'0' AS ctecol3
UNION ALL
SELECT 1 AS ctecol1 ,4 AS ctecol2 ,'0' AS ctecol3
UNION ALL
SELECT 2 AS ctecol1 ,1 AS ctecol2 ,'0' AS ctecol3
UNION ALL
SELECT 2 AS ctecol1 ,2 AS ctecol2 ,'0' AS ctecol3
UNION ALL
SELECT 2 AS ctecol1 ,3 AS ctecol2 ,'0' AS ctecol3
UNION ALL
SELECT 2 AS ctecol1 ,4 AS ctecol2 ,'0' AS ctecol3
UNION ALL
SELECT 3 AS ctecol1 ,1 AS ctecol2 ,'0' AS ctecol3
UNION ALL
SELECT 3 AS ctecol1 ,2 AS ctecol2 ,'0' AS ctecol3
UNION ALL
SELECT 3 AS ctecol1 ,3 AS ctecol2 ,'0' AS ctecol3
UNION ALL
SELECT 3 AS ctecol1 ,4 AS ctecol2 ,'0' AS ctecol3
),
cte2 AS ( SELECT a.ctecol1,
CASE WHEN t.col2 > 0 THEN '1' ELSE '0' END col
FROM CTE a
LEFT JOIN A_test t ON a.ctecol1 = t.col1
AND a.ctecol2 = t.col2 )
SELECT ctecol1,(SELECT col +'' FROM cte2 a WHERE a.ctecol1 = t.ctecol1 FOR XML PATH ('') ) AS target_value
FROM cte2 t
GROUP BY ctecol1
with tb as (
select 1 a ,1 b union all
select 1,2 union all
select 1,3 union all
select 1,4 union all
select 2,1 union all
select 3,2 union all
select 3,3
),
tc as(
select distinct * from (select a from tb)a,(select b from tb)b)
,td as(
select tc.a,(case when tb.b is null then '0' else '1' end)b
from tc left join tb on tc.a=tb.a and tc.b=tb.b)
select distinct a,b=(select ''+b from td where a.a=a for xml path('')) from td a
update B
SET B.[col2]= A.[col2]
FROM [dbo].[tableB] B INNER JOIN
(
SELECT [col1],[col2]=right('0000' + convert(varchar,sum(POWER (10,4-(col2)))),4)
from [dbo].[tableA]
group by [col1]
) A ON B.[col1]=A.[col1]