简体   繁体   中英

sql left join error using Month(date) in select statment

This code is working:

select Date1,  Date2
from 
    (SELECT Date1 FROM Table1 GROUP BY Date1) t1
left join
    (SELECT Date2 FROM Table2 GROUP BY Date2) t2
on
    t1.Date1 = t2.Date2

But I need to use it extracting Month(...) so when I change code like this I've getting an error:

Unknown column 'Date1' in 'field list'

select Date1,  Date2
from 
    (SELECT Month(Date1) FROM Table1 GROUP BY Date1) t1
left join
    (SELECT Month(Date2) FROM Table2 GROUP BY Date2) t2
on
    t1.Date1 = t2.Date2

Actually the code is bigger and only this part of code does not let me to proceed..

Alias name for month is missing in the inner select query

select Date1,  Date2
from 
    (SELECT Month(Date1) as date1 --Alias missing Here
     FROM Table1 
     GROUP BY Date1) t1
left join
    (SELECT Month(Date2) as date2 --Alias missing here
     FROM Table2 
     GROUP BY Date2) t2
on
    t1.Date1 = t2.Date2

SQL Engine throws an error because it couldn't find this name entity in your subquery. As mentioned by this user you need to provide aliases in subquery.

The alias name is missing while using the Month():

select Date1,  Date2
from 
    (SELECT Month(Date1) month1 FROM Table1 GROUP BY Date1) t1
left join
    (SELECT Month(Date2) month2 FROM Table2 GROUP BY Date2) t2
on
    t1.Date1 = t2.Date2

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