CTE有效,但简单的JOIN不会?

时间:2015-10-30 19:46:23

标签: sql-server tsql

我试图弄清楚为什么利用CTE去除错误的信息,但我不能直接在JOIN中使用相同的数据转换。例如,我有以下DOCUMENT表,如下所示:

RECTYPE  TYPEDESC               PAGE    DOCIMGLINK 
---------------------------------------------------------------------------
D        Delayed Birth Record   00127   1910-007704118500089870170008786670
D        Birth Record           00056   1910-007701897300089870160008810074
D        Delayed Birth Record   00118   1912-007704116600089870180008786651
D        Birth Record           00004   1913-007702338100089870200008804037
D        Birth Record           00249   1913-007702362500089870210008804281

然后我有一张表DM_CONTENT,其中包含以下信息:

DM_ID   DM_PATH
------------------------------------------------------------
1       BR\1910\1910-007704118500089870170008786670.TIF
2       BR\1910\1910-007701897300089870160008810074.TIF
3       BR\1912\1912-007704116600089870180008786651.TIF
4       BR\1913\1913-007702338100089870200008804037.TIF
5       BR\1913\1913-007702362500089870210008804281.TIF

如您所见,DM_PATHDOCIMGLINK,其中嵌入了其他信息。我想使用DOCIMGLINK作为JOIN两个表的唯一标识符,并带回所有信息。我想,让我们解析一下我想要比较的部分,这应该可以解决问题。

SELECT
   D.RECTYPE
   ,D.TYPEDESC
   ,D.PAGE
   ,D.DOCIMGLINK
   ,DMC.DM_ID
   ,DMC.DM_PATH
FROM
   DOCUMENT D
   LEFT JOIN DM_CONTENT DMC
      ON D.DOCIMGLINK = PARSENAME(REPLACE(DMC.DM_PATH, '\', '.'), 2)

没有结果......很奇怪,这应该有效,但事实并非如此。我的解决方案是使用具有相同精确PARSENAME函数的CTE:

WITH CTE AS
(SELECT
   DM_ID
   ,PARSENAME(REPLACE(DM_PATH, '\', '.'), 2) AS 'DM_PATH'
FROM
   DM_CONTENT)
SELECT
      D.RECTYPE
      ,D.TYPEDESC
      ,D.PAGE
      ,D.DOCIMGLINK
      ,CTE.DM_ID
      ,CTE.DM_PATH
   FROM
      DOCUMENT D
      LEFT JOIN CTE
         ON D.DOCIMGLINK = CTE.DM_PATH

带来积极的结果!虽然我有这个有效的解决方案,但我想了解为什么我不能以其他方式做到这一点。我必须要有一些简单的东西,这让我很生气。提前谢谢你帮助我无知的大脑放屁!

1 个答案:

答案 0 :(得分:0)

你确定这是相同的代码???你发布的示例工作得很好......一旦我变成了消耗品。

create table #Document
(
    RECTYPE char(1)
    , TYPEDESC varchar(50)
    , PAGE varchar(10)
    , DOCIMGLINK varchar(50)
)

insert #Document
select 'D', 'Delayed Birth Record', 00127, '1910-007704118500089870170008786670' union all
select 'D', 'Birth RecorD', 00056, '1910-007701897300089870160008810074' union all
select 'D', 'Delayed Birth Record', 00118, '1912-007704116600089870180008786651' union all
select 'D', 'Birth RecorD', 00004, '1913-007702338100089870200008804037' union all
select 'D', 'Birth RecorD', 00249, '1913-007702362500089870210008804281'

create table #DM_CONTENT
(
    DM_ID int
    , DM_PATH varchar(100)
)

insert #DM_CONTENT
select 1, 'BR\1910\1910-007704118500089870170008786670.TIF' union all
select 2, 'BR\1910\1910-007701897300089870160008810074.TIF' union all
select 3, 'BR\1912\1912-007704116600089870180008786651.TIF' union all
select 4, 'BR\1913\1913-007702338100089870200008804037.TIF' union all
select 5, 'BR\1913\1913-007702362500089870210008804281.TIF'

SELECT
   D.RECTYPE
   ,D.TYPEDESC
   ,D.PAGE
   ,D.DOCIMGLINK
   ,DMC.DM_ID
   ,DMC.DM_PATH
FROM
   #DOCUMENT D
   LEFT JOIN #DM_CONTENT DMC
      ON D.DOCIMGLINK = PARSENAME(REPLACE(DMC.DM_PATH, '\', '.'), 2)

drop table #DM_CONTENT
drop table #Document