我正在尝试将输出分成两行而不是一行。我最终希望通过循环遍历这些返回中的每一个来进行插入,但是一次只能一步。
select m6.mrid
,'6' as 'FromProj'
,m10.mrid
,'10' as 'ToProj'
from master6 m6
left join master10 m10
on (m01.mrtitle like '%40[%] load%' and m6.mrtitle like '%40[%] load%')
or ((m6.mrtitle like '%ie11 know%' or m6.mrtitle like '%ie11 kr%') and m10.mrtitle like '%ie11 kr%')
or (m6.mrtitle like '%survey issue%' and m10.mrtitle like '%survey issue%')
or (m6.mrtitle like '%binary script%' and m10.mrtitle like '%binary script%')
or (m6.mrtitle like '%zoom issue%' and m10.mrtitle like '%zoom issue%')
or (m6.mrtitle like '%auto%submit%' and m10.mrtitle like '%auto%submit%')
where m10.mrtitle is not null
结果:
mrid FromProj mrid ToProj
21 6 666 10
23 6 666 10
343 6 554 10
3432 6 222 10
5432 6 222 10
期望的结果:
mrid FromProj
21 6
666 10
23 6
666 10
343 6
554 10
3432 6
222 10
5432 6
222 10
答案 0 :(得分:0)
您可以CROSS JOIN
将当前结果行表示为' 6'和' 10'获取行重复,然后使用mrid
过滤掉相应的CASE...WHEN
(m6或m10):
修改
为了保留原始值的交错顺序,将原始查询移动到派生表或CTE中,并为每个投影行指定递增计数器(在交叉连接之前)。我使用了ROW_NUMBER()
,但您也可以使用SELECT CASE WHEN x.FromProj = '6'
THEN y.m6rid
ELSE y.m10mrid
END AS mrid,
x.FromProj
from
(SELECT m10.mrid as m10mrid, m6.mrid AS m6mrid,
ROW_NUMBER() OVER (ORDER BY m6.mrid ASC) AS rowOrder
FROM master6 m6
left join master10 m10
on (m01.mrtitle like '%40[%] load%' and m6.mrtitle like '%40[%] load%')
or ((m6.mrtitle like '%ie11 know%' or m6.mrtitle like '%ie11 kr%')
and m10.mrtitle like '%ie11 kr%')
or (m6.mrtitle like '%survey issue%' and m10.mrtitle like '%survey issue%')
or (m6.mrtitle like '%binary script%' and m10.mrtitle like '%binary script%')
or (m6.mrtitle like '%zoom issue%' and m10.mrtitle like '%zoom issue%')
or (m6.mrtitle like '%auto%submit%' and m10.mrtitle like '%auto%submit%')
where m10.mrtitle is not null
) y
cross join
(select '6' as FromProj
UNION
select '10') x
ORDER BY rowOrder ASC, FromProj ASC; -- Since you want 6 before 10
。使用序列。
{{1}}
答案 1 :(得分:0)
如果你是在2005年以上,你可以使用CROSS APPLY
取消此
SELECT ca.mrid,
ca.FromProj
FROM master6 m6
INNER JOIN master10 m10
ON ( m01.mrtitle LIKE '%40[%] load%'
AND m6.mrtitle LIKE '%40[%] load%' )
OR ( ( m6.mrtitle LIKE '%ie11 know%'
OR m6.mrtitle LIKE '%ie11 kr%' )
AND m10.mrtitle LIKE '%ie11 kr%' )
OR ( m6.mrtitle LIKE '%survey issue%'
AND m10.mrtitle LIKE '%survey issue%' )
OR ( m6.mrtitle LIKE '%binary script%'
AND m10.mrtitle LIKE '%binary script%' )
OR ( m6.mrtitle LIKE '%zoom issue%'
AND m10.mrtitle LIKE '%zoom issue%' )
OR ( m6.mrtitle LIKE '%auto%submit%'
AND m10.mrtitle LIKE '%auto%submit%' )
CROSS APPLY (SELECT m6.mrid,
6
UNION ALL
SELECT m10.mrid,
10) ca(mrid, FromProj)
WHERE m10.mrtitle IS NOT NULL
ORDER BY mrid,
FromProj