我有一个类似于以下的表
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值。我想知道是否可能。
答案 0 :(得分: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
希望这会解决你的问题......
答案 1 :(得分:0)
您可以在日期列上使用GETDATE()来自动获取它(除非您打算在那里丢失日期值)。在另一种情况下,您可以在该列中设置默认值(或DEFAULT CONSTRAINT),如果这是您的意思,则将默认值设为0。或者你的意思是你想回到已经插入的行并编辑列?
答案 2 :(得分:0)
在存储过程中,您可以将其作为 isNull(列,0)处理它,它将适用于存储过程中的插入或检索查询