语句如下:select Month(PayDate) as  Month1,isnull(Pay,0) as 'Pay' from EmployeePayInformation right join  (select DISTINCT MONTH(PayDate) as Month1 from EmployeePayInformation)  a on EmployeePayInformation.Month1=a.Month1
执行时报错:列名 'Month1' 无效
select Month(PayDate) as  Month1,isnull(Pay,0) as 'Pay' from EmployeePayInformationselect DISTINCT MONTH(PayDate) as Month1 from EmployeePayInformation
单独运行,都能成功执行该问题怎么解决

解决方案 »

  1.   

    直接用字段名也不行,DISTINCT MONTH(PayDate)好像不能作为字段名
      

  2.   

    (select Month(PayDate) as  Month1,isnull(Pay,0) as 'Pay' from EmployeePayInformation)b right join  (select DISTINCT MONTH(PayDate) as Month1 from EmployeePayInformation)  a on b.Month1=a.Month1
    这样看看
      

  3.   

    这样就行了
    select b.*
    (select Month(PayDate) as  Month1,isnull(Pay,0) as 'Pay' from EmployeePayInformation ) b
    right join  (select DISTINCT MONTH(PayDate) as Month1 from EmployeePayInformation)  a on b.Month1=a.Month1
      

  4.   

    做出来了
    select Month1,isnull(Pay,0) as Pay from (select Month(PayDate) as  Month1,Pay as Pay from EmployeePayInformation where EmployeeID='100002') a 
    right join (select DISTINCT MONTH(PayDate) as Month2 from EmployeePayInformation) b 
     on a.Month1=b.Month2两个别名要取不同的名字,如Month1,Month2
      

  5.   

    Month(PayDate) as  Month1在查询不处于子查询时,Month(PayDate)是没有列名的
    把条件改一下就行了
    select Month(EmployeePayInformation.PayDate) as  Month1,isnull(Pay,0) as 'Pay' 
    from EmployeePayInformation right join  (select DISTINCT MONTH(PayDate) as Month1 from EmployeePayInformation) a 
    on Month(EmployeePayInformation.PayDate)=a.Month1