0

我有一个类似于以下的表

Date   | qty1 | qty2 | qty3 | qty4 | column6
1 july | 33   | 0    |  0   | 0    | xyz
1 July | 0    | 20   | 0    | 0    | xyz
1 July | 18   | 0    | 0    | 0    | abc
3 July | 0    | 0    | 0    | 11   | abc
3 july | 23   | 0    |  0   | 0    | xyz
4 July | 0    | 40   | 0    | 0    | xyz
4 July | 57   | 0    | 0    | 0    | abc
7 July | 0    | 12   | 0    | 0    | xyz

我正在 sql server 2005 的存储过程中寻找如下输出。

 Date   | qty1 | qty2 | qty3 | qty4 
 1 July | 51   | 20   | 0    | 0 
 2 July | 0    | 0    | 0    | 0
 3 July | 23   | 0    | 0    | 11
 4 July | 57   | 40   | 0    | 0
 5 July | 0    | 0    | 0    | 0
 6 July | 0    | 0    | 0    | 0
 7 July | 0    | 12   | 0    | 0

正如您所注意到的,我希望它自动为缺失日期输入 0 值。我想知道这是否可能。

4

3 回答 3

1

如果您可以从实际表中默认生成一个月内的所有日期:1意味着解决方案将非常简单......就像......

 SELECT     date  AS Date ,
            ISNULL(qty1, '') AS qty1,
            ISNULL(qty2, '') AS qty2,
            ISNULL(qty3, '') AS qty3,
            ISNULL(qty4, '') AS qty4,
    FROM    your_table_name where for_all_date

否则你想做一些这样的事情......

CREATE PROCEDURE put_null ()
AS 
DECLARE @start_date DATETIME
DECLARE @End_date DATETIME
    BEGIN             
      SELECT TOP 1 @start_date = date FROM your_table_name ORDER BY Date   
      SELECT TOP 1 @End_date = date FROM your_table_name ORDER BY Date  DESC 
        WHILE(@start_Date < = @End_date)
        BEGIN
        SELECT  @start_date AS Date ,
                ISNULL(qty1, 0) AS qty1,
                ISNULL(qty2, 0) AS qty2,
                ISNULL(qty3, 0) AS qty3,
                ISNULL(qty4, 0) AS qty4,
        FROM    your_table_name

        SET @start_date = DATEADD(DAY,1,@start_date)
        END
    END     

希望这会解决你的问题....

于 2012-09-07T10:23:25.010 回答
0

您可以在日期列上使用 GETDATE() 来自动获取它(除非您打算缺少日期值)。在另一种情况下,您可以在该列中设置默认值(或 DEFAULT CONSTRAINT),如果这是您的意思,则将默认值设为 0。或者你的意思是你想回到已经插入的行并编辑列?

于 2012-09-02T11:07:28.917 回答
0

在存储过程中,您可以将其处理为isNull(column,0)它适用于存储过程中的插入或检索查询

于 2012-09-03T08:26:59.567 回答