简体   繁体   中英

sql server check for greater than with select statement

Why can't I do something like this:

    select distinct ps.ID, from vwList wt
    where Flag = 1 
    and GETDATE()  < (select dbo.fn_Date(ps.date1,ps.UploadDate) from pseres ps where ps.ID = wt.ID)  

I get The multi-part identifier "ps.ID" could not be bound.

Since ps.ID matches wt.ID, and ps is only mentioned in a subquery, why not:

select distinct wt.ID from vwList wt

?

The problem is in the outer select. Try this:

select distinct wt.ID
from vwList wt
where Flag = 1 and
      GETDATE()  < (select dbo.fn_Date(ps.date1,ps.UploadDate) from pseres ps where ps.ID = wt.ID)   

PS is in an inner select in the where clause. If you want to select the column from pseres then you need to have it in the from clause or as a join.

This should work:

SELECT DISTINCT wt.ID
FROM vwList wt
    INNER JOIN (SELECT dbo.fn_Date(ps.date1,ps.UploadDate) AS MyDate FROM pseres) ps ON     wt.ID = ps.ID
WHERE Flag = 1 AND
      MyDate > GETDATE()  

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM