user1951113
user1951113

Reputation:

Subquery column error

An error occurs in my query that I can't solve:

My query:

SELECT [subject], cal
FROM (
    SELECT [subject], cal
    FROM amir
    WHERE textfilter LIKE '% word %') a 
WHERE lev=3 AND cal between '6/10/2012' AND '3/11/2013'

The error occurs at "lev=3"

Msg 207, Level 16, State 1, Line 2
Invalid column name 'lev'.

My table columns are:

    [RecordId] [bigint] IDENTITY(1,1) NOT NULL,
    [text] [nvarchar](max) NOT NULL,
    [textfilter] [nvarchar](max) NOT NULL,
    [mo] [int] NULL,
    [loc] [int] NULL,
    [lev] [int] NOT NULL,
    [cal] [date] NOT NULL,
 CONSTRAINT [PK_SahifehEmam] PRIMARY KEY CLUSTERED 

Upvotes: 0

Views: 55

Answers (1)

You don't select lev in your subquery, so it's not available in the main query.

You could add it to your subquery like this:

select [subject], cal 
from (
    select [subject], cal, lev
    from amir 
    where textfilter like '% word %'
) a 
where lev=3 AND cal between '6/10/2012' AND '3/11/2013'

Although I don't even see why the subquery is necessary:

select [subject], cal 
from amir 
where textfilter like '% word %' and lev=3 AND 
    cal between '6/10/2012' AND '3/11/2013'

Upvotes: 5

Related Questions