试试这个解决方案 -
DECLARE @temp TABLE
(
id INT IDENTITY(1,1)
, k NVARCHAR(100)
, t NVARCHAR(500)
)
INSERT INTO @temp (k, t)
VALUES
(',15,90,104,73,95,13,14,', 'Medium~Male~28~Green~Right~No~10/04/2013'),
(',14,73,104,95,15,13,90,', '12/03/2013~Green~28~Right~Medium~No~Male~')
SELECT
data.id
, data.p
, data.r
FROM (
SELECT
p = p.value('(.)[1]', 'NVARCHAR(50)')
, po = p.value('for $i in . return count(../*[. << $i])', 'int')
, r = r.value('(.)[1]', 'NVARCHAR(50)')
, ro = r.value('for $i in . return count(../*[. << $i])', 'int')
, d.id
FROM (
SELECT
t.id
, txml = CAST('<r><s>' + REPLACE(t.k + ',', ',', '</s>' + '<s>') + '</s></r>' AS XML)
, kxml = CAST('<r><s>' + REPLACE(t.t + '~', '~', '</s>' + '<s>') + '</s></r>' AS XML)
FROM @temp t
) d
CROSS APPLY kxml.nodes('/r/s') t(p)
CROSS APPLY txml.nodes('/r/s') k(r)
) data
WHERE data.po = data.ro - 1
AND data.r IN ('14', '90')
--AND r + data.p != ''
ORDER BY data.id