views:

77

answers:

3

I am working with the following query.I want to use Date_Sent_to_Recorder in my other calculations which follows.I am getting an error "Invalid column name 'Date_Sent_to_Recorder'." People recommended using a CTE for this but i am not able to fit it in this scenerio.I have a correlated subquery which calculates Date_Sent_to_Recorder.Please help.

 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
A: 

You cannot refer to a computed column in other columns of a SELECT. The simplest solution for what you are trying to achieve is to pre-calculate Date_Sent_to_Recorder and store it in a temporary table, then join to that table when doing your main query.

Protip: use proper formatting and table aliases; SQL like you posted is difficult to read, and almost impossible to understand.

Edit: something like this:

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
Ian Kemp
ok,I will format it next time i will post some query.
Rohit
+2  A: 

Since your schema is sort of unclear, here is a brief sample for the derived table syntax and referencing values from the derived table

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

Results

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

So you'll want to move the calculated column, and sufficient columns to join to into a derived table, in your case it would it would look similar to the below

    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
cmsjr
Can i use a function as I have a correlated subquery in which i am finding this date for all the Secondary_Document_ID that are there in the select list.What i m thinking is to create that function with the parameter Secondary_Document_ID and use that function in select list and then use a variable to get this date.
Rohit
I don't see any reason why you couldn't use an inline function.
cmsjr
when i am assigning a value to a variable it says "A SELECT statement that assigns a value to a variable must not be combined with data-retrieval operations.".How to deal with this
Rohit
can you post a sample?
cmsjr
I have posted it as an EDIT to this post.Please refer to it.
Rohit
Take out the variable definition and reference, replace it with, [dbo].[GET_RecordingDate](SecONdary_Document.Secondary_Document_Id) as Date_Sent_to_Recorder. You just want to include the return value from the function as a column, not as a variable. If you need to test the value in other portions of the query, do not reference the variable or the name, reference he return value of the function.
cmsjr
Ya,It worked very well.Thanks a lot.
Rohit
Super, glad I could help.
cmsjr
A: 

Because Date_Sent_to_Recorder depends only on SECONDARY_DOCUMENT & SECONDARY_DOCUMENT_STATUS_HISTORY tables, you can replace SECONDARY_DOCUMENT in the INNER JOIN by the following (which gives you Date_Sent_to_Recorder as a column):

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

and finally you have:

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
najmeddine