在缺少日期的地方填写表格数据

时间:2014-02-10 08:03:38

标签: sql sql-server vb.net

我有一张包含以下数据的表格:

branch  day   amount   discount   gst
001     4     300.00   130.00     9.32
001     10    200.12   211.00     18.00
001     30    343.22   122.00     8.22
002     12    423.00   123.00     8.00
005     2     453.21   232.11     9.99
005     12    111.21   99.00      0.21
005     27    321.99   12.00      0.00

我希望通过使用vb.net将值插入表中来填充缺少的日子,就像这样

branch  day   amount   discount   gst
001     1     0.00     0.00       0.00
001     2     0.00     0.00       0.00
001     3     0.00     0.00       0.00
001     4     300.00   130.00     9.32
001     5     0.00     0.00       0.00
001     6     0.00     0.00       0.00
001     7     0.00     0.00       0.00
001     8     0.00     0.00       0.00
001     9     0.00     0.00       0.00
001     10    200.12   211.00     18.00
.......

从dayinmonth开始的总天数

更新:

我认为这样做:

*按日排序第一个表

jj = 1
while jj <> DaysInMonth(mth, yr)
if jj <> rowTable.item("day")
 * insert the value here.
jj = jj + 1
end if

2 个答案:

答案 0 :(得分:0)

您可以从以下查询中获取缺失的日期

DECLARE @MIN INT
DECLARE @MAX INT
SELECT @MIN = MIN(dayField) + 1, @MAX = MAX(dayField) - 1 FROM YourTable
CREATE TABLE #TMP (FIELD_NO INT)
WHILE @MIN <= @MAX
BEGIN
   IF NOT EXISTS (SELECT * FROM YourTable WHERE dayField = @MIN)
      INSERT INTO #TMP (FIELD_NO) VALUES (@MIN)
   SET @MIN = @MIN + 1
END

SELECT * FROM #TMP
DROP TABLE #TMP

现在您可以从结果集创建INSERT语句。

答案 1 :(得分:0)

尝试此查询

declare @tab table(branch varchar(3), day int,  amount float) ;
insert into @tab values
('001',4,300.00),
('001',10,200.12),
('001',30,343.22),
('002',12,423.00),
('005',2,453.21),
('005',12,111.21),
('005',27,321.99);

with month_days as
(select t.number,t1.branch from (select number from master..spt_values where type='P' and number between 1 and 30) t cross join (select distinct branch from @tab)t1) 
select md.branch,number as day,isnull(amount,0) amount from @tab t right join month_days md on  t.day=md.number and t.branch=md.branch;
相关问题