我必须比较以下两个数据表
表a
cust text
1 Director (Corporate Trustee)@Settlor
2 Director@Settlor@Shareholder
表b
cust text
1 Settlor@Director (Corporate Trustee)
2 Settlor@Director@Shareholder
我正在使用除了但是返回了不匹配,b'ze文本字段中的单词不是按顺序。
请问快速解决方案吗?
答案 0 :(得分:1)
为了让您入门,您可以使用字符串拆分功能(假设您的值由文本中的@分隔),然后比较每个客户的值。
declare @a table(cust int
,[text] nvarchar(500)
);
declare @b table(cust int
,[text] nvarchar(500)
);
insert into @a values
(1,'Director (Corporate Trustee)@Settlor')
,(2,'Director@Settlor@Shareholder');
insert into @b values
(1,'Settlor@Director (Corporate Trustee)')
,(2,'Settlor@Director@Shareholder@ExtraText'); -- Note the ExtraText inserted here.
with a -- Use derived tables to build a working dataset for both source tables.
as
(
select *
from @a a
cross apply dbo.DelimitedSplit8K(a.text,'@') aa -- This is how you use the string split function.
),b
as
(
select *
from @b b
cross apply dbo.DelimitedSplit8K(b.text,'@') bb
)
select a.cust as CustA
,a.[text] as TextA
,a.ItemNumber as ItemNumberA
,a.Item as ItemA
,b.cust as CustB
,b.[text] as TextB
,b.ItemNumber as ItemNumberB
,b.Item as ItemB
from a
full join b
on(a.cust = b.cust
and a.Item = b.Item
)
order by 1,2,3;
注意ExtraText为表A返回null,因为它不存在。
+-------+--------------------------------------+-------------+------------------------------+-------+----------------------------------------+-------------+------------------------------+
| CustA | TextA | ItemNumberA | ItemA | CustB | TextB | ItemNumberB | ItemB |
+-------+--------------------------------------+-------------+------------------------------+-------+----------------------------------------+-------------+------------------------------+
| 1 | Director (Corporate Trustee)@Settlor | 1 | Director (Corporate Trustee) | 1 | Settlor@Director (Corporate Trustee) | 2 | Director (Corporate Trustee) |
| 1 | Director (Corporate Trustee)@Settlor | 2 | Settlor | 1 | Settlor@Director (Corporate Trustee) | 1 | Settlor |
| 2 | Director@Settlor@Shareholder | 1 | Director | 2 | Settlor@Director@Shareholder@ExtraText | 2 | Director |
| 2 | Director@Settlor@Shareholder | 2 | Settlor | 2 | Settlor@Director@Shareholder@ExtraText | 1 | Settlor |
| 2 | Director@Settlor@Shareholder | 3 | Shareholder | 2 | Settlor@Director@Shareholder@ExtraText | 3 | Shareholder |
| NULL | NULL | NULL | NULL | 2 | Settlor@Director@Shareholder@ExtraText | 4 | ExtraText |
+-------+--------------------------------------+-------------+------------------------------+-------+----------------------------------------+-------------+------------------------------+
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE FUNCTION [dbo].[DelimitedSplit8K]
--===== Define I/O parameters
(@pString VARCHAR(8000), @pDelimiter CHAR(1))
--WARNING!!! DO NOT USE MAX DATA-TYPES HERE! IT WILL KILL PERFORMANCE!
RETURNS TABLE WITH SCHEMABINDING AS
RETURN
--===== "Inline" CTE Driven "Tally Table" produces values from 1 up to 10,000...
-- enough to cover VARCHAR(8000)
WITH E1(N) AS (
SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL
SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL
SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1
), --10E+1 or 10 rows
E2(N) AS (SELECT 1 FROM E1 a, E1 b), --10E+2 or 100 rows
E4(N) AS (SELECT 1 FROM E2 a, E2 b), --10E+4 or 10,000 rows max
cteTally(N) AS (--==== This provides the "base" CTE and limits the number of rows right up front
-- for both a performance gain and prevention of accidental "overruns"
SELECT TOP (ISNULL(DATALENGTH(@pString),0)) ROW_NUMBER() OVER (ORDER BY (SELECT NULL)) FROM E4
),
cteStart(N1) AS (--==== This returns N+1 (starting position of each "element" just once for each delimiter)
SELECT 1 UNION ALL
SELECT t.N+1 FROM cteTally t WHERE SUBSTRING(@pString,t.N,1) = @pDelimiter
),
cteLen(N1,L1) AS(--==== Return start and length (for use in substring)
SELECT s.N1,
ISNULL(NULLIF(CHARINDEX(@pDelimiter,@pString,s.N1),0)-s.N1,8000)
FROM cteStart s
)
--===== Do the actual split. The ISNULL/NULLIF combo handles the length for the final element when no delimiter is found.
SELECT ItemNumber = ROW_NUMBER() OVER(ORDER BY l.N1),
Item = SUBSTRING(@pString, l.N1, l.L1)
FROM cteLen l
GO