我正在尝试通过使用substring创建一个新列来与数据库中的另一列匹配来进行内部联接,以便在另一个表中显示该名称
代码是
SELECT
REVERSE(SUBSTRING(REVERSE(flh[FIleName]), 0, CHARINDEX('_', REVERSE(flh[FileName])))) AS FILENAMES,
SUBSTRING(flh.[Filename], 0, LEN(flh.[FileName]) - CHARINDEX('_', REVERSE(flh.[FileName])) + 1) AS DATE,
erc.DisplayName
FROM
[Recon_E].[ETL].[tblFileLoadHistory] flh
INNER JOIN
Feed.dbo.tblEnfusionReRunConfig erc on flh.filenames = erc.FileName
WHERE
flh.RecRunId = (SELECT MAX(RecRunId)
FROM Recon_E.etl.tblFileLoadHistory)
我在SubString Select中分隔日期和字符串。
但是我无法将别名文件名提供给新列并执行连接
答案 0 :(得分:0)
如果您使用的是SQL 2005或更高版本,一种解决方案是使用Common Table表达式或CTE,如下所示。
;with FileLoadHist as
-- define cte query
(
SELECT REVERSE(SUBSTRING(REVERSE(flh[FIleName]),0,CHARINDEX('_',REVERSE(flh[FileName])) ))AS FILENAMES,
SUBSTRING(flh.[Filename],0, len(flh.[FileName]) - charindex('_',reverse(flh.[FileName])) +1) AS DATE
FROM [Recon_E].[ETL].[tblFileLoadHistory] flh
where flh.RecRunId=( SELECT max(RecRunId) from Recon_E.etl.tblFileLoadHistory )
)
--define outer query
select FILENAMES, DATE, erc.DisplayName
from FileLoadHist
INNER JOIN Feed.dbo.tblEnfusionReRunConfig erc on FileLoadHist.FILENAMES = erc.FileName