在sql server 2005中的其他计算中使用列别名

时间:2009-10-13 13:14:24

标签: sql-server-2005

我正在使用以下查询。我想在后面的其他计算中使用Date_Sent_to_Recorder。我收到错误“无效的列名'Date_Sent_to_Recorder'。”人们建议使用CTE,但我无法在此场景中使用它。我有一个相关的子查询,可以计算Date_Sent_to_Recorder.Please帮助。

 SELECT    
 C.Name Client ,
 SecONdary_Document.Primary_Document_ID ,
 SecONdary_Document.Secondary_Document_Id ,
 Primary_Document.State + ',' + GB_Counties.CountyName State ,
 Grantor.Name Grantor ,
 Loan_Number ,
 CONVERT(VARCHAR(10), Primary_Document.PIF_Date, 101) PIF_Date ,

  ( SELECT    CASE WHEN dbo.SECONDARY_DOCUMENT.Status_ID = 21
                                     THEN CONVERT(VARCHAR(10), dbo.SECONDARY_DOCUMENT.Updated_Dt, 101)
                                     ELSE ( SELECT TOP 1
                                                    CONVERT(VARCHAR(10), dbo.SECONDARY_DOCUMENT_STATUS_HISTORY.Created_Dt, 101)
                                            FROM    dbo.SECONDARY_DOCUMENT_STATUS_HISTORY
                                            WHERE   dbo.SECONDARY_DOCUMENT_STATUS_HISTORY.Secondary_Document_ID = SecONdary_Document.Secondary_Document_Id
                                                    AND SECONDARY_DOCUMENT_STATUS_HISTORY.Status_ID = 21
                                            ORDER BY dbo.SECONDARY_DOCUMENT_STATUS_HISTORY.Created_Dt DESC
                                          )
                                END
                    ) AS Date_Sent_to_Recorder ,

                    Satis_TimeFrame ,
                    CASE WHEN PIF_Date IS NULL
                              OR Date_Sent_to_Recorder IS NULL THEN NULL
                         ELSE DATEDIFF(DAY, PIF_Date,
                                       Date_Sent_to_Recorder)
                    END TotalDays ,
                    CASE WHEN PIF_Date IS NULL
                              OR Date_Sent_to_Recorder IS NULL THEN NULL
                         ELSE CASE WHEN DATEDIFF(DAY, PIF_Date,
                                                 ISNULL(Date_Sent_to_Recorder,
                                                        GETDATE())) > Satis_TimeFrame
                                   THEN 'N'
                                   ELSE 'Y'
                              END
                    END InCompliance ,
                    Loan_Name ,
                    Deal_Name ,
                    Deal.Deal_Id ,
                    Loan.Loan_Id
          FROM      Primary_Document
                    INNER JOIN SecONdary_Document ON SecONdary_Document.Primary_Document_ID = Primary_Document.Primary_Document_ID
                    INNER JOIN Status ON Status.Status_Id = SecONdary_Document.Status_Id
                    INNER JOIN GB_Counties ON GB_Counties.CountyId = Primary_Document.County_Id
                    INNER JOIN Loan ON Loan.Loan_Id = Primary_Document.Loan_Id

EDIT ------------

DECLARE @Date_Sent_to_Recorder  varchar(10)
SELECT  C.Name Client ,
    SecONdary_Document.Primary_Document_ID ,
    SecONdary_Document.Secondary_Document_Id ,
    Primary_Document.State + ',' + GB_Counties.CountyName State ,
    Grantor.Name Grantor ,
    Loan_Number ,
    CONVERT(VARCHAR(10), Primary_Document.PIF_Date, 101) PIF_Date ,
                                                     --<START>
                                                    --3021,RRaghuvansi,If current status is 21 in SECONDARY_DOCUMENT then take Updated_Dt else take Created_Dt in SECONDARY_DOCUMENT_STATUS_HISTORY with status Out For Recorder                                                         
                                                    --CONVERT(VARCHAR(20), Recording_Date, 101) AS Recording_Date ,

    @Date_Sent_to_Recorder=[dbo].[GET_RecordingDate](SecONdary_Document.Secondary_Document_Id),                                             --<END> 
    Satis_TimeFrame ,

    Loan_Name ,
    Deal_Name ,
    Deal.Deal_Id ,
    Loan.Loan_Id
 FROM Primary_Document

3 个答案:

答案 0 :(得分:2)

由于您的架构有点不清楚,下面是派生表语法的简要示例,并引用派生表中的值

Declare @Sample table(id1 int, id2 int, value varchar(20))

insert into @sample values (1,1, 'this')
insert into @sample values(2,2, 'that')
insert into @sample values(3,2, 'the other')

Select t1.ID1, t1.Value, t2.RevVal, 
case 
when t2.RevVal = 'siht' then 1 else 0
end as RevIsThisBackwards
from @sample t1
inner join (Select id1, reverse(value) as RevVal from @sample) t2
on t1.ID1 = t2.ID1

结果

  id    VALUES  Rev     Rev Value is this backwards
    1   this    siht    1
    2   that    taht    0
    3   the other   rehto eht   0

因此,您需要移动计算列,并将足够的列连接到派生表中,在您的情况下,它将看起来类似于下面的

    INNER JOIN Loan ON Loan.Loan_Id = Primary_Document.Loan_Id
    INNER JOIN (Select SomPKField, CASE WHEN dbo.SECONDARY_DOCUMENT.Status_ID = 21
        THEN CONVERT(VARCHAR(10), dbo.SECONDARY_DOCUMENT.Updated_Dt, 101)
        ELSE ( SELECT TOP 1
        CONVERT(VARCHAR(10), dbo.SECONDARY_DOCUMENT_STATUS_HISTORY.Created_Dt, 101)
            FROM    dbo.SECONDARY_DOCUMENT_STATUS_HISTORY
    WHERE   
dbo.SECONDARY_DOCUMENT_STATUS_HISTORY.Secondary_Document_ID = SecONdary_Document.Secondary_Document_Id
           AND SECONDARY_DOCUMENT_STATUS_HISTORY.Status_ID = 21
            ORDER BY dbo.SECONDARY_DOCUMENT_STATUS_HISTORY.Created_Dt DESC
          )
        END as Date_Sent_to_Recorder
    ) as Sub1
    on SomeTable.SomePKField = Sub1.SomePKField


**I edit your edit in this edit.** 

SELECT  C.Name Client ,
    SecONdary_Document.Primary_Document_ID ,
    SecONdary_Document.Secondary_Document_Id ,
    Primary_Document.State + ',' 
    + GB_Counties.CountyName State ,
    Grantor.Name Grantor ,
    Loan_Number ,
    CONVERT(VARCHAR(10), Primary_Document.PIF_Date, 101) PIF_Date ,
    [dbo].[GET_RecordingDate]
    (SecONdary_Document.Secondary_Document_Id) 
    As Date_Sent_To_Recorder
    Satis_TimeFrame ,
    Loan_Name ,
    Deal_Name ,
    Deal.Deal_Id ,
    Loan.Loan_Id
 FROM Primary_Document

答案 1 :(得分:0)

您无法在SELECT的其他列中引用计算列。您尝试实现的最简单的解决方案是预先计算Date_Sent_to_Recorder并将其存储在临时表中,然后在进行主查询时加入该表。

Protip:使用正确的格式和表别名;像你发布的SQL难以阅读,几乎无法理解。

编辑:类似这样的事情:

create table #temp
           ( Primary_Document_ID   int,
             Date_Sent_To_Recorder datetime )

insert #temp
select Primary_Document_ID,
       CASE
         WHEN dbo.SECONDARY_DOCUMENT.Status_ID = 21
           THEN CONVERT(VARCHAR(10), dbo.SECONDARY_DOCUMENT.Updated_Dt, 101)
         ELSE
           (SELECT TOP 1
                   CONVERT(VARCHAR(10), dbo.SECONDARY_DOCUMENT_STATUS_HISTORY.Created_Dt, 101)
              FROM dbo.SECONDARY_DOCUMENT_STATUS_HISTORY
             WHERE dbo.SECONDARY_DOCUMENT_STATUS_HISTORY.Secondary_Document_ID = SecONdary_Document.Secondary_Document_Id
               AND SECONDARY_DOCUMENT_STATUS_HISTORY.Status_ID = 21
             ORDER BY dbo.SECONDARY_DOCUMENT_STATUS_HISTORY.Created_Dt DESC)
       END
  from SecONdary_Document

-- main select, joining to #temp

答案 2 :(得分:0)

由于Date_Sent_to_Recorder仅取决于SECONDARY_DOCUMENT & SECONDARY_DOCUMENT_STATUS_HISTORY表,因此您可以通过以下内容替换INNER JOIN中的SECONDARY_DOCUMENT(这会将Date_Sent_to_Recorder作为列):

SELECT SD.Primary_Document_ID,
                   SD.Secondary_Document_Id,
                   CASE WHEN SD.Status_ID = 21
                        THEN CONVERT(VARCHAR(10), SD.Updated_Dt, 101)
                        ELSE CONVERT(VARCHAR(10), SDSH.Created_Dt, 101)
                   END Date_Sent_to_Recorder
              FROM SECONDARY_DOCUMENT SD
        INNER JOIN dbo.SECONDARY_DOCUMENT_STATUS_HISTORY SDSH 
                ON SDSH.Secondary_Document_ID = SD.Secondary_Document_Id
               AND SDSH.Status_ID = 21
        INNER JOIN (SELECT Secondary_Document_ID, max(Created_Dt) Created_Dt
                      FROM dbo.SECONDARY_DOCUMENT_STATUS_HISTORY
                     WHERE Status_ID = 21 
                  GROUP BY Secondary_Document_ID) SDSH2 
                ON SDSH.Secondary_Document_ID = SDSH2.Secondary_Document_Id 
               AND SDSH.Created_Dt = SDSH2.Created_Dt

最后你有:

SELECT C.Name Client,
       SecONdary_Document.Primary_Document_ID,
       SecONdary_Document.Secondary_Document_Id,
       Primary_Document.State + ',' + GB_Counties.CountyName State,
       Grantor.Name Grantor,
       Loan_Number,
       CONVERT(VARCHAR(10), Primary_Document.PIF_Date, 101) PIF_Date,
       SecONdary_Document.Date_Sent_to_Recorder,
       Satis_TimeFrame,
       CASE WHEN PIF_Date IS NULL 
                 OR SecONdary_Document.Date_Sent_to_Recorder IS NULL 
            THEN NULL
            ELSE DATEDIFF(DAY, PIF_Date, 
                               SecONdary_Document.Date_Sent_to_Recorder)
       END TotalDays,
       CASE WHEN PIF_Date IS NULL 
                 OR SecONdary_Document.Date_Sent_to_Recorder IS NULL THEN NULL
            ELSE CASE WHEN DATEDIFF(DAY, PIF_Date,
                                ISNULL(SecONdary_Document.Date_Sent_to_Recorder
                                ,GETDATE())) > Satis_TimeFrame
                      THEN 'N'
                      ELSE 'Y'
                 END
       END InCompliance,
       Loan_Name,
       Deal_Name,
       Deal.Deal_Id,
       Loan.Loan_Id
 FROM Primary_Document
INNER JOIN (SELECT SD.Primary_Document_ID,
                   SD.Secondary_Document_Id,
                   CASE WHEN SD.Status_ID = 21
                        THEN CONVERT(VARCHAR(10), SD.Updated_Dt, 101)
                        ELSE CONVERT(VARCHAR(10), SDSH.Created_Dt, 101)
                   END Date_Sent_to_Recorder
              FROM SECONDARY_DOCUMENT SD
        INNER JOIN dbo.SECONDARY_DOCUMENT_STATUS_HISTORY SDSH 
                ON SDSH.Secondary_Document_ID = SD.Secondary_Document_Id
               AND SDSH.Status_ID = 21
        INNER JOIN (SELECT Secondary_Document_ID, max(Created_Dt) Created_Dt
                      FROM dbo.SECONDARY_DOCUMENT_STATUS_HISTORY
                     WHERE Status_ID = 21 
                  GROUP BY Secondary_Document_ID) SDSH2 
                ON SDSH.Secondary_Document_ID = SDSH2.Secondary_Document_Id 
               AND SDSH.Created_Dt = SDSH2.Created_Dt) SecONdary_Document 
        ON SecONdary_Document.Primary_Document_ID
                          = Primary_Document.Primary_Document_ID
INNER JOIN Status ON Status.Status_Id = SecONdary_Document.Status_Id
INNER JOIN GB_Counties ON GB_Counties.CountyId = Primary_Document.County_Id
INNER JOIN Loan ON Loan.Loan_Id = Primary_Document.Loan_Id