如何将NULL或零作为值包含在缺失的月份中

时间:2019-06-21 07:27:29

标签: vertica

在数据中,我们几个月都没有输入了。但是我们要为一组州和市的缺失月份设置NULL或0。 (仅适用于少于最大月份的月份)

数据就像

State   City    Month   Sales
Texas   A   Jan 78
Texas   A   Feb 45
Texas   A   April   32
Texas   A   May 56
Texas   A   July    22
Idaho   B   Jan 24
Idaho   B   Mar 77
Idaho   B   April   23
Idaho   B   Jun 55
Idaho   B   July    99

预期结果是

Texas   A   Jan 78
Texas   A   Feb 45
Texas   A   Mar NULL  or  0
Texas   A   April   32
Texas   A   May 56
Texas   A   Jun NULL  or  0
Texas   A   July    22
Idaho   B   Jan 24
Idaho   B   Feb NULL  or  0
Idaho   B   Mar 77
Idaho   B   April   23
Idaho   B   May NULL  or  0
Idaho   B   Jun 55
Idaho   B   July    99

1 个答案:

答案 0 :(得分:-1)

我希望你做得很好。您可以使用下面的查询来获得所需的结果。您只需将 @MyTable 替换为 YourTableName ,并删除 @MyTable 的声明以及所有 INSERT INTO @MyTable >查询。

DECLARE @Date DATETIME2 = '2019-01-01';
DECLARE @MyTable AS TABLE
    (
      ID INT IDENTITY(1, 1) ,
      State VARCHAR(500) ,
      City VARCHAR(500) ,
      Month VARCHAR(500) ,
      Sales DECIMAL(18, 2)
    )
DECLARE @ResultTable AS TABLE
    (
      ID INT IDENTITY(1, 1) ,
      State VARCHAR(500) ,
      City VARCHAR(500) ,
      Month VARCHAR(500) ,
      Sales DECIMAL(18, 2)
    )
INSERT  INTO @MyTable
        SELECT  'Texas' ,
                'A' ,
                'Jan' ,
                78
INSERT  INTO @MyTable
        SELECT  'Texas' ,
                'A' ,
                'Feb' ,
                45
INSERT  INTO @MyTable
        SELECT  'Texas' ,
                'A' ,
                'April' ,
                32
INSERT  INTO @MyTable
        SELECT  'Texas' ,
                'A' ,
                'May' ,
                56
INSERT  INTO @MyTable
        SELECT  'Texas' ,
                'A' ,
                'July' ,
                22
INSERT  INTO @MyTable
        SELECT  'Idaho' ,
                'B' ,
                'Jan' ,
                24
INSERT  INTO @MyTable
        SELECT  'Idaho' ,
                'B' ,
                'Mar' ,
                77
INSERT  INTO @MyTable
        SELECT  'Idaho' ,
                'B' ,
                'April' ,
                23
INSERT  INTO @MyTable
        SELECT  'Idaho' ,
                'B' ,
                'Jun' ,
                55
INSERT  INTO @MyTable
        SELECT  'Idaho' ,
                'B' ,
                'July' ,
                99

DECLARE @DistinctStates AS TABLE
    (
      StateID INT IDENTITY(1, 1) ,
      StateName VARCHAR(500)
    )
INSERT  INTO @DistinctStates
        SELECT DISTINCT
                State
        FROM    @MyTable
DECLARE @_MinStateID INT= 1;
DECLARE @_MaxStateID INT= ( SELECT  MAX(StateID)
                            FROM    @DistinctStates
                          )
WHILE @_MinStateID <= @_MaxStateID
    BEGIN
        DECLARE @_StateName VARCHAR(500)= ( SELECT TOP 1
                                                    StateName
                                            FROM    @DistinctStates
                                            WHERE   StateID = @_MinStateID
                                          )
        DECLARE @_City VARCHAR(500)= ( SELECT TOP 1
                                                City
                                       FROM     @MyTable
                                       WHERE    State = @_StateName
                                     )
        DECLARE @Min INT= 1
        DECLARE @Max INT= 12
        WHILE @Min <= @Max
            BEGIN
                DECLARE @_Month VARCHAR(150) = ( FORMAT(@Date, 'MMM') );
                IF EXISTS ( SELECT TOP 1
                                    1
                            FROM    @MyTable
                            WHERE   State = @_StateName
                                    AND Month = @_Month )
                    BEGIN
                        INSERT  INTO @ResultTable
                                ( State ,
                                  City ,
                                  Month ,
                                  Sales
                                )
                                SELECT  State ,
                                        City ,
                                        Month ,
                                        Sales
                                FROM    @MyTable
                                WHERE   State = @_StateName
                                        AND Month = @_Month
                    END
                ELSE
                    BEGIN
                        INSERT  INTO @ResultTable
                                ( State ,
                                  City ,
                                  Month ,
                                  Sales
                                )
                                SELECT  @_StateName ,
                                        @_City ,
                                        @_Month ,
                                        0
                    END
                SET @Date = DATEADD(DAY, 1, @Date);
                SET @Min = @Min + 1
            END
        SET @_MinStateID = @_MinStateID + 1
    END
SELECT * FROM @ResultTable