消息207,级别16,状态1,行3无效的列名称“CORE”

时间:2017-06-09 13:02:11

标签: sql sql-server tsql

SELECT X.CORE.Jobs.CreationDate AS 'Date Created', X.CORE.JobControlSheets.PrintJobName AS 'Batch Id', X.CORE.Jobs.JobId AS 'Job Name',
X.CORE.JobToJob.JobToJobId AS 'Sub- Job Name', X.CORE.JobControlSheets.DespatchMethod AS 'Postal Class', X.CORE.JobControlSheets.EnvelopeCode AS 'Envelope Code',
Y.[PrintStatus],Y.[PrintDate],Y.[DespatchDate], Y.[EnclosedDate],Y.[DocsetCount], Y.[FileName]
From 
(SELECT        CORE.Jobs.CreationDate AS 'Date Created', CORE.JobControlSheets.PrintJobName AS 'Batch Id', CORE.Jobs.JobId AS 'Job Name', 
                         CORE.JobToJob.JobToJobId AS 'Sub- Job Name', CORE.JobControlSheets.DespatchMethod AS 'Postal Class', 
                         CORE.JobControlSheets.EnvelopeCode AS 'Envelope Code', CORE.JobControlSheets.PrintJobName
FROM            CORE.Jobs INNER JOIN
                         CORE.JobControlSheets ON CORE.Jobs.JobId = CORE.JobControlSheets.JobId LEFT OUTER JOIN
                         CORE.JobToJob ON CORE.Jobs.JobId = CORE.JobToJob.SourceJobId) AS X
                         INNER JOIN 
(SELECT [PrintStatus],[PrintDate],[DespatchDate],[EnclosedDate],[DocsetCount], [FileName]
FROM 
(SELECT distinct Jc.JobID, 
case when EXists (SELECT distinct jobid FROM [MI_ProdMI].dbo.JOB_Tbl_000_Outstanding JO WHERE JO.jobid=Jc.JobID) 
then 'Inprogress' else 'Complte' end as [PrintStatus],Jb.[FileName] as [FileName]
FROM [MI_ProdMI].dbo.JOB_Tbl_002_Completions Jc 
INNER JOIN [MI_ProdMI].dbo.JOB_Tbl_001_Jobs Jb on Jb.ID=Jc.JobID) as A
left JOIN 
(SELECT JobID,[Timestamp]   as [PrintDate] FROM [MI_ProdMI].dbo.JOB_Tbl_002_Completions jc WHERE 
[Service] = 'PRINT' and JobStatus = 'Printed') as B on A.JobID=B.JobID 
left JOIN 
(
SELECT jobid,sum(Packs) as [DocsetCount],max(DespTimestamp)   as [DespatchDate] FROM [MI_ProdMI].dbo.JOB_Tbl_002_Completions GROUP BY JobID) 
as C on B.JobID=C.JobID
LEFT JOIN 
(SELECT JobID,Timestamp   as [EnclosedDate] FROM [MI_ProdMI].dbo.JOB_Tbl_002_Completions WHERE JobStatus = 'Processed')
  D on C.JobID=D.JobID) AS Y on Y.FileName=X.FileName

1 个答案:

答案 0 :(得分:0)

您在第一个子查询中使用列别名犯了错误。在连接表时也使用表别名,它将具有更好的可读性和易用性。

您可以在第一个子查询中使用out别名检索列名,稍后可以在外部SELECT中添加别名。

您可以尝试以下查询:

SELECT X.CreationDate AS 'Date Created', X.PrintJobName AS 'Batch Id', X.JobId AS 'Job Name', X.JobToJobId AS 'Sub- Job Name', 
       X.DespatchMethod AS 'Postal Class', X.EnvelopeCode AS 'Envelope Code', 
       Y.[PrintStatus], Y.[PrintDate], Y.[DespatchDate], Y.[EnclosedDate], Y.[DocsetCount], Y.[FileName]
FROM (
    SELECT [CJ].CreationDate, [CJC].PrintJobName, [CJ].JobId, [CJJ].JobToJobId, [CJC].DespatchMethod, [CJC].EnvelopeCode
    FROM CORE.Jobs [CJ]
    INNER JOIN CORE.JobControlSheets [CJC] ON [CJ].JobId = [CJC].JobId
    LEFT OUTER JOIN CORE.JobToJob [CJJ] ON [CJ].JobId = [CJJ].SourceJobId
    ) AS X
INNER JOIN (
    SELECT [PrintStatus], [PrintDate], [DespatchDate], [EnclosedDate], [DocsetCount], [FileName]
    FROM (
        SELECT DISTINCT Jc.JobID, CASE 
                WHEN EXISTS (
                        SELECT DISTINCT jobid
                        FROM [MI_ProdMI].dbo.JOB_Tbl_000_Outstanding JO
                        WHERE JO.jobid = Jc.JobID
                        )
                    THEN 'Inprogress'
                ELSE 'Complte'
                END AS [PrintStatus], Jb.[FileName] AS [FileName]
        FROM [MI_ProdMI].dbo.JOB_Tbl_002_Completions Jc
        INNER JOIN [MI_ProdMI].dbo.JOB_Tbl_001_Jobs Jb ON Jb.ID = Jc.JobID
        ) AS A
    LEFT JOIN (
        SELECT JobID, [Timestamp] AS [PrintDate]
        FROM [MI_ProdMI].dbo.JOB_Tbl_002_Completions jc
        WHERE [Service] = 'PRINT'
            AND JobStatus = 'Printed'
        ) AS B ON A.JobID = B.JobID
    LEFT JOIN (
        SELECT jobid, sum(Packs) AS [DocsetCount], max(DespTimestamp) AS [DespatchDate]
        FROM [MI_ProdMI].dbo.JOB_Tbl_002_Completions
        GROUP BY JobID
        ) AS C ON B.JobID = C.JobID
    LEFT JOIN (
        SELECT JobID, TIMESTAMP AS [EnclosedDate]
        FROM [MI_ProdMI].dbo.JOB_Tbl_002_Completions
        WHERE JobStatus = 'Processed'
        ) D ON C.JobID = D.JobID
    ) AS Y ON Y.FileName = X.FileName