每月收入给出错误(语法','无效)

时间:2016-06-13 20:11:43

标签: sql sql-server tsql

SELECT 
    year(T1.Docdate) as [year],                     
    Month (T1..Docdate) as [Month]                      
FROM 
    (
         Year, (T1.Doctotal) AS [BAL], year(T1.Docdate) as [year]  
     FROM 
         dbo.OCRD T0                        
     LEFT JOIN 
         dbo.OINV T1 ON T1.CardCode = T0.CardCode  
     WHERE
         T0.[CardType] ='C'                         
     UNION                      
     SELECT 
         Year, -(T1.Doctotal) AS [BAL], year(T1.Docdate) as [year]  
     FROM 
         dbo.OCRD T0                        
     LEFT JOIN 
         dbo.ORIN T1 ON T1.CardCode = T0.CardCode  
     WHERE 
         T0.[CardType] = 'C') S                     
PIVOT 
    (SUM(S.[BAL]) FOR [year] IN ([Jan], [Feb], [March], [April])) P

2 个答案:

答案 0 :(得分:0)

您的子查询中缺少SELECT

SELECT year(T1.Docdate) as [year],                      
Month (T1..Docdate) as [Month]                      

FROM **(SELECT** Year, (T1.Doctotal) AS [BAL],year(T1.Docdate) as [year]  FROM dbo.OCRD T0                     
        LEFT JOIN dbo.OINV T1 ON T1.CardCode = T0.CardCode  Where  T0.[CardType] ='C'                         
        UNION                     
        SELECT Year, -(T1.Doctotal)  AS [BAL],year(T1.Docdate) as [year]  FROM dbo.OCRD T0                        
        LEFT JOIN dbo.ORIN T1 ON T1.CardCode = T0.CardCode  Where  T0.[CardType] ='C'                       
) S                     
          PIVOT  (SUM(S.[BAL]) FOR [year] IN                        
         ([Jan],[Feb],[March],[April])) P 

答案 1 :(得分:0)

您的嵌套查询不完整,因为您没有SELECT关键字。正确的查询如下:

SELECT 
    year(T1.Docdate) as [year],                     
    Month (T1..Docdate) as [Month]                      
FROM 
    (
     SELECT Year, (T1.Doctotal) AS [BAL], year(T1.Docdate) as [year]  
     FROM 
         dbo.OCRD T0                        
     LEFT JOIN 
         dbo.OINV T1 ON T1.CardCode = T0.CardCode  
     WHERE
         T0.[CardType] ='C'                         
     UNION                      
     SELECT 
         Year, -(T1.Doctotal) AS [BAL], year(T1.Docdate) as [year]  
     FROM 
         dbo.OCRD T0                        
     LEFT JOIN 
         dbo.ORIN T1 ON T1.CardCode = T0.CardCode  
     WHERE 
         T0.[CardType] = 'C') S                     
PIVOT 
    (SUM(S.[BAL]) FOR [year] IN ([Jan], [Feb], [March], [April])) P