SQL Server:查看大小写

时间:2018-08-28 14:31:47

标签: sql sql-server

我试图在SQL Server 2014中创建视图,但是当我执行SQL语句时,出现此错误:

  

无效的列名“ MONTHNUMBER”

我的声明:

CREATE VIEW [Metadata].[NEW_VIEW]
AS
    SELECT   
        dbo.fnFirstDayOfMonth(a.Year,a.Month) AS FR_OBS_DATE,
        b.Code AS COUNTRY,
        a.Groups AS COMMODITY,
        a.ImpValue AS OBS_VALUE,
        a.DexValue AS OBS_VALUE_2,
        a.RexValue AS OBS_VALUE_3,
        a.TexValue AS OBS_VALUE_4,
        0 AS UNIT_MULT,
        'NUM' AS UNIT_MEASURE,
        a.Month AS MONTHNUMBER,
        CASE a.Month
                WHEN 'January' THEN 1
                WHEN 'February' THEN 2
                WHEN 'March' THEN 3
                WHEN 'April' THEN 4
                WHEN 'May' THEN 5
                WHEN 'June' THEN 6
                WHEN 'July' THEN 7
                WHEN 'August' THEN 8
                WHEN 'September' THEN 9
                WHEN 'October' THEN 10
                WHEN 'November' THEN 11
                WHEN 'December' THEN 12
        END,
        CAST(a.Year AS varchar(10)) + '-' + CAST(MONTHNUMBER AS varchar(10)) AS TIME_PERIOD
    FROM 
        [Trade].[HS] a
    INNER JOIN 
        [Codelist].[Countries] b ON a.CountryName = b.Name

我希望能指出我做错了什么。

3 个答案:

答案 0 :(得分:1)

尝试一下,MONTHNUMBERa.Month的别名,您不能在此处使用别名:

CREATE VIEW [Metadata].[NEW_VIEW]
AS
SELECT   --dbo.fnFirstDayOfMonth(a.Year,a.Month) AS FR_OBS_DATE, I think your problem is here
        b.Code AS COUNTRY,
        a.Groups AS COMMODITY,
        a.ImpValue AS OBS_VALUE,
        a.DexValue AS OBS_VALUE_2,
        a.RexValue AS OBS_VALUE_3,
        a.TexValue AS OBS_VALUE_4,
        0 AS UNIT_MULT,
        'NUM' AS UNIT_MEASURE,

        a.Month AS MONTHNUMBER,
            CASE a.Month
                WHEN 'January' THEN '1'
                WHEN 'February' THEN '2'
                WHEN 'March' THEN '3'
                WHEN 'April' THEN '4'
                WHEN 'May' THEN '5'
                WHEN 'June' THEN '6'
                WHEN 'July' THEN '7'
                WHEN 'August' THEN '8'
                WHEN 'September' THEN '9'
                WHEN 'October' THEN '10'
                WHEN 'November' THEN '11'
                WHEN 'December' THEN '12'
            END AS MonthName,

        CAST(a.Year AS varchar(10)) + '-' + 
              CASE a.Month
                WHEN 'January' THEN '1'
                WHEN 'February' THEN '2'
                WHEN 'March' THEN '3'
                WHEN 'April' THEN '4'
                WHEN 'May' THEN '5'
                WHEN 'June' THEN '6'
                WHEN 'July' THEN '7'
                WHEN 'August' THEN '8'
                WHEN 'September' THEN '9'
                WHEN 'October' THEN '10'
                WHEN 'November' THEN '11'
                WHEN 'December' THEN '12'
            END AS TIME_PERIOD

FROM [Trade].[HS] a
INNER JOIN [Codelist].[Countries] b
    ON a.CountryName = b.Name
GO

答案 1 :(得分:0)

MONTHNUMBER尚未绑定

CAST(a.Year AS varchar(10)) + '-' + CAST(MONTHNUMBER AS varchar(10)) AS TIME_PERIOD

因此,我相信您需要在其位置使用a.MONTH,例如

CAST(a.Year AS varchar(10)) + '-' + CAST(a.Month AS varchar(10)) AS TIME_PERIOD

答案 2 :(得分:0)

我将这种逻辑简化为:

SELECT . . .,
       month(cast(a.month + ' 2000' as date) as monthnumber,
       convert(varchar(7), cast(a.month + cast(year as varchar(255)) as date), 121) as time_period
FROM . . .

SQL Server具有相当灵活的日期转换功能。在这种情况下,它们非常方便。